繁体   English   中英

在第三个DataGridView中选择时,如何在2个DataGridViews中选择一行?

[英]How can I select a row in 2 DataGridViews when selected in a third DataGridView?

我有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++;
    }
}

但是我想要的是这样的东西,因此您不必遍历整个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];
}

我该如何进行这项工作?

要扩展我的评论并提供本地(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&amp;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 ,您可能需要创建一个FilterBuilder

编辑:为避免过滤或问题中提到的上述循环,请使用BindingSource等创建一个DataTable并将其链接到`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);

为了找到数据中的相关行,可以将其用于在DataGridView提供Selection()

我希望这有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM