Reputation: 274
I have a DataGridView where the cells in a column can have differnt cell types.
For example the cell in row 1 has the cell type DataGridViewTextBoxCell. And the cell in row 2 has the cell type DataGridViewImageCell.
I have created a method that does something if the mouse is over a cell of that column:
private void DataTableCellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
if
(
e.RowIndex >= 0 // If the current row is not the header.
&& e.ColumnIndex == dataGridViewDMSSettings.Columns["Example"].Index // And if the current column is the example column.
)
{
// Something happing here.
}
}
Now I want to add a comparsion of the cell type to run the code only if the cell type is DataGridViewImageCell.
I have tried to add...
&& dataGridViewDMSSettings.Rows[e.RowIndex].Cells["Example"].GetType() == DataGridViewImageCell
... but I'm getting the IntelliSense message "DataGridViewImageCell is a type and invalid in the current context."
Does someone have a solution for this?
Upvotes: 1
Views: 958
Reputation: 101738
As indicated in the comments, you need to use the typeof()
operator to use a type as a value. You can't use a type name as a value without that.
dataGridViewDMSSettings.Rows[e.RowIndex].Cells["Example"].GetType() ==
typeof(DataGridViewImageCell)
However, there is a much better way to check whether a value is of a particular type, and that is the is
operator:
dataGridViewDMSSettings.Rows[e.RowIndex].Cells["Example"].GetType() is
DataGridViewImageCell
An important difference here is that is
respects inheritance relationships, while ==
plus typeof()
requires exact type equivalence.
Upvotes: 2