问题描述
您好,
我正在使用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
推荐答案
//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.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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!