【问题标题】:How to prevent datagridview from selecting the first cell as currentcell on opening如何防止datagridview在打开时选择第一个单元格作为currentcell
【发布时间】:2013-08-01 11:32:12
【问题描述】:

我正在尝试找到一种方法来阻止我的 datagridview 选择第一个单元格作为默认单元格。现在,如果导入的单元格中有负数,我有代码可以将我的 datagridview 中的单元格的背景颜色变为红色。但是,这在我的第一个单元格中无法正常工作,因为默认情况下它已经在导入时突出显示。如果有人能找到如何关闭单元格的选择,我将不胜感激! :)

我知道它一定很简单,比如 DataGridView1.CurrentCell.Selected = False

【问题讨论】:

标签: vb.net datagridview


【解决方案1】:

按如下方式处理 DataGridView 的 DataBindingComplete 事件:

private void myGrid_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    myGrid.ClearSelection();
}

【讨论】:

    【解决方案2】:

    试试

     datagridview.currentrow.selected = true
    

    这将使整行被选中,因此更改当前背景颜色的代码不会受到影响。

    我有一个创建焦点的代码,但我忘记了。要设置网格的选择,您需要更改方向

    【讨论】:

      【解决方案3】:

      这个工作(和不同的选择模式网格视图)

      Public Function Grdvw_Cell_Unselect(ByVal Grdvw As DataGridView) As Boolean
          ' cancel all defaut selection in a datagridview
          Grdvw_Cell_Unselect = False
          Try
              Grdvw.ClearSelection()
              Grdvw.Item(0, 0).Selected = False
              Grdvw_Cell_Unselect = True
          Catch ex As Exception
          End Try
      End Function
      

      然后像这样使用: Grdvw_Cell_Unselect(your_datagridview) ...

      【讨论】:

        【解决方案4】:

        试试这个,对我有用。将此代码放在包含数据网格的表单代码中的任何位置。

        Private Sub YourDataGridName_DataBindingComplete(ByVal sender As System.Object, _
            ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) _
            Handles YourDataGridName.DataBindingComplete
        
            Dim DGV As DataGridView
            DGV = CType(sender, DataGridView)
            DGV.ClearSelection()
        End Sub
        

        (source)

        【讨论】:

          【解决方案5】:

          对我有用的是清除选​​择,然后根据行索引设置行。希望这会有所帮助。

             GridView.ClearSelection()
             GridView.Rows(RowIndex).Selected = True
             GridView.DataSource = DataTable
             GridView.Refresh()
          

          【讨论】:

            【解决方案6】:

            datagridview 绑定Paint 事件。

            点击datagridview > Properties > Paint > 双击Paint附近的空间 > 它会创建类似这样的方法:datagridview1_Paint(object sender, PaintEventArgs e)

            在显示的那个方法中写下这两行:enter code here

            private void datagridview1_Paint(object sender, PaintEventArgs e) {
                this.datagridview1.ClearSelection();
                this.datagridview1.CurrentCell = null;
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2011-11-04
              • 1970-01-01
              • 2010-10-21
              • 1970-01-01
              • 1970-01-01
              • 2011-12-10
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多