繁体   English   中英

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

[英]How to hide a particular checkbox cell in datagridview

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

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

在此输入图像描述

我在数据绑定完成时添加了一些代码,但它给出了编译时错误"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中隐藏特定单元格?

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

//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;
   }
}

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 ..etc的其他处理程序处理。

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;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM