-1

How can I select datarow from another datarow? Datarow have [id,name,surname,phone,email,status] I need get datarow with [id, name] or [id,name,phone]

foreach (DataRow row in dt.Rows)
        {
//row have columns [id,name,surname,phone,email,status]
            switch ((Int32)row["status"])
            {
                case 1:
                    someFunction(row.[SELECT id,name]);  //need here datarow with colums id,name
                    break;
                case 2:
                    someFunction(row.[SELECT id,name,phone]);  //need here datarow with colums id,name,phone
                    break;
            }
        }
Alex
  • 39
  • 1
  • 8
  • What? Sorry. Come again. – Nikhil Agrawal Jul 02 '14 at 06:52
  • It's difficult to understand exactly what you're asking, and this question is coming very close to "can you please write this code for me", which is typically not allowed on SO. Please revise your question to be more clear about what you mean. – Leon Newswanger Jul 02 '14 at 07:18
  • You dont need write code for me. Question is simple: How get "DataRow with columns id,name" from "DataRow with columns id,name,phone..." If where is no way to do that - it will be the answer! – Alex Jul 02 '14 at 07:31

3 Answers3

0

You should pass it like this

someFunction(row["id"] + "," row["name"]);

and your some function should take string like this

Public Foo someFunction(string str)
{
  //Here split str with comma (,)
}
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
0

For Selecting rows in datatable you could use DataTable.Select method.

//select rows having status = 1
DataRow[] rows1 = dt.Select("status = 1");
//select rows having status = 2
DataRow[] rows2 = dt.Select("status = 2");

Or you could use overloaded function to deal with different rows.

foreach (DataRow row in dt.Rows)
{
     int iRowStatus = Convert.ToInt32(row["status"]);
     int iId = Convert.ToInt32(row["id"]);
     string sName = row["name"].ToString();
     string sPhone = row["phone"].ToString();

     switch (iRowStatus)
     {
        case 1:
           someFunction(iId,sName);  
           break;
        case 2:
           someFunction(iId,sName, sPhone);  
           break;
      }
} 

private void someFunction (int iId, string sName)
{
     //do something
}


private void someFunction (int iId, string sName, string sPhone)
{
     //do something
}
Hassan
  • 5,360
  • 2
  • 22
  • 35
0

The someFunction should be defined as:

DataRow someFunction(DataRow row,String columns){
return row.Table.DefaultView.ToTable(false,columns).Rows[row.Table.Rows.IndexOf(row)];
}

Note that this function can be optimized by caching the filtered datatable.

mrida
  • 1,157
  • 1
  • 10
  • 16