从 DataGridView WinForms 中删除蓝色行 [英] Remove blue colored row from DataGridView WinForms

查看:23
本文介绍了从 DataGridView WinForms 中删除蓝色行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我逐行填充 DataGridView 时(调用它的添加函数),顶行变为蓝色.它没有被选中,因为我也尝试了 ClearSelection() 但它没有用.它给人一种错误的错觉,即第一行被选中了.

When I fill a DataGridView row by row (calling it's add function), the top row gets blue colored. It's not selected because I tried ClearSelection() also but it didnt work. It's giving the wrong illusion that the first row is selected.

如何摆脱它?

 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;执行.

In the debugger I found that CurrentCell is already null before DataGridViewParties.CurrentCell = null; executes.

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

This question http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/c440c4f6-6dfc-47b6-97c0-1ce49c105b64/ is also related to it but does not offer a solution.

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

Its weird but it works for Load event, I was doing it in constructor. I want that when the first row is selected and when the user presses the UP arrow key, the focus moves to a certain textbox. But in this case it does not work (first row appears blue)

    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();
        }
    }

推荐答案

要与 ClearSelection 一起实现此目的,您需要再设置一个属性

To achieve this along with the ClearSelection you will need to set one more property

DataBindingComplete

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

编辑

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

Based on your comments you can modify the code as

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

这篇关于从 DataGridView WinForms 中删除蓝色行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆