【问题标题】:Remove blue colored row from DataGridView WinForms从 DataGridView WinForms 中删除蓝色行
【发布时间】:2011-05-11 07:47:29
【问题描述】:

当我逐行填充 DataGridView 时(调用它的 add 函数),顶行变为蓝色。 它没有被选中,因为我也尝试过 ClearSelection() 但它没有用。 选择第一行会产生错误的错觉。

如何摆脱它?

 void FillDGV()
    {
        dataGridViewParties.Rows.Clear();
        for (int i = 0; i < dataGridViewParties.Columns.Count; i++)
        {
            dataGridViewParties.Columns[i].Width = this.Width / dataGridViewParties.Columns.Count;
        }


        DataTable partyTbl = UtilityClass.GetDataTable(@"SELECT [PartyID]
                                                        ,[PartyName]
                                                        ,[PartyAddress]
                                                        ,[PartyState]
                                                        ,[PartyCity]
                                                        ,[PartyPhone]
                                                        FROM [VegiManager].[dbo].[Parties]
                                                        WHERE [PartyName] LIKE '" + textBoxPartySearch.Text + "%' ");
        foreach (DataRow dr in partyTbl.Rows)
        {
            dataGridViewParties.Rows.Add(1);

            dataGridViewParties.Rows[dataGridViewParties.Rows.Count - 1].Cells[0].Value = dr["PartyID"].ToString();
            dataGridViewParties.Rows[dataGridViewParties.Rows.Count - 1].Cells[1].Value = dr["PartyName"].ToString();
            dataGridViewParties.Rows[dataGridViewParties.Rows.Count - 1].Cells[2].Value = dr["PartyAddress"].ToString();
            dataGridViewParties.Rows[dataGridViewParties.Rows.Count - 1].Cells[3].Value = dr["PartyState"].ToString();
            dataGridViewParties.Rows[dataGridViewParties.Rows.Count - 1].Cells[4].Value = dr["PartyCity"].ToString();
            dataGridViewParties.Rows[dataGridViewParties.Rows.Count - 1].Cells[5].Value = dr["PartyPhone"].ToString();
        }

        if (dataGridViewParties.Rows.Count > 0)
        {
            dataGridViewParties.ClearSelection();
            dataGridViewParties.CurrentCell = null;
        }
    }

在调试器中,我发现在 DataGridViewParties.CurrentCell = null; 之前 CurrentCell 已经为 null;执行。

这个问题http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/c440c4f6-6dfc-47b6-97c0-1ce49c105b64/也和它有关,但没有提供解决方案。

编辑:它很奇怪,但它适用于 Load 事件,我是在构造函数中做的。 我希望当第一行被选中并且用户按下向上箭头键时,焦点移动到某个文本框。但在这种情况下它不起作用(第一行显示为蓝色)

    private void dataGridViewInwards_KeyDown(object sender, KeyEventArgs e)
    {
         if (e.KeyCode == Keys.Up && dataGridViewParties.SelectedRows.Count > 0 && dataGridViewParties.SelectedRows[0].Index == 0)
        {
            textBoxPartySearch.Focus();
            dataGridViewParties.ClearSelection();
            dataGridViewParties.CurrentCell = null;
        }
        else if (e.KeyCode == Keys.Up && dataGridViewParties.SelectedRows.Count == 0)
        {
            textBoxPartySearch.Focus();
        }
    }

【问题讨论】:

  • 可能没有选中,但可能是焦点行
  • 我刚查了一下,发现一个文本框有焦点。
  • 我尝试了 dataGridView.ClearSelection() 并且工作正常。可以分享一下代码吗?

标签: c# .net winforms datagridview


【解决方案1】:

要与ClearSelection 一起实现这一点,您需要再设置一个属性

DataBindingComplete试试这个

dataGridView1.ClearSelection();
dataGridView1.CurrentCell = null;

编辑

根据您的 cmets,您可以将代码修改为

if (e.KeyCode == Keys.Up && dataGridView1.CurrentCell.RowIndex == 0)
{                   
     this.ActiveControl = textBoxPartySearch;
     dataGridView1.Refresh();
     dataGridView1.ClearSelection();
     dataGridView1.CurrentCell = null;
     e.Handled = true;
}

【讨论】:

  • 我实际上是逐行填充datagridview(不是数据绑定它)。我应该把那个代码放在哪里?我尝试在填充 datagridview 后放置它,但它不起作用!
  • 我尝试添加行并在之后设置语句..对我有用。检查您是否正在执行任何其他操作
  • 是的,我也做过类似的事情,在调用FillDGV() 之后,您是否执行任何操作?这可能是一些微不足道的事情..检查(我已经在表单的加载中完成了这段代码)
  • 好的,我是在构造函数中做的。它在 Load 事件中工作。 But I am using one code to move the focus to top textbox when the first row is selected and the user presses the UP arrow but it does not work.查看我有问题的更新。
  • 抱歉没听明白,顶部文本框是什么意思?你想按箭头做什么?
【解决方案2】:

我尝试了上述方法,但没有任何效果。

最后,我只是决定将选定单元格的默认颜色设置为与未选定单元格相同。

然后,在单元格单击方法中,我将它们设置回来。这样,在单击之前没有任何内容被选中,这对我的应用程序来说已经足够了。这是我在 click 方法中使用的代码,用于将一切恢复正常:

dataGridView.DefaultCellStyle.SelectionBackColor = SystemColors.Highlight;
dataGridView.DefaultCellStyle.SelectionForeColor = SystemColors.Window;

令人讨厌的是,如果我将 ClearSelection 方法放在一个按钮中,它实际上工作得很好,但是如果我创建包含数据网格的控件,将一些数据加载到其中,然后尝试立即清除它,它不会工作。我希望我知道为什么......

【讨论】:

  • 是的,datagridview 有很多 bug。希望 MS 能够处理它。
【解决方案3】:

尝试将 ClearSelection() 添加到 VisibleChanged 事件中,如下所示:

private void datagridview1_VisibleChanged(object sender, EventArgs e)
{
datagridview1.ClearSelection();
}

【讨论】:

    【解决方案4】:

    Form.Shown事件中做一个:

    dataGridView1.ClearSelection();
    dataGridView1.CurrentCell = null;
    

    【讨论】:

      【解决方案5】:

      经过三个小时的故障排除后,我想通了:您的DataGridView 需要显示在屏幕上之前调用dataGridView.ClearSelection()

      我怎么知道这是有效的:

      我有两个DataGridViews,每个都在不同的面板中。一次只能看到一个面板。在最初可见的面板中,DataGridView 从未忽略我对ClearSelection() 的调用,因此我从未见过立即选择的行。

      我曾经填充另一个DataGridView 并在显示包含它的面板之前清除它的选择,但最上面一行仍然突出显示。在清除 DataGridView 的选择之前,我更改了代码以显示另一个面板,并且对 ClearSelection() 的调用正常工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-30
        • 1970-01-01
        • 2020-09-21
        • 2019-02-04
        • 2017-11-24
        • 2016-08-15
        相关资源
        最近更新 更多