I've created a DataGridView and added a DataGridViewImageColumn (using the Designer).
The DataGridView has "AllowUserToAddRows = true" so a blank row is displayed for the user to enter a new row in. In this blank row the DataGridViewImageColumn displays a red x. I want to always display the same image in the column regardless of whether the row has any data or not (I am binding the cell's click event so using the DataGridViewImageColumn as a button).
How do I get rid of the red x?
I use an ImageList to hold all my images, but here's a trimmed down version that should help. You're right to set the Value, but you should set it to the null-equivalent of a bitmap, which is just a static blank image. I use:
e.Value = (imageIndex != int.MinValue) ? imageList.Images[imageIndex] : nullImage;
where nullImage is defined earlier in the class as:
private readonly Image nullImage = new Bitmap(1, 1);
I used IsNewRow property of grid.row like this
private void dgUserFileList_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) {
DataGridViewRow row = dgUserFileList.Rows[e.RowIndex];
if (row.IsNewRow) {
row.Cells[0].Value = null;
}
}
}
I looked through these solutions and the way I got this to work was to do this after InitializeComponent()
call in the constructor to make the default image for data bound rows an empty image
dataGridView1.Columns[0].DefaultCellStyle.NullValue = null;
And hook the RowPrePaintEvent
to remove the red X for the empty row at the end of the grid because AllowUserToAddRows = true
:
private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
if (e.RowIndex >= numberOfRows)
{
dataGridView1.Rows[e.RowIndex].Cells[0].Value = null;
}
}
Where numberOfRows
is the number of valid rows in the grid. Just spent a few hours dealing with this. Hope this saves someone else the headache...
I've found a solution, but I'm not sure it's the best way.
I override the RowsAdded event and set the Value of the DataGridViewImageColumn to null. I think since the value is null, it displays the image.
private void dgvWorksheet_RowsAdded(object sender,
DataGridViewRowsAddedEventArgs e)
{
dgvWorksheet.Rows[e.RowIndex].Cells[colStartClock.Index].Value = null;
}
I also set the NullValue of the Column to null in the Form_Load
colStartClock.DefaultCellStyle.NullValue = null;
I'm not sure if there's anything else I need to do. It seems to be working but it seems a bit buggy - random clicking sometimes results in exceptions, so more investigation is needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With