I have a DataGridView
which was the subject of a previous question (link). But sometimes the Button is null
. This is fine. But if it is null, is th
For a more simple solution it is possible to hide the column containing the button you want to hide.
For example:
GridView1.Columns[0].Visible = false;
(First column)
Just count which column you want to hide starting from 0.
Handle custom painting and paint a textbox over there.
void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == yourColumnIndex && String.IsNullOrEmpty((string)e.FormattedValue))
{
Graphics g = e.Graphics;
TextBoxRenderer.DrawTextBox(g, e.CellBounds,
System.Windows.Forms.VisualStyles.TextBoxState.Normal);
e.Handled = true;
}
}
Based on Tobias' answer I made a small static helper method to hide the contents of the cell by adjusting it's padding.
Be aware though that the button is still "clickable" in that if the user selects the cell and presses space it clicks the hidden button, so I check that the cell's value is not readonly before I process any clicks in my contentclick event
public static void DataGridViewCellVisibility(DataGridViewCell cell, bool visible)
{
cell.Style = visible ?
new DataGridViewCellStyle { Padding = new Padding(0, 0, 0, 0) } :
new DataGridViewCellStyle { Padding = new Padding(cell.OwningColumn.Width, 0, 0, 0) };
cell.ReadOnly = !visible;
}