Hi,
I need to loop through a specific column(Not all the columns) in DataGridview amd update some values: For Example I have dataGridView populated with a csv file and contains 4 column as

Name, Age, Asset
Martin,32,350
John,19,12
Joay,47,1200
humberto,50,950
Richardo,62,23

I displaye the cells value in DataGridView on a WinForm now I would like to loop through the Columns just by the Column Name. For example How i I can loop through Column = Age and find all ages under 30 and How I can loop throuh the Asset column and add 100 to all values lesss than 500.
Please be advise that I need to serch the column by COULMN NAME because I am not getting the Csv file in the same order as mentioned abouve so i have to use a loop which find the coulmns by thir name

Thanks alot

Loop through the rows of particular column:
1.

    List<string> usersOver30  = new List<string>();
    foreach(DataGridViewRow row in dataGridView1.Rows)
    {
         int age = 0;
         if(int.TryParse(row["Age"].Value.ToString(), out age))
             if(age >= 30) 
                  usersOver30.Add(row["Name"].Value.ToString();
    }
    //list contains all names of users that are over 30 years old

2.

foreach(DataGridViewRow row in dataGridView1.Rows)
{
     DataGridViewCell cell = row["Asset"] as DataGridViewCell;
     int asset = 0;
     if(int.TryParse(cell.Value.ToString(), out asset))
         if(asset < 500) 
              cell.Value = (asset + 100).ToString();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.