Hello,

I'm not able to add a DataRow to a DataTable, believe it or not. I'm sure it has something to do with the scoping of the DataTable object, but I don't know what. When I set up the code in one method procedurally, everything works fine.

I declare the table at the class level:

public partial class OrderCreate : System.Web.UI.Page 
{

// Fill this table with items as you're building the order.
public DataTable dtItems = new DataTable();
DataColumn Col1=new DataColumn("Product_Number",typeof(System.String));
DataColumn Col2=new DataColumn("Name",typeof(System.String));
DataColumn Col3=new DataColumn("Description",typeof(System.String));
DataColumn Col4=new DataColumn("Price",typeof(System.Decimal));

I then attempt to add a row to it when I click on a GridView:

    DataRow row = null;
    row = dtItems.NewRow();

    row[0] = gridProducts.Rows[gridProducts.SelectedIndex].Cells[2].Text;
    row[1] = gridProducts.Rows[gridProducts.SelectedIndex].Cells[3].Text;
    row[2] = gridProducts.Rows[gridProducts.SelectedIndex].Cells[4].Text;
    row[3] = gridProducts.Rows[gridProducts.SelectedIndex].Cells[5].Text;

    dtItems.Rows.Add(row);

    gridOrder.DataSource = dtItems;
    gridOrder.DataBind();

It gets stuck at one record and never adds more. What am I missing?

Edited 3 Years Ago by Dani: Formatting fixed

A DataTable and a GridView are two different things.
In your code you are adding a row to the DataTable, which is something that happens in memory somewhere. You show me no code to change the GridView control you see on the screen.

This article has been dead for over six months. Start a new discussion instead.