Click here to Skip to main content
Sign Up to vote bad
good
See more: C#WinForm
Hi,
 
DataGrid view has data populated from excel sheet.
I want to compare COLUMN 2[PR_NO] and COLUMN 3[STUDY_NO] in datagrid view for each row.
I used several events such as CELL_VALIDATING, CELL_VALIDATE, each ain't working.
 
Can you please help with event and piece of code so that i can learn.
Thanks in Advance.
 

P.S. In Gridview, this can be in RowDataBound event but couldn't find such event in DataGridview.
Posted 23-Sep-12 3:38am
Edited 23-Sep-12 4:06am

Comments
Sandeep Mewara - 23-Sep-12 10:07am
RowDataBound event is present for Grids in ASP.NET (web application). What you are trying is a Winforms.
Vineet_2109 - 23-Sep-12 14:01pm
Yes, i know and i am asking about similar event for winForm which could help.
Wes Aday - 23-Sep-12 10:11am
"ain't working" is not a description of any kind of a problem. So you don't have a real question you are just looking for a tutor?
Vineet_2109 - 23-Sep-12 14:00pm
Yes, if i can get a tutorial then its perfectly fine. Afterwards i will implement in my project.
Rajesh Kuramdasu - 24-Sep-12 14:07pm
Using a DataTable can help the comparisons a lot easier.
supernorb - 24-Sep-12 16:50pm
Not clear at all, What is the data type of each column? What may the result be after the comparison? How do you want to compare two columns? I think the most primary way is to loop through all the rows and make a comparison at each row, the comparisons at different rows may be different so You have to decide yourself the result of the entire comparison between the two columns.
Vineet_2109 - 24-Sep-12 18:14pm
C1 C2 R1 22-11 22-11 R2 45 33 C1 and C2 are coloumn and R1 & r2 are rows.. for 1st Row 22-11 is same in both columns but in 2nd row it 45 and 33 do not match. so i need to to this comparision. It data do not match so that row will be seen in red color. Please help. DataGridView
supernorb - 25-Sep-12 4:29am
Well, if so I think it is nice to make a loop through all the rows, if it doesn't meet your requirement, then try changing its back color to red. Even if you have another way, I think the underlying algorythm would use a loop.
Vineet_2109 - 25-Sep-12 12:03pm
Thanks for your solution. I am wondering on which particular event of DataGridView i should write this code.
supernorb - 25-Sep-12 14:06pm
Place the code after InitializeComponents() in the form constructor. Of course the important thing is the DataGridViewRow.DefaultCellStyle has to be set or changed after the form is loaded (that's why I placed the style changing code in Load event handler), therefore you can also place that code in a Click event handler of a button (at the time you can click that button, your form should be surely loaded so that's OK). Hope it helps!
Vineet_2109 - 26-Sep-12 15:51pm
On the button click event it would work perfectly fine. But when data is loaded at that time is should check for validation. so which event should be proper ?
supernorb - 27-Sep-12 0:25am
As I said, the important thing is your form should be loaded before changing the cell style of a particular row. As in my code, the datagridview should be filled with data first before validating, if you want to validate the data first, you should store data in somewhere like a datatable and do the same validating algorithm on it instead of on the datagridview. I hope you can easily do that yourself!

1 solution

Try this:
var mylist = new[] {
                new{ A = 1, B = 1},
                new {A = 2, B = 2},
                new {A = 3, B = 2},
                new {A = 4, B = 4}};
            
dataGridView1.DataSource = mylist;
Load += (sender, e) =>
     {
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
          if((int)row.Cells["A"].Value != (int)row.Cells["B"].Value) 
              row.DefaultCellStyle.BackColor = Color.Red;
        }                
     };
 
Please remember that, the DefaultCellStyle.BackColor is set successfully for a particular row of datagridview only when the form is loaded. I searched for this before posting my code here for you.
 
Hope it helps!
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Christian Graus 514
1 Ron Beyer 296
2 OriginalGriff 248
3 samadhan_kshirsagar 229
4 Tadit Dash 213
0 Sergey Alexandrovich Kryukov 7,007
1 Prasad_Kulkarni 3,815
2 OriginalGriff 3,557
3 _Amy 3,372
4 CPallini 2,975


Advertise | Privacy | Mobile
Web01 | 2.6.130617.1 | Last Updated 25 Sep 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid