Datagrid Help

  • Thread starter Thread starter Jamie Sutherland
  • Start date Start date
J

Jamie Sutherland

Hi,
I have a windows form using vb.net that has 5 text boxes, 1 button and a
datagrid.
txtProdID
txtDesc
txtQty
txtUnit
txtTotal
dgProducts
btnAddtoDataGrid

When the user has entered the info into the textboxes and then click
btnAddtoDataGrid then I want the data added to the datagrid.
Question - How do I do this? - I am not sure on creating the rows in the
datagrid.

Thanks
Jamie
 
You will need to create the data in an object that is compatible with
DataGrid.DataSource such as DataTable or DataSet.

Here is a quick example of a DataTable:

DataTable dt = new DataTable("Products");
dt.Columns.Add(new DataColumn("txtProdID", typeof(string));
dt.Columns.Add(new DataColumn("txtDesc", typeof(string));
dt.Columns.Add(new DataColumn("txtQty", typeof(int));
dt.Columns.Add(new DataColumn("txtUnit", typeof(double));
dt.Columns.Add(new DataColumn("txtTotal", typeof(double));

DataRow dr = dt.NewRow();
dr["txtProdID"] = txtProdID.Text;
dr["txtDesc"] = txtDesc.Text;
dr["txtQty"] = System.Convert.ToInt32(txtQty.Text); // this could throw a
NumberFormat exception

....

dt.Rows.Add(dr);

then go back to DataRow and add another row when the button gets pressed
again.

when finished setting up your table:

datagrid.DataSource = dt;

Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top