c# winform 如何从datagridview1中获得一行数据然后赋值到datagridview2中相应cell中?

c# winform 如何从datagridview1中获得一行数据然后赋值到datagridview2中相应cell中? 如题
郁闷了.......

第1个回答  2011-12-05
this.bcDataGridView1["FNumber", this.bcDataGridView1.CurrentRow.RowIndex].Value;这是取选中行的某一列值

你很多列就用for循环赋值啊,
还有 你这样 int.Parse( dataGridView2.CurrentRow.Cells[0].Value.ToString()); 容易报空值错误,你就直接
this.bcDataGridView2["FNumber", this.bcDataGridView1.CurrentRow.RowIndex].Value=this.bcDataGridView1["FNumber", this.bcDataGridView1.CurrentRow.RowIndex].Value追问

报错。不对

追答

你不会直接贴我的代码吧,你的datagridview跟我不一样,列明也不一样的,这些都要改的,

追问

我都改成自己的了 但还是不行 主要是[]里面报错

追答

你把你的代码贴给我看看

追问

类似这样的
string name = dataGridView2.CurrentRow.Cells[1].Value.ToString();
dataGridView2.CurrentRow.Cells[1].Value = name;
string ShangpinName = dataGridView2.CurrentRow.Cells[2].Value.ToString();
dataGridView2.CurrentRow.Cells[1].Value = ShangpinName;

追答

怎么都是datagridview2啊,还有你是放在什么事件里的,要什么的效果,是选中1里面数据就往2里面存数据么

追问

你有百度HI没有?
我贴错了
我这样写得
int id =int.Parse( dataGridView2.CurrentRow.Cells[0].Value.ToString());
dataGridView1.CurrentRow.Cells[0].Value = id;
string name = dataGridView2.CurrentRow.Cells[0].Value.ToString();
dataGridView1.CurrentRow.Cells[1].Value = name;
放在button点击事件里。

追答

for (int i=0;i<dataGridView1.Columns.Count;i++)
{
dataGridView1.CurrentRow.Cells[i].Value =dataGridView2.CurrentRow.Cells[i].Value;
}
这么写不可以么

本回答被提问者采纳
第2个回答  2011-12-05
this.dataGridView1.Rows[N].Cells[0].Value+ this.dataGridView1.Rows[N].Cells[1].Value。。。。。。。
获得你需要的一行数据,然后拼接成字符串,赋值给datagridview2中相应cell追问

我这样写得
int id =int.Parse( dataGridView2.CurrentRow.Cells[0].Value.ToString());
dataGridView1.CurrentRow.Cells[0].Value = id;
string name = dataGridView2.CurrentRow.Cells[0].Value.ToString();
dataGridView1.CurrentRow.Cells[1].Value = name;
第二行和第四行提示:未将对象引用设置到对象的实例。 如何修改?

追答

你是不是写反了。你不是要把dataGridView1相应的值赋给dataGridView2吗?

追问

没写反,我的提问问反了。要把2中得赋值给1.

追答

int id =int.Parse( dataGridView1.Rows[N]..Cells[0].Value.ToString());
id = dataGridView2.CurrentRow.Cells[0].Value ;
string name = dataGridView1.Rows[N].Cells[0].Value.ToString();
name= dataGridView2.CurrentRow.Cells[0].Value ;

追问

明显这样写不对!!!

第3个回答  2011-12-05
GridView1.Rows[1].Cells[0].Text=GridView1.Rows[0].Cells[0].Text;
GridView1.Rows[1].Cells[1].Text=GridView1.Rows[0].Cells[1].Text;追问

有text的属性么 大哥....

追答

int id =int.Parse( dataGridView2.CurrentRow.Cells[0].Value.ToString());
dataGridView1.CurrentRow.Cells[0].Value = id;
string name = dataGridView2.CurrentRow.Cells[1].Value.ToString();
dataGridView1.CurrentRow.Cells[1].Value = name;

第4个回答  2011-12-05
DataRowView dr = datagridview1.CurrentCell.OwningRow.DataBoundItem as DataRowView;
datagridview2.Rows.Add( dr);
第5个回答  2011-12-05
dataGridView2.Rows[i].Cells[2].Value

或者

dataGridView2.SelectedRows[i].Cells[0].Value追问

我这样写得
int id =int.Parse( dataGridView2.CurrentRow.Cells[0].Value.ToString());
dataGridView1.CurrentRow.Cells[0].Value = id;
string name = dataGridView2.CurrentRow.Cells[0].Value.ToString();
dataGridView1.CurrentRow.Cells[1].Value = name;
第二行和第四行提示:未将对象引用设置到对象的实例。 如何修改?

相似回答