【问题标题】:How to hide a particular checkbox cell in datagridview如何在datagridview中隐藏特定的复选框单元格
【发布时间】:2013-08-30 12:58:07
【问题描述】:

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

【问题讨论】:

  • 您想隐藏false 复选框单元格,那么如何检查它们?或者您只需要显示复选框单元格而不需要交互?
  • 它来自数据库。如果数据库中的 IsImage 为真,它会在 Datagribview 中检查。
  • 我也试过 "this.dgvleftEdit["IsImage", row.Index] = new DataGridViewTextBoxCell();"但在单元格中,它以文本形式显示真或假。
  • 你是否使用数据绑定来填充dataGridView中的DB数据?
  • 我正在使用绑定源填充DB DataGridView

标签: c# winforms datagridview


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    将您的 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;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-04
      • 2017-10-24
      相关资源
      最近更新 更多