Click here to Skip to main content
12,403,130 members (72,073 online)
Rate this:
 
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 21:55pm
Updated 8-Aug-12 22: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 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 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 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 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.
evilcd 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 18 Jan 2013
Copyright © CodeProject, 1999-2016
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