1

I had a Gridview which contains 3 columns , initially during form load i only two columns will be visible one column will be having data and other having checkbox.

i want when i check a checkbox in particular cell, corresponding to that checkbox the third column cell will be visible, i don't want to complete 3rd column to get visible on checking a check box ,Gridview is hardcoded only rows are dymanic (column1,column2 are set to visible and column 3 is set to invisible)

in below inmage when i m checking checkbox complete thried column is visible,which i don't want enter image description here

enter image description here

can any one help me in this?

i tried below code but it is making 3rd column visible not, particular cell

public form1() 
{
    dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
}

void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    dataGridView1.Columns[e.ColumnIndex + 1].Visible = true;
}
Rémi
  • 3,867
  • 5
  • 28
  • 44
Deadlock
  • 330
  • 1
  • 3
  • 21
  • You want any checked cell in the second column will have the corresponding cell in the third column `visible`? – King King Aug 16 '13 at 09:44
  • yes, i want the cell to be visible corresponding to the checkbox.... but clicking on one checkbox should not display complete column – Deadlock Aug 16 '13 at 09:51

3 Answers3

0

Well You can try RowCommand event for this but as i see you already have event created for checkbox ,Try to find row Index for particular row and then use Cell number (cell[2]) to find the control and assign its property as visible = false,

Suraj Singh
  • 4,041
  • 1
  • 21
  • 36
0

Here is the demo I've just tried. It seems to work OK. The whole idea is You can't hide a particular cell in DataGridView. However you can make it hidden as a normal GUI engine will use when it wants to hide any control/element (I think so). You just customize it to paint the cell with the BackgroundColor of your DataGridView. Of course, to make it work, it's not such easy. Here is the code for you:

//First, you have to be sure the whole third column is Visible.
//CellPainting event handler for your dataGridView1
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
        if (e.ColumnIndex == 2)//This is the Column index you want to hide.
        {                
            object o = e.RowIndex == -1 ? null : dataGridView1[e.ColumnIndex - 1,e.RowIndex].Value;
            if (o!=null &&!(bool)o || e.RowIndex == -1 || e.RowIndex == dataGridView1.RowCount - 1)
            {
                e.Graphics.FillRectangle(new SolidBrush(dataGridView1.BackgroundColor), e.CellBounds);
                if(e.RowIndex > -1) dataGridView1[e.ColumnIndex, e.RowIndex].ReadOnly = true;
                e.Handled = true;
            }
            if (o != null && (bool)o)
            {
                dataGridView1[e.ColumnIndex, e.RowIndex].ReadOnly = false;                    
            }
        }
    }
//CellContentClick event handler for your dataGridView1
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
   UpdateThirdColumCell(e);
}
//CellContentDoubleClick event handler for your dataGridView1
private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
   UpdateThirdColumCell(e);
}
private void UpdateThirdColumCell(DataGridViewCellEventArgs e)
{
        if (e.ColumnIndex == 1)//The column index of the CheckBox column
        {
            DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)dataGridView1[e.ColumnIndex, e.RowIndex];
            cell.Value = cell.EditingCellFormattedValue;
            dataGridView1.Invalidate();
            if ((bool)cell.Value)
            {
                dataGridView1.CurrentCell = dataGridView1[e.ColumnIndex + 1, e.RowIndex];
            }
        }
}
//CellStateChanged event handler for your dataGridView1
private void dataGridView1_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
{
    if (e.Cell.ColumnIndex == 2 && e.Cell.Selected)
    {
        dataGridView1.BeginEdit(false);
    }
}

And that's all :)

enter image description here

King King
  • 61,710
  • 16
  • 105
  • 130
0

Yeah ,actually we can't make invisible but i want to give an explianation about it in simple way.using

datagridview.rows(e.rowindex).cells[your column name]=true ; 

using this it won't allow the user to enter the data in the textbox.if it is false then we can modify it/enter any text.

datagridview.rows(e.rowindex).cells[your column name].backcolour=color.gray/white/black;

using this we can fill color to a particular textbox which is to be painted when checkbox is true/false;

saimanisha
  • 31
  • 1
  • 5