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!