如何在c#中将数据从一个datagridview提取到另一个datagridview [英] how can i fetch data from one datagridview to another datagridview in c#
问题描述
您好,
我正在使用Windows应用程序。
我想要获取数据从一个datagridview到另一个datagridview
我已将数据从数据库带到datagridview1。
当我双击或按Enter键时,我想要datagridview1的特定单元格应该显示第二个datagridview中的值,即datagridview2。
注意:我已经动态创建了第一个datagridview,即datagridview1
并从数据库中显示该值。
我也想知道如何动态创建事件。
谢谢
Hello,
I'm working on windows application.
I want to fetch data from one datagridview to another datagridview
I have brought data in from database to datagridview1.
I want that when I doubleclick or press enter on particular cell of datagridview1 it should display the values in second datagridview i.e datagridview2.
Note: I have dynamically created the first datagridview i.e datagridview1
and display the value in that from database.
I also want to know how to create the event dynamically.
thanks
推荐答案
我认为这不是一个网络应用程序,因为如果是的话,你已经告诉我们了。只需从数据源中删除该项并将其添加到列表中,该列表将是第二个网格的数据源,然后重新绑定两个数据源。
I assume this is not a web app, because if it was, you'd have told us. Just remove the item from your datasource and add it to a list, which will be the datasource for the second grid, then rebind both data sources.
您好Sagar55,
DataTable dtOutput = new DataTable(); //在全局中分配
在DataGridView1 CellBeginEdit事件中编写以下代码
Hi Sagar55,
DataTable dtOutput=new DataTable(); // Assign in Global
In your DataGridView1 CellBeginEdit Event write the following code
//Its Working in Cell Double Click
void dataGridView1_CellBeginEdit(object sender,DataGridViewCellCancelEventArgs e)
{
DataTable dtGrid1=(DataTable)DataGridView1.DataSource;
string sId=DataGridView1.Rows[e.RowIndex].Cells["Id"].Value.ToString();
//Here 'Id' is Filter Column (Assume)
DataRow[] drFilter=dtGrid1.Select("Id="'+sId+'"");
DataTable dtData=new DataTable();
dtData=dtGrid1.Clone();
foreach(DataRow dr in drFilter)
{
dtData.ImportRow(dr);
}
if(dtOutput.Columns.Count==0)
{
dtOutput=dtGrid1.Clone();
}
dtOutput.Merge(dtData);
DataGridView2.DataSource=dtOutput.Copy();
}
我希望这段代码对你有用。
干杯:)
I hope this code useful to you.
Cheers :)
在创建datagridview1时为其分配一个事件,如
while creating datagridview1 assign a event to it like
datagridview1.CellDoubleClick+=new DataGridViewCellEventHandler(datagridview1_CellDoubleClick);
创建一个方法,如:
create one method like :
private void datagridview1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
string cellValue= dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
//create datasource base on your cellvalue and assign that datasource to datagridview2
}
这篇关于如何在c#中将数据从一个datagridview提取到另一个datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!