I am a beginner, so don't be amused by my questions. I think
if (ds.Tables[0].Rows.Count > 0)
is used to check if the dataset is empty. But what exactly [0]
signify in this case? Can you explain this statement in a little more detail?
And this one too.. ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
It gives you access to the first table of the DataSet
. A DataSet
holds an array of DataTables
, and it can have 0, 1 or many of these DataTables
. You access them just like any other array - by indexing into them.
If there were 2 DataTables
in this DataSet
, you could access the first one by using ds.Tables[0]
, and the second one by ds.Tables[1]
The ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
statement is adding a new row to the first DataTable
in the DataSet
. By calling ds.Tables[0].NewRow()
, you are creating a new row that is associated to the first DataTable
in the array.
Here ds
is an instance of DataSet. A DataSet may contain multiple instances of Table.
ds.Tables[0]
is accessing the first table in the Tables
collection. And ds.Tables[0].Rows.Count
is counting the rows on that first table.
Rows accesses the DataRow collection. The Add method creates one more row; however, you need to pass an instance of DataRow as the parameter. This DataRow object must have the same structure (columns) that the table, for this reason, you use the same table to create a new DataRoq: ds.Tables[0].NewRow()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With