Search code examples
c#wpfdatatablewpfdatagrid

How to error free"ColumnName Already Belongs to datatable "?


Hai am write the code below in c#,it always error as column already belongs to this datatable.i dnt knw how to error free.the below code is am used.

private void Send_Click(object sender, RoutedEventArgs e)
{
 System.Data.DataRowView selectedFile = (System.Data.DataRowView)dataGrid1.SelectedItem;
 string studid = Convert.ToString(selectedFile.Row.ItemArray[3]);
 DataRow row = tabl.NewRow();
 tabl.Columns.Add("StudyUID", typeof(string));
 tabl.Columns.Add("Patient ID");
 tabl.Columns.Add("Patient Name");
 row["StudyUID"] = selectedFile.Row.ItemArray[3];
 row["Patient ID"] = selectedFile.Row.ItemArray[1];
 row["Patient Name"] = selectedFile.Row.ItemArray[2];
 sendstudy(this.SUID, row);
}

I need how to avoid the error "column already belongs to this datatable"

thanks in Advance


Solution

  • You don't need to add columns for every new row - once the columns are defined, they'll be included in every new row. Just delete these lines:

     tabl.Columns.Add("StudyUID", typeof(string));
     tabl.Columns.Add("Patient ID");
     tabl.Columns.Add("Patient Name");