Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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
v3
Comments
Sandeep Mewara at 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 at 23-Sep-12 14:01pm
   
Yes, i know and i am asking about similar event for winForm which could help.
Wes Aday at 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 at 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 at 24-Sep-12 14:07pm
   
Using a DataTable can help the comparisons a lot easier.
supernorb at 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 at 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 at 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 at 25-Sep-12 12:03pm
   
Thanks for your solution. I am wondering on which particular event of DataGridView i should write this code.
supernorb at 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 at 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 at 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

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

Solution 1

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  
v2

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 395
1 Abhinav S 235
2 Nirav Prabtani 187
3 _Amy 160
4 Dave Kreskowiak 155
0 OriginalGriff 7,540
1 Sergey Alexandrovich Kryukov 6,597
2 Maciej Los 3,849
3 Peter Leow 3,653
4 CHill60 2,712


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 25 Sep 2012
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