Reputation: 4652
I Have two tables I select a record from table 1 to show information in table 2 on double click in one of information I got window to update data after closing this window I have to refresh the two tables and I want the same row be selected in table 1. I was working in wpf and I use selectedindex.
Now I use this for the moment:
int index = dgTable1.Grid.CurrentRow.Index;
///**
frm.ShowDialog();
frm.Dispose();
ReloadTable1();
selectedindex(index)
with
private void selectindex(int index)
{
dgwTable1.Grid.Rows[index].Selected = true;
DATAtype data= dgwTable1.GetObjectFromRow<DATAtype>(index);
LoadTable2(data);
}
It work But I have Grid_SelectionChanged
for table1 and don't fire i have to reload data, also If the scroll bar is down when I use this I return to the top of table 1!
But I know this not the right way to do it:( and it's too simple in wpf :/
dgTable is a UserControl
with a DataGridView
as a Grid
Upvotes: 0
Views: 14783
Reputation: 19
To Save Current Index :
int index = dataGridView1.CurrentRow.Index;
After Editing:
dataGridView1.Rows[index].Selected = true;
Hope this helps.
Upvotes: 2
Reputation: 1044
In my opinion, the easiest way to solve your problems, is to use the MVVM pattern. In your ViewModel for your View that contains the two DataGrids, you have a property like "SelectedNameOfContentClass" that is bound to first DataGrids SelectedValue-property. This "SelectedNameOfContentClass"-property is also bound to seconds datagrid DataSource. So if you changes the selected row in first DataGrid, the Source of second DataGrid automatically updates.
The list, that is bound to first Datagrids DataSource, should be an ObservableCollection. For the dialog you can use the IEditableObject interface in your objects.
Upvotes: 0
Reputation: 60
May be DataView and BindingSource with filters better to use?
DataView view = new DataView(_table);
BindingSource tSource = new BindingSource();
tSource.DataSource = view;
_dataGridView.DataSource = _tSource;
_tSource.Filter = "Value=0";
like this...
To save scrolling use myDataGridView.FirstDisplayedScrollingRowIndex
int scrollIndex = 0;
if (myDataGridView.FirstDisplayedScrollingRowIndex >= 0)
scrollIndex = myDataGridView.FirstDisplayedScrollingRowIndex;
after editing:
if (myDataGridView.Rows.Count > 0)
myDataGridView.FirstDisplayedScrollingRowIndex = scrollIndex;
Hope this helps.
Upvotes: 0