Hii ...我想通过在两个表中都存在ID时单击anather数据网格视图行来选择数据网格视图行,那么我该怎么办? [英] Hii... I want to select data grid view row by clicking on anather data grid view row when ID is present in both tables so what can I do ?

查看:61
本文介绍了Hii ...我想通过在两个表中都存在ID时单击anather数据网格视图行来选择数据网格视图行,那么我该怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在同一页面上有两个gridview,当我选择第一个gridview行时,我想选择第二个gridview行,当两个表中都有相同的ID



我尝试过:



if(e.RowIndex< 0)

{

返回;

}

int index = e.RowIndex;

dataGridView2.Rows [index] .Selected = true;





但在这种情况下,当我点击第一个gridview时,在第二个GridView中逐个选择所有行,我想只选择一个用于特定的第二个gridview中的id。

i have two gridview on same page when i select 1st gridview row that time i want to select 2nd gridview row when Same ID present in both table

What I have tried:

if (e.RowIndex < 0)
{
return;
}
int index = e.RowIndex;
dataGridView2.Rows[index].Selected = true;


but in this case one by one all rows is selected in 2nd GridView when i clicking on 1st gridview, i want only one is selected for perticular id in 2nd gridview.

推荐答案

我在这里假设Winforms ...当第一个网格的行选择更改事件触发时,在第二个网格中找到行索引并使用
I am assuming Winforms here... When the row selection changed event fires for the first grid, find the row index in the second grid and use
dataGrid.Rows[index].Selected = true;

这是一种紧凑的做法你想要什么:

Here is a compact way of doing what you want:

DataGrid.Rows.OfType<DataGridViewRow>().Where(x => (int)x.Cells["Id"].Value == pId).ToArray<DataGridViewRow>()[0].Selected = true;


参考此示例



refer this example

private void Form1_Load(object sender, EventArgs e)
       {
           DataTable dt = new DataTable();
           dt.Columns.Add("Id");
           dt.Columns.Add("Name");
           dt.Rows.Add(1, "one");
           dt.Rows.Add(2, "two");
           dt.Rows.Add(3, "three");

           dataGridView1.DataSource = dt;
           dataGridView2.DataSource = dt;
           dataGridView1.RowHeaderMouseClick +=dataGridView1_RowHeaderMouseClick;

       }

       private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
       {
           string id = Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells["Id"].Value);
           foreach (DataGridViewRow row in dataGridView2.Rows)
           {
               if (row.Cells["Id"].Value.ToString() == id)
               {
                   row.Selected = true;
                   break;
               }

           }
       }





注意:我有使用 RowHeaderMouseClick 事件,您将使用相关的。



note: I have used RowHeaderMouseClick event, you shall use the relevant one.


您好,您可以从选定的网格视图中获取Id(A)(A并找到与另一个网格视图相同的id(A)记录(B)

此外,您还需要确保(A和B)ID应该相同。
Hi,You can get Id(A) from selected grid view(A) and find same id(A) record with another grid view(B)
Also you need to ensure both(A and B) ID should be same.


这篇关于Hii ...我想通过在两个表中都存在ID时单击anather数据网格视图行来选择数据网格视图行,那么我该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆