1

I am new to C#. I know how to get it by using a simple for each loop as shown below. Can you show me how to get it by position/index/column number ?

foreach (DataRow row_ in dt.Rows){
  row = row_;
  foreach (DataColumn col_ in dt.Columns){
    col = col_;
    strMsg = strMsg + col.ColumnName + ": " + row[col.Ordinal].ToString() 
             + Environment.NewLine;
  }
  MessageBox.Show(strMsg);
  strMsg = "";
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Steam
  • 9,368
  • 27
  • 83
  • 122
  • I'm not sure that I'm clear about what you want to do but why don't you use DataReader to get specific column you want? Thats going to return results to you row by row. – curiousBoy Nov 05 '13 at 21:24
  • @curiousBoy - Instead of using that foreach to get a data column, i want to use something like this for(DataColumnAtIndex[index]){//code}. – Steam Nov 05 '13 at 21:27
  • yourGrid.Columns.IndexOf(col), something like this? – curiousBoy Nov 05 '13 at 21:30
  • @curiousBoy - thanks. but I want to use a datatable. Is there a similar method for that ? – Steam Nov 05 '13 at 21:31
  • 1
    You can look at here : http://stackoverflow.com/questions/3511576/getting-index-of-a-value-in-datatable hope it helps – curiousBoy Nov 05 '13 at 21:32
  • I don't think that will work. Let me clarify - I have columns in this order Col1,col2,Col3... I want to get the column and 7th position. so, I am looking for something like DataColumn dc = myDataTable.Column(7); Is that possible ? – Steam Nov 05 '13 at 21:44
  • 2
    does `myDataTable.Columns[7]` not work? – Vlad Nov 05 '13 at 21:46
  • Have you tried getting them in to a DataColumnCollection your_columns = yourTable.Columns; then get the index of that collection? That might work – curiousBoy Nov 05 '13 at 21:54
  • @Vlad - yes it does. thanks ! also, how do i get the number of columns ? – Steam Nov 06 '13 at 00:34
  • 2
    @blasto `myDataTable.Columns.Count` or `myDataTable.Columns.Count()` with Linq – Vlad Nov 06 '13 at 15:05

1 Answers1

1

You can fetch the ordinal via IndexOf. Here it is in your example:

foreach (DataRow row_ in dt.Rows) {
  row = row_;
  foreach (DataColumn col_ in dt.Columns) {
    col = col_;
    strMsg = strMsg + col.ColumnName + ": " + row[dt.Columns.IndexOf(col_)].ToString() 
         + Environment.NewLine;
  }
  MessageBox.Show(strMsg);
  strMsg = "";
}

And here is a fully working sample:

var dt = new DataTable();

dt.Columns.Add(new DataColumn());
dt.Columns.Add(new DataColumn());

var row1 = dt.NewRow();
row1[0] = "0";
row1[1] = "1";
dt.Rows.Add(row1);

foreach (DataRow row in dt.Rows)
{
    foreach (DataColumn col in dt.Columns)
    {
        var strMsg = col.ColumnName + ": " + row[dt.Columns.IndexOf(col)].ToString();
        Console.WriteLine(strMsg);
    }
}

If you just want the column names in an array, remove the foreach loops and replace them with this line:

string[] colNames = (from DataColumn col in dt.Columns select col.ColumnName).ToArray();
Chris Schiffhauer
  • 17,102
  • 15
  • 79
  • 88