具有DataSet源的DataGridView更改数据 [英] DataGridView with a DataSet Source change Data

查看:137
本文介绍了具有DataSet源的DataGridView更改数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我有一个连接到odbc服务器的DataSet.
我在数据集中有一个DataTable,可以将SQL-Server中的数据填充到Datagridview中.

我将在Datagridview中复制选定的行,并更改行.

如果运行以下命令,则会收到ArgumentException:

Hello,

I have a DataSet with a connection to an odbc-server.
I have in the dataset a DataTable, who fill the data from SQL-Server in a Datagridview.

I will copy selected Rows in the Datagridview with changes in the Rows.

I get a ArgumentException if I run :

dataSet1.DataTable2.Rows.Add(dataGridView1.SelectedRows[x]);



我的错是什么?

在此先感谢




我更改了密码...是什么问题?



What my mistake?

Thanks in advance




I have change the Code... what is the problem?

int copyCount = Convert.ToInt32(tbCopyCount.Text);
int maxDataset = (int)getMaxDataset();
DataGridViewRow[] newRows = new DataGridViewRow[copyCount * dataGridView1.SelectedRows.Count];

for (int i = 0; i < dataGridView1.SelectedRows.Count; i++ )
{
  newRows[i] = (DataGridViewRow)dataGridView1.SelectedRows[i].Clone();
  newRows[i].Cells[0].Value = ++maxDataset;
  dTable.Rows.Add(newRows[i].DataBoundItem as DataRow);
}

推荐答案

亚历山大,您好,

您正在分配不兼容(不可广播)的对象. DataRow(位于DataTable2.Rows中)与DataGridViewRow(位于dataGridView1.SelectedRows中)没有任何共同之处.

您必须通过创建新行(仅当您已继承自DataRow时)并将SelectedRows数据传递给表来将数据添加到表中.可以将此行添加到表中.或者,您可以直接在Add-Statement中传递数据(通过对象数组).

干杯
于尔根(Jürgen)
Hi Alexander,

you''re assigning incompatible (non-castable) objects. A DataRow (which is in DataTable2.Rows) has nothing in common with a DataGridViewRow (which is in dataGridView1.SelectedRows).

You have to add the data to the table by creating a new row (only if you have inherited from DataRow) and passing the SelectedRows data to it. This Row can be added to the table. Or you pass the data directly in the Add-Statement (via an array of objects).

Cheers
Jürgen


您不能使用像这样的数据表...

使用数据行之类的东西来拾取数据并将其逐行添加到临时表中,然后可以将数据从该数据表复制到另一个数据集.


you can''t use datatable like that...

use somthing like datarow to pick data and add it row by row in a temprory table then you may copy data from that datatable to anothor dataset.


DataTable dt = new DataTable("nicktable");
dt.Columns.Add("col1", Type.GetType("System.Int32"));
dt.Columns.Add("col2", Type.GetType("System.String"));


SqlDataAdapter adp = new SqlDataAdapter("select * from tbemp", ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
DataSet ds = new DataSet();
adp.Fill(ds);
DataRow dr = dt.NewRow();
dr[0] = ds.Tables[0].Rows[0];
dt.ImportRow(dr);
//DataSet dss = ds.Copy(); may use this also for Dataset 2 dataset



DataSet dss = new DataSet();
dss.Tables.Add(dt);
/// I think this is what you are looking for....



希望这对您有帮助...



hope this helps...


这里有两个错误:首先,您尝试复制网格行,而不是基础行,其次,您需要克隆该行在将其添加为同一行对象之前,不允许将其包含在多个表中.

我想你要
There''s two mistakes here: first, you are trying to copy the grid row, not the underlying row, and second, you need to clone the row before adding it as the same row object is not allowed to be in more than one table.

I think you want
dataSet1.DataTable2.Rows.Add(((DataRow)dataGridView1.SelectedRows[x].DataBoundItem).Copy());


这篇关于具有DataSet源的DataGridView更改数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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