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;


    gridOrder.DataSource = dtItems;

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

Edited by Dani: Formatting fixed

9 Years
Discussion Span
Last Post by ddanbe

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.