【问题标题】:DataGridView without selected row at the beginning开头没有选中行的DataGridView
【发布时间】:2013-04-08 12:48:15
【问题描述】:

在我的 WinForms 我有DataGridView。我想一次选择整行,所以我将SelectionMode 设置为FullRowSelect。现在我遇到了问题,因为在开始时我的表单在第一行下划线(选定的行集是空的,第一行没有被选择,但只是下划线)。我尝试了很多东西,例如:

    private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
        dataGridView1.ClearSelection();
    }

而且都失败了,因为实际上没有选择。

我怎样才能去掉这个下划线?

感谢您的帮助!

【问题讨论】:

  • 如果您是手动添加行,添加所有行后清除选择。

标签: c# .net winforms datagridview


【解决方案1】:

只需将dataGridView1.ClearSelection(); 放入表单的加载事件中即可。

【讨论】:

    【解决方案2】:

    这对我有用:

    private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
        dataGridView1.Rows[0].Selected = false;
    }
    

    【讨论】:

    • 不幸的是它不起作用。正如我所说,第一行没有被选中,它只是加了下划线。
    • 这可能是由于该行具有焦点。您可以尝试使用 focus() 方法将焦点放在不同的控件上。
    【解决方案3】:

    不幸的是,这些答案都没有帮助我,但我找到了其他解决方案。而不是无法选择,我将用这段代码隐藏它:

    dataGridView1.DefaultCellStyle.SelectionBackColor = dataGridView1.DefaultCellStyle.BackColor;
    dataGridView1.DefaultCellStyle.SelectionForeColor = dataGridView1.DefaultCellStyle.ForeColor;
    

    所以如果有人只想隐藏选择,它会很好用。

    干杯:)

    【讨论】:

      【解决方案4】:

      您应该尝试输入 Shown event datagridView.ClearCelection()datagridView.CurrentCell=null,例如,如果您想选择删除或更改信息的行,只需执行 if(datagridView.CurrentCell==null){ MessageBox.Show("You must select row");} 它对我有用

      【讨论】:

      • 这也适用于我!如果您像我一样将 ClearSelection 方法调用放在 OnLoad 事件处理程序中而不是 OnShown 中,那么 ClearSelection 将不知何故只工作一次(在您第一次加载表单时)。
      【解决方案5】:

      试试这可能会有所帮助

      private void dgv_order_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
              {
                  dgv_order.CurrentCell.Selected = false;
                  dgv_order.ClearSelection();
              }
      

      【讨论】:

        【解决方案6】:

        尝试在InitializeComponent() 之后的构造函数中设置DataGridView.AllowUserToAddRows = false

        【讨论】:

        • 它有效 :) 我唯一需要考虑的是在 Shown 事件中编写这行代码。当我在构造函数中写这个时,什么也没发生。
        【解决方案7】:

        您可以像这样在 form_Load 事件中调用 dataGridView.ClearSelection() 方法...

            private void Form1_Load(object sender, EventArgs e)
            {
             // You will get selectedCells count 1 here
             DataGridViewSelectedCellCollection selectedCells = dataGridView.SelectedCells;
             // Call clearSelection 
             dataGridView.ClearSelection();
             // Now You will get selectedCells count 0 here
             selectedCells = dataGridViewSchedule.SelectedCells;
            }
        

        【讨论】:

          【解决方案8】:

          这项工作对我来说是为了明确选择数据绑定

          Protected Sub GridCancel_DataBinding(sender As Object, e As EventArgs) Handles GridCancel.DataBinding
              GridCancel.SelectedIndex = -1
          
          End Sub
          

          【讨论】:

          • 这可能是它的工作原理......但这里的问题与Winforms Datagridview,而不是ASP.NET GridView有关,所以答案不合适。
          【解决方案9】:

          在开始时为禁用的选定行设置的事件是这样的, 并管理一个 FLAG 来停止 ClearSelection

          private void dataGridView_SelectionChanged(object sender, EventArgs e)
          {
          
              if (FLAG==true)
              {
                 dataGridView.ClearSelection();
                 FLAG=false;
              }
          }
          

          【讨论】:

            【解决方案10】:

            如果这是因为它在初始加载时引发了不需要的 GridView1_SelectionChanged 事件,您可以使用标志来处理此问题

            public partial class YourFormName
            { 
                private bool IsReady= false;
            
                private void YourFormName_Load(object sender, EventArgs e)
                { 
                       //Load your GridView1...
                       //Format your GridView1...
                        IsReady = true;
                }
                void GridView1_SelectionChanged(object sender, EventArgs e)
                {
                     if (!IsReady) 
                         return;
                     //do the rest of the stuffs
                }
            }
            

            【讨论】:

              【解决方案11】:

              有时,当您在不关闭程序的情况下重新加载表单时,第一行会突出显示。但它不会被选中,你会得到 -1 为选中的行索引。

              你可以这样做:

              1. 加载表单时存储默认样式:

               Public Class aRoots
                  Dim df1, df2, df3, df4 As Color
                  Private Sub aRoots_Load(sender As Object, e As EventArgs) Handles Me.Load
                          df1 = DGV_Root.DefaultCellStyle.SelectionBackColor
                          df2 = DGV_Root.DefaultCellStyle.BackColor
                          df3 = DGV_Root.DefaultCellStyle.SelectionForeColor
                          df4 = DGV_Root.DefaultCellStyle.ForeColor
              

               2. 与 datagridview 交互时更改单元格样式:

              Private Sub LoadRoot()
                     For i = 0 To 5
                              DGV_Root.Rows.Add()
                              For j = 0 To 3
                                  DGV_Root.Item(j, i).Value = ...
                              Next
                          Next
                      'DGV_Root.ClearSelection() ==> instead of this use 2 lines below
                      DGV_Root.DefaultCellStyle.SelectionBackColor = df2
                      DGV_Root.DefaultCellStyle.SelectionForeColor = df4
                  End Sub
              

               3. 更改选择时将单元格样式更改为默认值,例如 cell_click 或 cell_double click:

              Private Sub DGV_Root_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DGV_Root.CellMouseClick
                      DGV_Root.DefaultCellStyle.SelectionBackColor = df1
                      DGV_Root.DefaultCellStyle.SelectionForeColor = df3
              
              
              ...
              End Sub
              

              4. 当你想关闭表单时全部恢复为默认值:

              Private Sub PbClose_Click(sender As Object, e As EventArgs) Handles PbClose.Click
                      BtnCancel.PerformClick()
                      DGV_Root.DefaultCellStyle.SelectionBackColor = df1
                      DGV_Root.DefaultCellStyle.BackColor = df2
                      DGV_Root.DefaultCellStyle.SelectionForeColor = df3
                      DGV_Root.DefaultCellStyle.ForeColor = df4
                      Me.Close()
              End Sub
              

              希望对大家有所帮助。

              【讨论】:

                【解决方案12】:

                "Shown" 在第一次显示帧后运行的事件对我有用:

                private void frmMain_Shown(object sender, EventArgs e)
                        {
                            dataGridView1.ClearSelection();
                        }
                

                【讨论】:

                  【解决方案13】:

                  遇到同样的情况,“dataGridView.Shown”事件是唯一对我来说像魔术一样有效的解决方案:

                  // Clears the default selection of both Data Grid Views
                  private void dataGridView_Shown(object sender, EventArgs e)
                  {
                      dataGridView.ClearSelection();
                  }
                  

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2012-02-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2015-09-12
                    • 2018-08-08
                    相关资源
                    最近更新 更多