Here is how to add columns to a datatable. You can also set the Ordinal Position when you add it, to rearrange to order of the columns.
If the datatable has been attached to a datagridview and then you add the columns, you need to set the datagridview.datasource=null and the refresh the grid. Then add the columns and reattach the datatable to the gridview and refresh it.
If you do not disconnect and reconnect, the new fields will show up, but they will appear at the end of the display.
[code]
public void AddCertColumns(DataTable vdt)
{
if (!vdt.Columns.Contains("Certificate"))
{
vdt.Columns.Add("Certificate", Type.GetType("System.String")).SetOrdinal(0);
}
if (!vdt.Columns.Contains("BatchNum"))
{
vdt.Columns.Add("BatchNum", Type.GetType("System.String")).SetOrdinal(1);
}
if (!vdt.Columns.Contains("CancelEntryDate"))
{
vdt.Columns.Add("CancelEntryDate", Type.GetType("System.String")).SetOrdinal(2);
}
}
[/code]
No comments:
Post a Comment