I have a Mouse Click Event handler which works when i select the first time but..? - winforms

I have a DataGridView in C# which is connected with my Access database. I see all the books on my DataGridView. I have created a mouse Click event handler to select a row than I can save that row, as you see below:
private void Mouse_Click(object sender, MouseEventArgs e)
{
try
{
temp = dataGridView1.SelectedRows[0].Index.ToSt…
//This one is selecting the row. To do that select panel>event> on MouseClick write name and double click:
MessageBox.Show("You have selected the row " + temp);
}
catch
{
}
}
It works when I open the form. However, after I save the selected row to another database, the Mouse Click event handler stop working. In other words, after saving the first selected row, when I come back to select another row and save to database the Mouse Click Event handler does not work. The Message says row is out of range.

You didn't give too much information related to connection type, Event Handler and more importantly the way you save/update the record so I'm just guessing here: aren't you accidentally overwriting the DataSource of your grid? That's the only thing I can imagine for such behavior ("row is out of range" - which also assumes the event still works as intended but the code in the function cannot see the data anymore).

Related

How to update database using dataset, tablemanager, and bindingsource?

This is a pretty small program, so I just used the autogenerated code for my datasource and dragged/dropped a datagridview on my WinForm. I'm trying to manually update a column in a row, but it fails each time I get to the "UpdateAll" of my tablemanager. Here's my code for double click of cell content:
private void dgv1DataGridView_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
try
{
string delMemberID = dgv1DataGridView.Rows[e.RowIndex].Cells[0].Value.ToString();
ds1DataSet.Table1.Rows[ds1DataSet.Table1.Rows.IndexOf(ds1DataSet.Table1.FindByMemberID(delMemberID))][9] = MemberID.ToString();
this.Validate();
this.bs1BindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.ds1DataSet);
MessageBox.Show("Replacement Successful");
this.Close();
}
catch
{
MessageBox.Show("Replacement Failed");
}
}
My expectations are; when a user double clicks anywhere inside a row of the datagridview, it will update that row with a different "MemberID" for that column, save the data everywhere (including database), and then close the form (to return the parent form).
For some reason, my tableAdapterManager wasn't linked to my TableAdapter on that form - once I linked them, the above code worked fine.
To be more precise, I clicked on the design view of my form and clicked the the tableAdapterManager at the bottom of the design window. Then looking in the "Properties" window, I noticed that the TableAdapter was blank (if you used a wizard to create the DataSource, then it will be named the same as the table in your DataSource). Once I selected the correct name for the TableAdapter, everything worked perfect.

rowdeselect event can't fire to save data when it is in last row in Extjs EditorGridPanel

I am using EditorGridPanel with cellEditor which acts nearly like Excel. We implemented Arrow Keys to move among rows and
columns. We do row validation when user moves from one row to another (specifically in rowdeselect event) and then save the
record. There is some issues are:
For the last row of the grid, rowdeselect event does not fire, as we do not have any other control after the grid.
rowdeselect event fires if we move from row to row using Tab, Enter & Arrow keys. But when user clicks on another row using mouse - events do not come in correct sequence, so focus moves to the new row, but earlier row not saved. Currently we solved this by calling stopEditing at the beginning of rowdeselect event.
We would like to know how we can solve these two issues and whether there are more robust ways to handle automatic grid saving.
You can fill free to check the problem in our site. -> http://www.softworksbd.com/swazilandlmis/yyyy_stockdata.php
1 ) First of all the event is not firing on the blur of the whole grid and not just the last row. try changing a row and clicking anywhere on the screen but the grid and you will see that no validation takes place (which means your rowdselect does not fire).
You should try and add a blue event to the whole grid that runs your validation scripts.
2) As far as I have checked the event is fired even on another row click so please explain exactly what you mean...
ps
I have viewed only this page in your application:

Datagridview : how to reload the data?

I have a populated Datagridview, which gets its data from a DataTableAdapter.
I have a separate button on my form. When I click the button I want the DataTable to refresh and repopulate completely.
Seems so easy, but I can't find a way to do this.
Thanks in advance
If you wish to swap the code for dynamic grid functionality, right click on your datatable and add a new query (green box in the SS):
Select * from tablename
Then go locate the line in the form_load that takes care of the data population:
this.yourTableAdapter.Fill(this.yourDataSet.yourdatatable);
The default SQL query is always called .Fill, but in case you add a new query the second one will be called (orange box in SS):
this.yourTableAdapter.FillBy(this.yourDataSet.yourdatatable);
By running this second method somewhere in your code (a button press maybe) the grid should change to the new sql command and display the new results in the gridview:
protected void Button1_Click(object sender, EventArgs e)
{
this.yourTableAdapter.FillBy(this.yourDataSet.yourdatatable);
}

DevExpress XtraGrid checkbox check not registered unless focus changes

We have a databound XtraGrid on our Windows form. One of the columns is a check box. The problem is as follows: when users check the checkbox and click OK button, the checkbox, while visibly checked, is not considered checked by the grid. When I do this (while looping through rows):
isAllowed = Convert.ToBoolean(viewMain.GetRowCellValue(nRowCtr, "IsAllowed"))
I get back False. BUT, if the user checks the box, and then clicks somewhere else on the form or on another row in this grid, thus taking away focus from the checked checkbox, the same code above will return True.
Any insight on how to fix this behavior would be greatly appreciated.
Workaround found:
With default settings, when users click on a cell to edit it, the cell goes into edit mode, loads the editor control (in this case I have a CheckEdit repository control) and changes control's value (in this case checked state). If I click on another row, or another control, the cell then gets out of edit mode, committing the change to data item. But if I click on a button, then my change is lost. The workaround is to use the CheckEdit's CheckedChanged event to close editor:
Private Sub edCheck_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles edCheck.CheckedChanged
gridYears.FocusedView.CloseEditor()
End Sub
There's actually a cleaner way of doing this (it works for all RepositoryItems), detailed on the DevExpress site. The idea is to call the GridView.PostEditor method from a repository item's EditValueChanged event handler to immediately save the edited value to the grid's cell and the underlying column.
This code in the view's CellValueChanging event handler solved the problem:
private void OnCellValueChanging(object sender, CellValueChangedEventArgs e)
{
_gridView.SetFocusedRowCellValue(_gridView.FocusedColumn, e.Value);
}

Button simulating keyboard "Shift" key pressed

I Have a WPF window that have a RadGridView and a Button,
My requirement is, when you click on the button and
select a row in the grid, it should work similar to
pressing on the shift key and clicking on a gridview row.
(To select multiple rows).
So, i need to press the shift key programatically in button
click event.
Can this be done?
Appreciate if anyone can provide a solution
Do not fire a KeyPress event manually. Rather, on this page, Telerik indicates that to allow multiple selection, you must set the SelectionMode to Extended:
this.radGridView.SelectionMode = System.Windows.Controls.SelectionMode.Extended;
So, put this in your button click (I guess you'll want it to toggle between Extended and Single. Multiple's behaviour is awkward, but that's just me) and you're good to go.
Edit:
The toggle button will have something like this in its click event handler:
private void toggleButton1_Click(object sender, RoutedEventArgs e)
{
this.radGridView.SelectionMode = toggleButton1.IsChecked ? SelectionMode.Extended : SelectionMode.Single;
}

Resources