How to select column from a datatable

  • Thread starter Thread starter Guest
  • Start date Start date
Hi Chung:
Chung said:
Hi All,

Does anyone know how can I select some column from a datatable?

Chung

What do you mean by Select? You can reference a column in a DataTable via
it's .Columns property ie myDataTable.Columns[0] would give you the first
column in the table, (Or you can reference it by name, so if column 0 was
named "User_Name") you could reference myDataTable.Columns["User_Name"];

Where possible, use the numeric index instead b/c it's a lot faster (for
instance, if you were iterating through 10000 rows you'd probably see a
notable performance difference). or you can use Bill Vaughn's cool method
and use an enum.

Now, if you are talking about a column in context of a row you could use
foreach(DataRow dro in myDataTable.Rows){
string s = (string)dro[0];
}

There is also a .Select method but I'm guessing that's not what you're
after.

Let me know if I didn't answer it and I'll do what I can.

HTH,

Bill


www.devbuzz.com
www.knowdotnet.com
 
----- William Ryan eMVP wrote: ----

Hi Chung
Chung said:

What do you mean by Select? You can reference a column in a DataTable vi
it's .Columns property ie myDataTable.Columns[0] would give you the firs
column in the table, (Or you can reference it by name, so if column 0 wa
named "User_Name") you could reference myDataTable.Columns["User_Name"]

Where possible, use the numeric index instead b/c it's a lot faster (fo
instance, if you were iterating through 10000 rows you'd probably see
notable performance difference). or you can use Bill Vaughn's cool metho
and use an enum

Now, if you are talking about a column in context of a row you could us
foreach(DataRow dro in myDataTable.Rows)
string s = (string)dro[0]


There is also a .Select method but I'm guessing that's not what you'r
after

Let me know if I didn't answer it and I'll do what I can

HTH

Bil


www.devbuzz.co
www.knowdotnet.co


Hi Bill

Thank you for your quick respones
I think I have to make my question more clear. What I mean is within the DotNet framework, any fast function which can select some of the column from a datatable by passing a query statement. Like (Select column_01, column_02 from Datatable)

I know that we can use DataSet.Tables[0].Select("ID=10") to get all the datarow which contains ID=10, but it will return all the datacolumn from the datatable. Do you know any function can select some of the datacolumn from a datatable

Chun
 
Not as such, but you can reference the affected columns specifically so that
shouldn't be a big deal. Remember that columns are just pieces in a
collection so you can reference those directlly.

HTH,

Bill
Chung said:
----- William Ryan eMVP wrote: -----

Hi Chung:
Chung said:

What do you mean by Select? You can reference a column in a DataTable via
it's .Columns property ie myDataTable.Columns[0] would give you the first
column in the table, (Or you can reference it by name, so if column 0 was
named "User_Name") you could reference myDataTable.Columns["User_Name"];

Where possible, use the numeric index instead b/c it's a lot faster (for
instance, if you were iterating through 10000 rows you'd probably see a
notable performance difference). or you can use Bill Vaughn's cool method
and use an enum.

Now, if you are talking about a column in context of a row you could use
foreach(DataRow dro in myDataTable.Rows){
string s = (string)dro[0];
}

There is also a .Select method but I'm guessing that's not what you're
after.

Let me know if I didn't answer it and I'll do what I can.

HTH,

Bill


www.devbuzz.com
www.knowdotnet.com



Hi Bill,

Thank you for your quick respones.
I think I have to make my question more clear. What I mean is within
the DotNet framework, any fast function which can select some of the column
from a datatable by passing a query statement. Like (Select column_01,
column_02 from Datatable).
I know that we can use DataSet.Tables[0].Select("ID=10") to get all the
datarow which contains ID=10, but it will return all the datacolumn from the
datatable. Do you know any function can select some of the datacolumn from a
datatable.
 
Back
Top