Click here to Skip to main content
15,395,181 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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"

C#
if(ResultSet.Rows[i][j].column name = "abc")
{
// then do something
}


or is there any way to iterate on datarow like
C#
foreach(datacolumn col in row)
{
if(col == "")
{
//do something
}
}
Posted
Updated 8-Aug-12 22:04pm
v4

DataColumn class has a public property called ColumnName. You can use that.
   
Comments
Gufran_khan 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.
dan!sh 9-Aug-12 4:20am
   
The object "col" in your code has a property called ColumnName. Use that.
Try this one
C#
DataTable DT = new DataTable();
foreach (DataColumn column in DT.Columns)
{
    Console.Write("Item: ");
    Console.Write(column.ColumnName);
    Console.Write(" ");
    Console.WriteLine(row[column]);
}
   
C#
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");
                }
   
Comments
Sebastian T Xavier 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.
mendy aaron 26-Aug-19 14:37pm
   
Thank you. I never new you could do this!
It's not so taugh. Write a linq query to store the column names in the array.
Try this:
C#
//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
   
Hi,
Please try it...

C#
DataTable dt = new DataTable();
            foreach (DataColumn dc in dt.Rows.Columns)
            {
                if (dc.ColumnName == "MyColumn")
                {
                    // Do code
                }
            }


Regards
Sebastian
   
v3
Comments
Gufran_khan 9-Aug-12 4:14am
   
not working throwing exception at "foreach (DataColumn dc in dt.Rows) " saying invalid cast.
Sebastian T Xavier 9-Aug-12 8:08am
   
This code will work without any issues
Sebastian T Xavier 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.
PipuMon 10-Jan-13 23:36pm
   
DataColumn dc in dt.Rows ?????? dt.Rows =DataRowCollection. of course invalid cast ...... (facepalm)
Sebastian T Xavier 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900