我有3个DataGridViews。 使用DataGridView1和DataGridView2,您可以选择行。 按下按钮后,将比较DataGridView1和DataGridView2中的行,并在DataGridView3中设置具有相同值的每一行。
我想要的是当你在DataGridView3中选择一行时,在DataGridView1和DataGridView2中选择了相同的行。
我的代码,也有效:
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++;
}
}
但我想要的是这样的,所以你不必遍历整个网格:
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];
}
我该如何做到这一点?
答案 0 :(得分:0)
扩展我的评论并提供本地(stackoverflow)解决方案:
使用BindingSource.Filter
属性完成过滤。 MSDN上提供的示例是:
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'";
}
为了简化操作,您可能需要创建FilterBuilder
。
编辑:为了避免过滤或问题中提到的上述循环,请使用DataTable
等创建BindingSource
,然后将其链接到您的`DataGridView1:
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);
要查找数据中的相关行,可以使用此行向Selection()
中DataGridView
提供{{1}}。
我希望这会有所帮助。