简体   繁体   中英

Set More than one Initial row in datatable

The below function set only one Initial row.

How can i make 10 initial rows for example.

Any Suggestions?

Function

private void SetInitialRow()
{
    DataTable dt = new DataTable();
    DataRow dr = null;
    dt.Columns.Add(new DataColumn("Sr.No", typeof(string)));
    dt.Columns.Add(new DataColumn("Column1", typeof(string)));
    dt.Columns.Add(new DataColumn("Column2", typeof(string)));
    dt.Columns.Add(new DataColumn("Column3", typeof(string)));
    dt.Columns.Add(new DataColumn("Column4", typeof(string)));

    dr = dt.NewRow();


    dr["Sr.No"] = 1;
    dr["Column1"] = string.Empty;
    dr["Column2"] = string.Empty;
    dr["Column3"] = string.Empty;
    dr["Column4"] = string.Empty;

    dt.Rows.Add(dr);
    //Store the DataTable in ViewState
    ViewState["CurrentTable"] = dt;

    griditem.DataSource = dt;
    griditem.DataBind();
}

this may be?

for (int i = 0; i < 10; i++)
{
    DataRow dr = dt.NewRow();

    dr["Sr.No"] = i + 1;
    dr["Column1"] = string.Empty;
    dr["Column2"] = string.Empty;
    dr["Column3"] = string.Empty;
    dr["Column4"] = string.Empty;

    dt.Rows.Add(dr);
}

You could create your new row in a for loop. Something along the lines of the following:

for (var i = 0; i < 10; i++)
{
    dr = dt.NewRow();

    dr["Sr.No"] = i + 1;
    dr["Column1"] = string.Empty;
    dr["Column2"] = string.Empty;
    dr["Column3"] = string.Empty;
    dr["Column4"] = string.Empty;

    dt.Rows.Add(dr);
}

This will then give you 10 rows in your dt object, and assign the Sr.No value to the index + 1 of the loop, ie 1,2...10

try this..

    private void SetInitialRow()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("Sr.No", typeof(string)));
        dt.Columns.Add(new DataColumn("Column1", typeof(string)));
        dt.Columns.Add(new DataColumn("Column2", typeof(string)));
        dt.Columns.Add(new DataColumn("Column3", typeof(string)));
        dt.Columns.Add(new DataColumn("Column4", typeof(string)));

        for (int i = 0; i < 10; i++)
        {
            dr = dt.NewRow();
            dr["Sr.No"] = (i +1);
            dr["Column1"] = string.Empty;
            dr["Column2"] = string.Empty;
            dr["Column3"] = string.Empty;
            dr["Column4"] = string.Empty;
            dt.Rows.Add(dr);
        }

        //Store the DataTable in ViewState
        ViewState["CurrentTable"] = dt;

        griditem.DataSource = dt;
        griditem.DataBind();
    }

this could also be the option

  DataTable dt = new DataTable();
  DataRow dr = null;
  dt.Columns.Add(new DataColumn { AutoIncrement=true,AutoIncrementSeed=1,AutoIncrementStep=1,ColumnName="SrNo",DataType=typeof(int)});
  dt.Columns.Add(new DataColumn { ColumnName = "Column1", DataType = typeof(string) });
  dt.Columns.Add(new DataColumn { ColumnName = "Column2", DataType = typeof(string) });
  dt.Columns.Add(new DataColumn { ColumnName = "Column3", DataType = typeof(string) });
  dt.Columns.Add(new DataColumn { ColumnName = "Column4", DataType = typeof(string) });
  for(int i=0;i<10;i++)
    dt.Rows.Add(dt.NewRow());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM