I have 3 DataGridViews. With DataGridView1 and DataGridView2 you can select rows. After pushing a button the rows from DataGridView1 and DataGridView2 are compared and every row that has the same values are set in DataGridView3.
What I want is when you select a row in DataGridView3, the same rows are selected in DataGridView1 and DataGridView2.
The code I have, and also works is:
private int ShowSelected(int selectedId, Boolean sBool)
{
DataTable dt = DataGridView1.DataSource;
if(!sBool)
currentGrid = DataGridView1;
int indexCounter = 0;
foreach (DataRow dr in dt.Rows)
{
int cellIdDgv = Convert.ToInt32(dr["cellId"]);
if (selectedId == cellIdDgv)
{
if (sBool)
{
DataGridView1.Rows[indexCounter].Selected = true;
DataGridView1.FirstDisplayedCell = DataGridView1.Rows[indexCounter].Cells[0];
}
else
{
DataGridView2.Rows[indexCounter].Selected = true;
DataGridView2.FirstDisplayedCell = DataGridView2.Rows[indexCounter].Cells[0];
}
}
indexCounter++;
}
}
But what I want is something like this, so you don't have to loop through the whole Grid:
string selection = "cellId = " + selectedId;
DataRow[] drResult = dt.Select(selection);
int rowId = drResult.RowId;
if (sBool)
{
DataGridView1.Rows[rowId].Selected = true;
DataGridView1.FirstDisplayedCell = DataGridView1.Rows[rowId].Cells[0];
}
else
{
DataGridView2.Rows[rowId].Selected = true;
DataGridView2.FirstDisplayedCell = DataGridView2.Rows[rowId].Cells[0];
}
How can I make this work?
To extend on my comment and to provide a local (stackoverflow) solution:
Filtering is accomplished using the BindingSource.Filter
property. The example provided on MSDN is:
private void PopulateDataViewAndFilter()
{
DataSet set1 = new DataSet();
// Some xml data to populate the DataSet with.
string musicXml =
"<?xml version='1.0' encoding='UTF-8'?>" +
"<music>" +
"<recording><artist>Coldplay</artist><cd>X&Y</cd></recording>" +
"<recording><artist>Dave Matthews</artist><cd>Under the Table and Dreaming</cd></recording>" +
"<recording><artist>Dave Matthews</artist><cd>Live at Red Rocks</cd></recording>" +
"<recording><artist>Natalie Merchant</artist><cd>Tigerlily</cd></recording>" +
"<recording><artist>U2</artist><cd>How to Dismantle an Atomic Bomb</cd></recording>" +
"</music>";
// Read the xml.
StringReader reader = new StringReader(musicXml);
set1.ReadXml(reader);
// Get a DataView of the table contained in the dataset.
DataTableCollection tables = set1.Tables;
DataView view1 = new DataView(tables[0]);
// Create a DataGridView control and add it to the form.
DataGridView datagridview1 = new DataGridView();
datagridview1.AutoGenerateColumns = true;
this.Controls.Add(datagridview1);
// Create a BindingSource and set its DataSource property to
// the DataView.
BindingSource source1 = new BindingSource();
source1.DataSource = view1;
// Set the data source for the DataGridView.
datagridview1.DataSource = source1;
//The Filter string can include Boolean expressions.
source1.Filter = "artist = 'Dave Matthews' OR cd = 'Tigerlily'";
}
To make it easier, you may want to create a FilterBuilder
.
Edit: To avoid filtering or the above looping mentioned in your question, create a DataTable
using a BindingSource
etc. which is linked to your `DataGridView1 then:
DataTable DT; // Obtained from the relevent DGV.
DataView view = new DataView(DT);
DataView thisDV = new DataView();
// Then you can use...
thisDV.Find(thisOrThat);
thisDV.FindRows(thisOrThat);
To find the relevant rows in the data, this can then be used to feed a Selection()
in you DataGridView
s.
I hope this helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.