Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi community,

I managed to bind a string[][] to a datagridview like this:

SQL
dataGridView1.DataSource = (from arr in test select new { Data = arr[0], Dog = arr[1] }).ToArray();


DataGridView1 = a dataGridView and test is a string[x][2] (yes I am using a jagged array for a rectangular matrix). I am happy with that and would like to keep it that way.


Now lets picture the following:

current datagridviewContent:

C#
         Col1     Col2
Row1     Marc     John
Row2     Anna     Susi



during the next update of my string[][] Susi changes to Alex like :

changed datagridviewContent:

C#
         Col1     Col2
Row1     Marc     John
Row2     Anna     Alex


Due to the change I would like an event to be raised that will give me both the row and column index of all changed cells - or even better I would like EACH affected cell to raise an event so that I can f.e. change the affected cells background colour.

I have tried using the inbuilt CellValueChanged event, but I believe this does not work, because i am not changing the respective cells individually, but update the datagridview using a datasource.
I have tried the DataSourceChanged event, but its EventArgs are generic and do not implement a row or column property.

How do I raise an event in this case that will give me the mentioned properties?
I am aware of writing my own events, but I would assume that visual studio already has an event implemented for a situation like this and I must admit in this particular case I am not sure how I would implement an event.

Help is appreciated, thank you all in advance.
Regards,

-DK
Posted
Comments
kbrandwijk 2-Sep-14 17:58pm    
Why are you reposting part of your original question as new question here?
KergalBerlin 2-Sep-14 18:13pm    
posted that one before I read your answer.
Thruth be told, I felt that Sergey Kryukov was trolling me in the other thread and I wanted to avoid "fun" responses and confusion. Besides, I feel I phrased my question better this time.

1 solution

1. Keep your old array of values
2. Read your new array of values
3. Extend your LINQ query to compare the two
4. Use the PaintCell event of the grid to look at the '...Changed' boolean to change backgroundcolor etc.

C#
string[][] origArr =
    {
        new [] {"A", "B", "C"},
        new [] {"D", "E", "F"},
        new [] {"G", "H", "I"}
    };

string[][] newArr =
    {
        new [] {"A", "B", "C"},
        new [] {"D", "X", "F"},
        new [] {"G", "H", "K"}
    };

var datasource = (from arrOld in origArr
                  from arrNew in newArr
                  select new
                  {
                      First = arrOld[0],
                      FirstChanged = arrNew[0] != arrOld[0],
                      Second = arrOld[1],
                      SecondChanged = arrNew[1] != arrOld[1],
                      Third = arrOld[2],
                      ThirdChanged = arrNew[2] != arrOld[2]
                  });
 
Share this answer
 

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