The following code is used to populate a DGV:
private void frmSwitch_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'newCityCollectionDataSet.PropertyInformation' table. You can move, or remove it, as needed.
this.propertyInformationTableAdapter.Fill(this.newCityCollectionDataSet.PropertyInformation);
// TODO: This line of code loads data into the 'newCityCollectionDataSet.ClientTable' table. You can move, or remove it, as needed.
this.clientTableTableAdapter.Fill(this.newCityCollectionDataSet.ClientTable);
}
This code allows me to pass the necessary information to the “summary form”:
private void propertyInformationDataGridView_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
System.Data.DataRowView SelectedRowView;
newCityCollectionDataSet.PropertyInformationRow SelectedRow;
SelectedRowView = (System.Data.DataRowView)propertyInformationBindingSource.Current;
SelectedRow = (newCityCollectionDataSet.PropertyInformationRow)SelectedRowView.Row;
frmSummary SummaryForm = new frmSummary(this);
SummaryForm.LoadCaseNumberKey(SelectedRow.CaseNumberKey, true, null);
SummaryForm.LoadBRTNumberKey(SelectedRow.BRTNumber, null);
SummaryForm.Show();
}
What I am looking to do is pass the SelectedRow and add 1 to go to the next row if the current SelectedRow is no longer valid (for instance when FileFinishedCheckBox is checked on the “summary form”). I also want the same thing to happen anytime a checkbox is checked on the DataGridview so people do not have to scroll back to the file they are working on.
The code that performs the refresh whenever needed is as follows:
public void PerformRefresh()
{
this.propertyInformationBindingSource.EndEdit();
this.propertyInformationTableAdapter.Fill(this.newCityCollectionDataSet.PropertyInformation);
this.propertyInformationDataGridView.Refresh();
}
Any help would be great.
This question seems to be in two parts:
There are many different ways of achieving both tasks so I’m just going to give you two that will work. The first (for windows forms) is the simplest, while the second (for changing the selected row) is in my opinion the correct method.
Communication between windows forms
The most straightforward way to communicate between two windows forms is pass a reference to one form into the other form.
So say you have Form1 which opens Form2, you could do something like this:
So in your example you would add a method to the parent form which takes as its parameter the unique value for the row selected in dgv3 to show in gdv1. In the method (which is a member of the parentForm you put the centering code which I will show below).
Other ways of doing this include passing a delegate to the child form which is the method to center the datagridview. This has the advantage that you are no longer tied down to always passing in Form1 and can even provide different actions in resonse to the checkbox but is slightly more complicated to implement.
Centering on a selected record in a DataGridView
My preferred way of doing this is to use a bindingsource to provide the datasource for the grid. You can also directly access the grid position using the CurrentCell property but with the bindingsource you get a bit more bang for your buck.
In the code below we have a form which creates a BindingSource, sets its datasource to a BindingList of type MyBindingList and then sets the binding source as the datasource of a datagridview.
The objects within the BindingList have a unique property “PrimaryKey” allowing us to find them.
Then I show the centering code which is actually very simple.
First we get the index in the binding source of the desired you by calling the
Find()method of the binding source.Second we change the binding sources position (this also updates the datagridview display).
Finally we change the FirstDisplayedScrollingRowIndex of the datagridview so that the selected row is not at the very top or bottom of the grid (you will want to add a check to ensure this is a valid index if you use this line).
Now the last this to note is that bindinglist out of the box does not support the
Find()method of the bindingsource. This is why I use my custom MyBindingList. Code to implement this can be found here.Essentially you need a class like the following: