获取datagridview单元格选择一个到另一个c# [英] get datagridview cell select one to another c#
本文介绍了获取datagridview单元格选择一个到另一个c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在两个datagridview中有相同的数据。我在c#
I have same data in two datagridview. I select one datagridview cells its affected in another in c#
推荐答案
中选择一个受其影响的数据网格视图单元格。只需使用其中一个网格的单元格点击事件
您可以根据您的输出添加或进行更改:
考虑到我有两个具有相同行和列的datagridView并且我动态添加行和列两个网格。
Just use the Cell Click event of one of the grid
You can add or make changes according to your output:
Consider that i have Two datagridView with same rows and columns and im dynamically adding rows and columnsto both the grids.
private void btnAdd_Click(object sender, EventArgs e)
{
dataGridView1.ColumnCount = 3;
dataGridView1.Columns[0].Name = "Product ID";
dataGridView1.Columns[1].Name = "Product Name";
dataGridView1.Columns[2].Name = "Product Price";
string[] row = new string[] { "1", "Product 1", "1000" };
dataGridView1.Rows.Add(row);
row = new string[] { "2", "Product 2", "2000" };
dataGridView1.Rows.Add(row);
row = new string[] { "3", "Product 3", "3000" };
dataGridView1.Rows.Add(row);
row = new string[] { "4", "Product 4", "4000" };
dataGridView1.Rows.Add(row);
dataGridView2.ColumnCount = 3;
dataGridView2.Columns[0].Name = "Product ID";
dataGridView2.Columns[1].Name = "Product Name";
dataGridView2.Columns[2].Name = "Product Price";
string[] row1 = new string[] { "1", "Product 1", "1000" };
dataGridView2.Rows.Add(row);
row1 = new string[] { "2", "Product 2", "2000" };
dataGridView2.Rows.Add(row);
row1 = new string[] { "3", "Product 3", "3000" };
dataGridView2.Rows.Add(row);
row1 = new string[] { "4", "Product 4", "4000" };
dataGridView2.Rows.Add(row1);
}
其次我有我的第一个DataGrid的CellClick事件:
Secondly I have the CellClick Event of My first DataGrid :
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
int rowindex = dataGridView1.Rows[e.RowIndex].Index;
int columnindex = dataGridView1.Columns[e.ColumnIndex].Index;
dataGridView2.Rows[rowindex].Cells[columnindex].Selected = true;
}
因此,当我点击第一个网格的一个内容时,第二个网格中的相同单元格被选中网格。
您可以在dataGrid的CellClick事件中添加代码。
So when i click one content of my first grid the same cell gets selected in the second grid .
You can add your code in CellClick Event of dataGrid .
这篇关于获取datagridview单元格选择一个到另一个c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文