Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET ADO.NET
hi i require following thing to achieve
 
My problem is that i have data in datatable. now i want to chage value for one column only having name "xyz"
 
if(ResultSet.Rows[i][j].column name = "abc")
{
// then do something
}
 
or is there any way to iterate on datarow like
foreach(datacolumn col in row)
{
if(col == "")
{
//do something
}
}
Posted 8-Aug-12 22:55pm
Edited 8-Aug-12 23:04pm
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

DataColumn class has a public property called ColumnName. You can use that.
  Permalink  
Comments
Gufran_khan at 9-Aug-12 4:03am
   
yes but how? i want to find column in datarow not in table. so can not use table.column.columnname.
d@nish at 9-Aug-12 4:20am
   
The object "col" in your code has a property called ColumnName. Use that.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Try this one
DataTable DT = new DataTable();
foreach (DataColumn column in DT.Columns)
{
    Console.Write("Item: ");
    Console.Write(column.ColumnName);
    Console.Write(" ");
    Console.WriteLine(row[column]);
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

clone = ResultSet.Copy();
                clone.Columns.Remove("disbursementdate");
                clone.Columns.Add("disbursementdate");
                               for (int i = 0; i < ResultSet.Rows.Count; i++)
                {
                    clone.Rows[i]["disbursementdate"] = (Convert.ToDateTime(ResultSet.Rows[i]["disbursementdate"])).ToString("MM/dd/yyyy");
                }
  Permalink  
Comments
Sebastian T Xavier at 9-Aug-12 8:11am
   
Ha, very good!!! You asking a question(That's too not clear), then answers it and accepts yourself. This is not fair.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

It's not so taugh. Write a linq query to store the column names in the array.
Try this:
//Storing the column names in string array. (dt is your datatable object.)
string[] columnNames = dt.Columns.Cast<datacolumn>().Select(x => x.ColumnName).ToArray();
 
if(columnNames[0]=="MyCol1"){
    //do your work here.
}
else if(columnNames[1]=="MyCol12"){
    //do other work here.
}
</datacolumn>
 

--Amit
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
Please try it...
 
DataTable dt = new DataTable();
            foreach (DataColumn dc in dt.Rows.Columns)
            {
                if (dc.ColumnName == "MyColumn")
                {
                    // Do code
                }
            }
 
Regards
Sebastian
  Permalink  
v3
Comments
Gufran_khan at 9-Aug-12 4:14am
   
not working throwing exception at "foreach (DataColumn dc in dt.Rows) " saying invalid cast.
Sebastian T Xavier at 9-Aug-12 8:08am
   
This code will work without any issues
Sebastian T Xavier at 9-Aug-12 8:09am
   
I din't know , who down voted this answer. Anyway I don't think he is doing a fair job.
evilcd at 10-Jan-13 23:36pm
   
DataColumn dc in dt.Rows ?????? dt.Rows =DataRowCollection. of course invalid cast ...... (facepalm)
Sebastian T Xavier at 18-Jan-13 4:19am
   
corrected now

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 280
1 Sergey Alexandrovich Kryukov 279
2 CPallini 205
3 Maciej Los 197
4 Afzaal Ahmad Zeeshan 160
0 OriginalGriff 5,635
1 DamithSL 4,496
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 18 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100