1

我有一个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 中隐藏特定单元格?

4

2 回答 2

3

我认为这就是你想要的,如果没有留下一些评论为什么:

//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;
   }
}
于 2013-08-30T14:33:32.637 回答
1

改变你DataGridVIewCheckBoxColumnDataGridViewImageColumn

然后在处理程序中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;
        }
    }
}
于 2013-08-31T21:58:17.600 回答