Click here to Skip to main content
15,884,042 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
Hi ,
I have an unbound datagridview populated with data, some columns are checkbox columns,
I would like to read a cell in a row and based on the value
I would like to hide a checkbox in certain cells so that the user does not see it. How can I do this please.
Posted

use the CellPainting event

C#
private void Grid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
    {
        if (Grid.Columns[e.ColumnIndex].Name == "CheckBoxColumn" && Grid.Rows[e.RowIndex].Cells["ColumnToCheck"].Value == <TrueHideValue>)
        {
            e.PaintBackground(e.ClipBounds, true);
            e.Handled = true;
        }
    }
}
 
Share this answer
 
Comments
Member 13474881 24-Oct-17 22:44pm    
Where did you based that "<truehidevalue>"?
Hi,
You can do that in ItemDataBound event of Datagrid.
Search the column you want with help of FindColumnByUniqueName and you can hide that column. Give a try...

Thank you,
-------------
Amith V.A.
 
Share this answer
 
You can handle this during ItemDataBound event of the datagrid:

In this event, while the data is binded row by row - add something like:
C#
protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
// 
//
   //This textbox has the value of the cell based on which you need to hide checkbox
   Textbox txtCheckValue =   (Textbox)e.Row.FindControl("Textbox1"); 
   //Check the value condition as per your wish
   if(txtCheckValue_SatisfyCertainCondition())
   {
     Checkbox chkCurrRow =   (Checkbox)e.Row.FindControl("Checkbox1"); 

     // set visibility of your control
     chkCurrRow.Visible = false;
    }
//
//
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900