How to show buttons in cells of DataGridView if, for example, column A has value?
private void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
var dgv = (DataGridView)sender;
if (dgv.Columns[e.ColumnIndex].Name == "btn")
{
var hide= dgv.Rows[e.RowIndex].Cells[dgv.Columns["A"].Index].Value is DBNull;
var cell = dgv.Rows[e.RowIndex].Cells[dgv.Columns["B"].Index];
if (hide)
{
//cell.Value = null;
//cell = new DataGridViewTextBoxCell();
cell.Visible = false; // This dosn't work
}
}
}
Probably the best way to do this is to bind to the Item_DataBound (I believe that's what it's called) on the grid view. Then check the column to see if it contains the value you need. Then you can hide specific controls in the cell like this (using most of your code in your post):
cell.Controls[0].Visible = false;
Create a custom DataGridView
and CustomDataGridViewColumn
for it and assign the property to set visible
and invisible
particular cell button. here is an example that demonstrate how to create custom column for datagridview. http://www.codemag.com/Article/0707061 otherwise it is not possible to make any cell button directly either visible or invisible.
You need to cast button containing cell to DataGridViewButtonCell . The Cell
can be cast to this class if it is Button Cell
.
Use this:
var cell = dgv.Rows[e.RowIndex].Cells[dgv.Columns["B"].Index] as DataGridViewButtonCell;
cell.Visible = false;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.