Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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


EDIT:

I have change the Code... what is the problem?
C#
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);
}
Posted
Updated 7-Oct-11 1:37am
v4
Comments
Bala Selvanayagam 5-Oct-11 8:51am    
what is variable x ?

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
 
Share this answer
 
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.


C#
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...
 
Share this answer
 
Comments
Alexander Eirich 6-Oct-11 2:11am    
Sorry, this not helps me...

i have a Class DataSet1 that includes a Class DataTable2, and I have a DataTable2TableAdapter.
With a dataTable2bindingSource the Table was bind to the DataGridView.

I should like copy one or more Rows in the DataGrid to dublicating DataRows, but i get with all ways a Exception...

Thanks for your help.
the headless nick 7-Oct-11 4:20am    
Can You show us your code. .???
Alexander Eirich 7-Oct-11 6:59am    
//Initializing:
dTable = new DataTable();
dataAdapter.Fill(this.dTable);
dView = new DataView(dTable);
dataGridView1.DataSource = dView;

private void tsBCopy_Click(object sender, EventArgs e)
{
int copyCount = Convert.ToInt32(tbCopyCount.Text);
DataRowView[] newRows = new DataRowView[copyCount * dataGridView1.SelectedRows.Count];
for (int i = 0; i < dataGridView1.SelectedRows.Count; i++ )
{
newRows[i] = (DataRowView)dataGridView1.SelectedRows[i].DataBoundItem;
}
// And now I will change the primary Key in the newRows and Add it to the DataTable
}
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());
 
Share this answer
 
Comments
Alexander Eirich 6-Oct-11 6:41am    
the Function Copy() is not defined if i write this line.
BobJanova 6-Oct-11 16:42pm    
Huh, that's odd. I guess I was thinking of the one on DataTable. You may have to copy the row manually (field by field).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900