Appending datarow to the DataGrid windows control

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How do you add a new row to the datagrid windows control one row at a time without overwriting the existing row

Thanks a lot

Obi
 
DataRow dro = DataSet.Tables[Index].NewRow();
//Set the values accordingly, I'm using Strings and an Int, but you should
use values that match the columns of your datatable.
dro[0] = "FirstValue";
dro[1]= "SecondValue";
dro[2] = SomeIntValue;

DataSet.Tables[Index].Rows.Add(dro);

Obi Oberoi said:
How do you add a new row to the datagrid windows control one row at a time
without overwriting the existing row ?
 
Hi William

Thanks for the suggestion. I've converted the code in vb.net, but I'm getting the "object reference not set error". I know what this error means, but it appears, there is something missing in my code. Can you please take a look at my code below

Tr
Dim dr As DataRo
Dim dt As New DataTabl
Dim ds As New DataSe

dr = ds.Tables(0).NewRo
dr.Item("FirstName") = txtCol1.Tex
dr.Item("LastName") = txtCol1.Tex
ds.Tables(0).Rows.Add(dr

Catch ex As SqlExceptio
ex.Message.ToString(
End Tr

----- William Ryan eMVP wrote: ----

DataRow dro = DataSet.Tables[Index].NewRow()
//Set the values accordingly, I'm using Strings and an Int, but you shoul
use values that match the columns of your datatable
dro[0] = "FirstValue"
dro[1]= "SecondValue"
dro[2] = SomeIntValue

DataSet.Tables[Index].Rows.Add(dro)

Obi Oberoi said:
How do you add a new row to the datagrid windows control one row at a tim
without overwriting the existing row
 
Obi:

If you are using this exact code, there is no Table 0 in the dataset. The
reference was conceptual, but the datatable is new as is the dataset. The
DataTable has no columns so even if you get through the first problem by
adding dt to the dataset, you'll blow up right afterward b/c dt was just
created and no columns were added to it.

Use some real code where you populate a datatable/dataset and give it
another try...you'll be good to go.

Cheers,

Bill
Obi said:
Hi William,

Thanks for the suggestion. I've converted the code in vb.net, but I'm
getting the "object reference not set error". I know what this error means,
but it appears, there is something missing in my code. Can you please take
a look at my code below:
Try
Dim dr As DataRow
Dim dt As New DataTable
Dim ds As New DataSet

dr = ds.Tables(0).NewRow
dr.Item("FirstName") = txtCol1.Text
dr.Item("LastName") = txtCol1.Text
ds.Tables(0).Rows.Add(dr)

Catch ex As SqlException
ex.Message.ToString()
End Try

----- William Ryan eMVP wrote: -----

DataRow dro = DataSet.Tables[Index].NewRow();
//Set the values accordingly, I'm using Strings and an Int, but you should
use values that match the columns of your datatable.
dro[0] = "FirstValue";
dro[1]= "SecondValue";
dro[2] = SomeIntValue;

DataSet.Tables[Index].Rows.Add(dro);

Obi Oberoi said:
How do you add a new row to the datagrid windows control one row at
a time
without overwriting the existing row ?
 
William

I got it. I was fooling around with the data table, datacolumn and here's what I came up with. Infact, I find this new approach easier somehwhat..
What I am doing is that I add values to the DataColumn. I then add the DataColumn to the DataRow and then add the DataRow to the DataTable and then bind the datatable to the DataGrid. Now, however many times, you add the values to the row, they all get appended to the DataGrid. This is way too cool. The thing to remember though, is that you must declare the datacolumn, datarow and datatable objects at the module level. See the code below
Private dr As DataRo
Private dt As New DataTabl
Private dcCol1 As New DataColumn("First Name", GetType(String)
Private dcCol2 As New DataColumn("Last Name", GetType(String)

Private Sub AddRow(

Tr
dr = dt.NewRo
dr(dcCol1) = txtCol1.Tex
dr(dcCol2) = txtCol2.Tex
dt.Rows.Add(dr
grdTest.DataSource = d

Catch ex As SqlExceptio
ex.Message.ToString(
End Tr

End Su

Thanks

Obi
 
Back
Top