0
votes

Terrible title, let me explain more. I have a DataGridView that I want to customize a little by adding a few properties to each cell, to each column, and to each row. What I can't wrap my head around is how to pull my CustomDataGridViewRow (my class that inherits DataGridViewRow) out of the DataGridView.Rows object, since that's a collection of DataGridViewRow.

If my understanding of casting is correct, it would be wrong (and impossible) to cast DataGridViewRow to CustomDataGridViewRow, but as I see it, the only way to access the DataGridView's row collection is via DataGridView.Rows.

Here's what I want to do, essentially:

foreach(CustomDataGridViewRow row in myDataGridView.Rows) // <-- throws InvalidCastException
{
    //do something with each row's custom property
}

CustomDataGridViewRow is defined very simply:

class CustomDataGridViewRow : DataGridViewRow
{
    string RowType;
    public CustomDataGridViewRow() { }
}

What's the correct way to go about this?

Edit: Here's the kicker... the above code throws an InvalidCastException, as expected, but the same thing works flawlessly with a CustomDataGridViewColumn (accessed via DataGridView.Columns) and a CustomDataGridViewCell (accessed via DataGridView.Rows[i].Cells). The custom classes are defined the same way as above... I can loop through them and the derived classes' properties are editable and preserved. Pulling my hair out over this!

2

2 Answers

0
votes

I don't know how to get your implementation working, but if I was designing what you need (if I understand it correctly), here's how I would do it.

public class MyRowData { }
public class MyCellData { }
public class MyColumnData { }
public static class Extender
{
    static Dictionary<DataGridViewRow, MyRowData> dictRow = new Dictionary<DataGridViewRow, MyRowData>();
    static Dictionary<DataGridViewCell, MyCellData> dictCell = new Dictionary<DataGridViewCell, MyCellData>();
    static Dictionary<DataGridViewColumn, MyColumnData> dictColumn = new Dictionary<DataGridViewColumn, MyColumnData>();
    public static MyRowData GetMyRow(this DataGridViewRow row)
    {
        MyRowData myRow;
        if (dictRow.TryGetValue(row, out myRow))
            return myRow;
        return null;  // or throw error or return blank MyRow...
    }
    public static void SetMyRow(this DataGridViewRow row, MyRowData myRow)
    {
        dictRow[row] = myRow;
    }
}

I only put in the code for the row extensions but column and cell would be exactly the same. You'd probably have problems with cloning or casting but they could be worked around.

Just a suggestion in case you can't get your code working...

0
votes

Or...(du-uh, don't know why I didn't think of it sooner) just use the cell/column/row tags:

        cell.Tag = new MyCellData();
        row.Tag = new MyRowData();
        column.Tag = new MyColumnData();

Now you can add your properties to your "My" classes:

public class MyCellData {
    public string Property1 { get; set; }
    public string Property2 { get; set; }
}