Windows Develop Bookmark and Share   
 index > Windows Forms Sample Applications > Hideing and showing a DataGridViewButtonCell
 

Hideing and showing a DataGridViewButtonCell

I need help on how to make individual dataGridViewButtonCell's invisable and visible programically? ANybody?

Aarron345  Thursday, July 17, 2008 7:54 PM

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 Snippet

public 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 Zhang

Windows Forms General FAQs
Windows Forms Data Controls and Databinding FAQs

Rong-Chun Zhang  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 Snippet

public 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 Zhang

Windows Forms General FAQs
Windows Forms Data Controls and Databinding FAQs

Rong-Chun Zhang  Monday, July 21, 2008 6:25 AM

You can use google to search for other answers

Custom Search

More Threads

• Source Code
• Problem with Default namespace: http://tempuri.org
• TaskVision Excel dependency
• why I can't use my former acount?
• Pocket PC Client and accessing the server version on GotDotNet
• C# TaskVision does NOT include the code for the GDI+ controls!
• SQL-Server setup
• Unable to locate the localhost database
• TASKVISION DataLayer - How many table can the datalayer support
• æ— æ³•打开文件 C:\Program Files\Terrarium Server [Beta 1]\\website\chartdata\33b4a0ad-aefb-4661-8e5b-7152b87f0ecc.gif 以进行导出ã€