c# adding row to datatable which has an auto increment column

三世轮回 提交于 2019-12-04 07:35:43
Raju Padhara

try this.

DataColumn column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.AutoIncrement = true;
column.AutoIncrementSeed = 1;
column.AutoIncrementStep = 1;

// Add the column to a new DataTable.
DataTable table = new DataTable("table");
table.Columns.Add(column);

DataRow oRow = table.NewRow();
table.Rows.Add(oRow);

Open the designer of the dataset xsd file and set the AutoIncrement, AutoIncrementSeed and AutoIncrementStep property of the column A in datatable for an existing column.

khalid khan

Try one of the these two:

  1. Set field values:

    row.A = null;
    row.B = 1;
    row.C = 3;
    
  2. Add row to DataTable:

    dtA.Rows.Add(null,1,2);
    

They are both the same just try any of them and it should get you going. Also remember that whenever you want to make a column auto-increment in DataTable then you have to insert null into it.

The way that I do it is

public void AppendtodtA(int num1, doubledouble1, string string1)
{
    object[] RowArray = new object[] {null,(int)num1, (double) doubledouble1, (string) string1}
    DataRow CurrentRow = dtA.NewRow();
    CurrentRow.ItemArray = RowArray;
    dtA.Rows.Add(CurrentRow);
}

use ItemArray property and leave a null in the place of the autoincremented column

AppendtodtA(MyNumber,MyDouble,MyString);

Then just call the method with your variables.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!