【问题标题】:Inactive checkbox in datagridview VB.NETdatagridview VB.NET 中的非活动复选框
【发布时间】:2014-09-24 04:57:11
【问题描述】:

我像这样在datagridview 添加了复选框

 Dim CbxColumn As New DataGridViewCheckBoxColumn
                With CbxColumn
                    .HeaderText = ""
                    .Name = "Return"
                    .Width = 50
                End With
                dgvDetail.Columns.Insert(0, CbxColumn)

当我运行它时显示正确但现在我想动态禁用 dataGridView 上的某些行不是每一行只是某些行依赖于该行中的其他值我的意思是当 column2 具有值“打开”时我尝试这样做

 For i = 0 To dgvDetail.Rows.Count - 1
                    If dgvDetail.Rows(i).Cells(1).Value = "Open" Then
     //I want to do what i expect here//
                        dgvDetail.Rows(i).Cells(1).ReadOnly = True
                    End If
                Next

但它只是无法编辑值,但我更希望它像我们设置 buttoncontrol.enabled=false 时那样禁用为灰色或非活动控件,我该怎么办非常感谢

【问题讨论】:

    标签: c# sql-server vb.net


    【解决方案1】:

    试试这个:

     private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
    {
        if (e.RowIndex == 1)
        {
            DataGridViewCell cell=dataGridView1.Rows[e.RowIndex].Cells[0];
            DataGridViewCheckBoxCell chkCell = cell as DataGridViewCheckBoxCell;
            chkCell.Value = false;
            chkCell.FlatStyle = FlatStyle.Flat;
            chkCell.Style.ForeColor = Color.DarkGray;
            cell.ReadOnly = true;
    
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      你为什么不只是禁用你提到的单元格(只读),但也将其设置为 BackColor

        dgvDetail.Item(1, i).Style.BackColor = Color.LightGray
      

      【讨论】:

        【解决方案3】:

        您可以通过禁用 DataGridViewCell 来实现此目的

        private void enableCell(DataGridViewCell row, bool enabled) {
            //toggle read-only state
            row.ReadOnly = !enabled;
            if (enabled)
            {
                //restore cell style to the default value
                row.Style.BackColor = row.OwningColumn.DefaultCellStyle.BackColor;
                row.Style.ForeColor = row.OwningColumn.DefaultCellStyle.ForeColor;
            }
            else { 
                //gray out the cell
                row.Style.BackColor = Color.LightGray;
                row.Style.ForeColor = Color.DarkGray;
            }
        }
        

        或者您可以扩展上述代码以通过遍历每个单元格来禁用整个DataGridViewRow

        【讨论】:

          【解决方案4】:

          请查看此答案:https://stackoverflow.com/a/1626948/1361234

          我找到了一个项目来做到这一点:http://www.codeproject.com/Articles/31829/Disabled-Checkbox-Column-in-the-DataGridView

          但是,我使用了@Thirisangu的答案中的方法,它可以工作。谢谢你,蒂里桑古。

              chkCell.FlatStyle = FlatStyle.Flat;
              chkCell.Style.ForeColor = Color.DarkGray;
              cell.ReadOnly = true;
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-07-27
            • 1970-01-01
            • 1970-01-01
            • 2017-02-21
            • 1970-01-01
            • 2011-08-22
            • 2021-11-10
            • 1970-01-01
            相关资源
            最近更新 更多