0

I tried to insert rows on GridView ASP.Net control. I tried this code:

DataTable dt = new DataTable();
DataRow dr = dt.NewRow();
dr["Nr"] = string.Empty;
dt.Rows.Add(dr);



GridView1.DataSource = dt;
GridView1.DataBind();

But i Getting this info:

Column 'Nr' does not belong to table.

Please help me, what i make wrong ? With GridView.

2 Answers 2

1

Try below code. You have to mention column information.

DataTable tbl = new DataTable();
tbl.Columns.Add(new DataColumn("ID", typeof(Int32)));
tbl.Columns.Add(new DataColumn("Name", typeof(string)));
DataRow row = tbl.NewRow();
row["ID"] = i;
row["Name"] = i + ". row";
tbl.Rows.Add(row);
GridView1.DataSource = dt;
GridView1.DataBind();
1
  • I don't know why it don't get from asp formate the name of column.
    – Grzegorz Z
    Dec 9, 2016 at 10:16
0

Define your DataColumn first:

DataColumn dc = new DataColumn("Nr", typeof(String));
dt.Columns.Add(dc);

Then insert value init and then add it in DataTable.

dr["Nr"] = string.Empty;
dt.Rows.Add(dr);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.