Hideing and showing a DataGridViewButtonCell
-
Thursday, July 17, 2008 7:54 PM
I need help on how to make individual dataGridViewButtonCell's invisable and visible programically? ANybody?
Answers
-
Monday, July 21, 2008 6:25 AM
Hi Aarron345,
Based on my experience, there are two options to make the DataGridViewButtonCell invisible.
One way is that we can custom paint the cell. Since the button inside a DataGridViewButtonCell is not an actual button, it is painted like a button, we can paint particular cell to make it looks like a common cell by handling the CellPainting event. Please check the following link for details.
The other way is very tricky. We can change the DataGridViewButtonCell to DataGridViewTextBoxCell to hide the Button. Here is a sample for your information.
Code Snippetpublic partial class Form17 : Form
{
public Form17()
{
InitializeComponent();
}
private void Form17_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("item");
for (int i = 0; i < 50; i++)
{
dt.Rows.Add(i, "item" + i);
}
dt.AcceptChanges();
DataGridViewButtonColumn btnColumn = new DataGridViewButtonColumn();
btnColumn.DataPropertyName = "id";
btnColumn.Width = 100;
btnColumn.ReadOnly = true;
this.dataGridView1.Columns.Add(btnColumn);
this.dataGridView1.DataSource = dt;
foreach (DataGridViewRow dr in this.dataGridView1.Rows)
{
if (dr.Cells[0].Value != null)
{
int i;
if ((int.TryParse(dr.Cells[0].Value.ToString(),out i)))
{
if (i % 2 == 0)
{
DataGridViewTextBoxCell txtcell = new DataGridViewTextBoxCell();
dr.Cells[0] = txtcell;
}
}
}
}
}
}
Best regards.
Rong-Chun ZhangWindows Forms General FAQs
Windows Forms Data Controls and Databinding FAQs
All Replies
-
Monday, July 21, 2008 6:25 AM
Hi Aarron345,
Based on my experience, there are two options to make the DataGridViewButtonCell invisible.
One way is that we can custom paint the cell. Since the button inside a DataGridViewButtonCell is not an actual button, it is painted like a button, we can paint particular cell to make it looks like a common cell by handling the CellPainting event. Please check the following link for details.
The other way is very tricky. We can change the DataGridViewButtonCell to DataGridViewTextBoxCell to hide the Button. Here is a sample for your information.
Code Snippetpublic partial class Form17 : Form
{
public Form17()
{
InitializeComponent();
}
private void Form17_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("item");
for (int i = 0; i < 50; i++)
{
dt.Rows.Add(i, "item" + i);
}
dt.AcceptChanges();
DataGridViewButtonColumn btnColumn = new DataGridViewButtonColumn();
btnColumn.DataPropertyName = "id";
btnColumn.Width = 100;
btnColumn.ReadOnly = true;
this.dataGridView1.Columns.Add(btnColumn);
this.dataGridView1.DataSource = dt;
foreach (DataGridViewRow dr in this.dataGridView1.Rows)
{
if (dr.Cells[0].Value != null)
{
int i;
if ((int.TryParse(dr.Cells[0].Value.ToString(),out i)))
{
if (i % 2 == 0)
{
DataGridViewTextBoxCell txtcell = new DataGridViewTextBoxCell();
dr.Cells[0] = txtcell;
}
}
}
}
}
}
Best regards.
Rong-Chun ZhangWindows Forms General FAQs
Windows Forms Data Controls and Databinding FAQs -
Monday, February 13, 2012 8:26 PM
Second option works cool!