newbie: How to get rows in specific column?

  • Thread starter Thread starter deko
  • Start date Start date
D

deko

How do I reference the rows in a particular column in a DataTable?

tblMyDataTable

row# colA colB pkID fkID
1 str1 srt2 23 19
2 str3 [null] 24 19

I understand I can get a _horizontal_ row of values with DataRowCollection.
For example, I could get the values in row2 like this:

DataRowCollection drc = tblMyTable.Rows[2];

and I could reference the values like this:

if (drc[1] == null);
{
[code here]
}

But how do I get a _vertical_ column of rows?

In the example above, if I used a foreach loop to loop through each row, I
would get the null value in colB - which I don't want.

Can I get a count of, and return, all non-null values in colB without
looping?

Thanks in advance.
 
Deko,

A dataset is a collection of relations and datatables
A datatable is a collection of columns and datarows
A datarow is a collection of items which describtion is in the datatable in
which they rows are in.

Therefore a field is forever
dataset.tables[x].Rows.Item[y]

Where x stands for index or tablename(string)
Where i stands for index
Where y stands for fieldname(string), index or columname.

I hope this helps,

Cor
 
dataset.tables[x].Rows.Item[y]
Where x stands for index or tablename(string)
Where i stands for index
Where y stands for fieldname(string), index or columname.

So I should be able to reference anything I want with this:

dataset.Tables[tableName].Rows[rowName].Item[columnName]

The part that was missing for me was Item[columnName]

Do I have to loop to get all the items in a particular column? No other
way?

In SQL it would look like this:

SELECT [myItem] FROM tblMyTable;

Do I have to wait for LINQ to do this with an ADO.NET DataSet?
 
Deko,
Do I have to wait for LINQ to do this with an ADO.NET DataSet?

What is against looping it is a normal way of getting Data, what your Select
statement does as well.

Why do you need it, however. If you want to represent a column in a binded
textbox, you just bind that column to the textbox.

If you want to show only one column in a DataGrid/DataGridView than you can
use styles

If you want a copy table with datarows only containing one column than you
can in version 2.0 use the overloaded version of the DataView.ToTable.

But in the case that you need this (I can me really not imagen where), than
Linq will probably your solution.

I hope this helps,

Cor
 
If you want a copy table with datarows only containing one column than you
can in version 2.0 use the overloaded version of the DataView.ToTable.

I will look into the DataView idea.

Thanks for the help.
 
Back
Top