如何隐藏datagridview中的特定复选框单元格

时间:2013-08-30 12:58:07

标签: c# winforms datagridview

我有一个datagridview,其中包含一些textboxtype列和一个checkboxtype列。 CheckBoxColumn与bool类型属性绑定。

我希望如果选中复选框,它会在网格中看到,否则不会如图所示。

enter image description here

我在数据绑定完成时添加了一些代码,但它给出了编译时错误"Property or indexer 'System.Windows.Forms.DataGridViewCell.Visible' cannot be assigned to -- it is read only"

private void dgvleftEdit_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{   
    var reportLogoList = cWShowInvoicePaymentDetailsBindingSource.List as IList<CWShowInvoicePaymentDetails>;

    foreach (DataGridViewRow row in dgvleftEdit.Rows)
    {
        var objReport = row.DataBoundItem as CWShowInvoicePaymentDetails;
        var findItem = from f in reportLogoList
                       //where f.fReportID == objReport.fKey
                       select f;
        if (objReport.IsImage == false)
        {
            this.dgvleftEdit.Rows[row.Index].Cells[7].Visible = false;
        }
        else
        {
            this.dgvleftEdit.Rows[row.Index].Cells[7].Visible = true;
        }
    } 
}

是否可以在datagridview中隐藏特定的单元格?

2 个答案:

答案 0 :(得分:2)

我认为这就是你想要的,如果不是为了原因留下一些评论:

//CellPainting event handler for your dataGridView1
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) {
   if (e.ColumnIndex > -1 && e.RowIndex > -1 && dataGridView1.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn){
     if (e.Value == null || !(bool)e.Value) {
         e.PaintBackground(e.CellBounds, false);
         e.Handled = true;
     }
   }
}
//CellBeginEdit event handler for your dataGridView1
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e){
   if (dataGridView1.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn){
            object cellValue = dataGridView1[e.ColumnIndex, e.RowIndex].Value;
            e.Cancel = cellValue == null || !(bool)cellValue;
   }
}

答案 1 :(得分:1)

将您的DataGridVIewCheckBoxColumn更改为DataGridViewImageColumn

然后在datagridview.CellFormatting

的处理程序中
private void datagridview_CellFormatting(object sender,
                                          dataGridViewCellFormattingEventArgs e) 
{
    if (e.RowIndex >= 0 && dataGridView1.Columns[e.ColumnIndex] is DataGridViewImageColumn)
    {
        if (e.Value != null && (bool)e.Value == true)
        {
            e.Value = My.Resources.yourCheckedImage;
        }
        else
        {
            e.Value = null;
        }
    }
}

然后,单元格更新可以处理MouseDown处理程序或ClickEnter ..等的其他处理程序。

private void datagridview_MouseDown(Object sender, MouseEventArgs e)
{
    DataGridView dgv = (DataGridView)sender;
    DataGridView.HitTestInfo click = dgv.HitTest(e.Location.X, e.Location.Y);
    //If your have predefined columns, then maybe better compare by Column.name
    if(click.RowIndex >= 0 && dgv.Columns(click.ColumnIndex) is DataGridViewImageColumn)
    {
        DataGridViewCell cellTmp = dgv.Row(click.RowIndex).Cells(click.ColumnIndex);
        if (cellTmp.Value == null)
        {
            cellTmp.Value = My.Resources.yourCheckedImage;
        }
        else
        {
            cellTmp.Value = null;
        }
    }
}