从DataGridView的WinForms删除蓝色行蓝色、DataGridView、WinForms

2023-09-04 22:34:12 作者:给我十块钱让你踢两脚

当我填写一个DataGridView逐行(调用它的附加功能),顶行获得蓝色。 这不是选择,因为我试过ClearSelection()还,但它没有工作。 它给了错误的错觉,第一行被选中。

如何摆脱它?

 无效FillDGV()
    {
        dataGridViewParties.Rows.Clear();
        的for(int i = 0; I< dataGridViewParties.Columns.Count;我++)
        {
            dataGridViewParties.Columns [I] .WIDTH = this.Width / dataGridViewParties.Columns.Count;
        }


        数据表partyTbl = UtilityClass.GetDataTable(@选择[PartyID]
                                                        [PartyName]
                                                        [PartyAddress]
                                                        [PartyState]
                                                        [PartyCity]
                                                        [PartyPhone]
                                                        FROM [VegiManager]。[DBO]。[缔约方]
                                                        WHERE [PartyName] LIKE'+ textBoxPartySearch.Text +%');
        的foreach(在partyTbl.Rows的DataRow博士)
        {
            dataGridViewParties.Rows.Add(1);

            dataGridViewParties.Rows [dataGridViewParties.Rows.Count  -  1] .Cells [0] .value的=博士[PartyID]的ToString()。
            dataGridViewParties.Rows [dataGridViewParties.Rows.Count  -  1] .Cells [1]。价值=博士[PartyName]的ToString()。
            dataGridViewParties.Rows [dataGridViewParties.Rows.Count  -  1] .Cells [2]。价值=博士[PartyAddress]的ToString();
            dataGridViewParties.Rows [dataGridViewParties.Rows.Count  -  1] .Cells [3]。价值=博士[PartyState]的ToString();
            dataGridViewParties.Rows [dataGridViewParties.Rows.Count  -  1] .Cells [4]。价值=博士[PartyCity]的ToString()。
            dataGridViewParties.Rows [dataGridViewParties.Rows.Count  -  1] .Cells [5]。价值=博士[PartyPhone]的ToString();
        }

        如果(dataGridViewParties.Rows.Count大于0)
        {
            dataGridViewParties.ClearSelection();
            dataGridViewParties.CurrentCell = NULL;
        }
    }
 

在调试器中我发现,CurrentCell是前DataGridViewParties.CurrentCell =空已空;执行。

如何让一个软件启动后自动删除

此问题http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/c440c4f6-6dfc-47b6-97c0-1ce49c105b64/还涉及,但不提供一个解决方案。

编辑:它的怪异,但它适用于Load事件,我在做它的构造函数。 我想,当第一行被选中,当用户presses向上箭头键,将焦点移动到某个文本框。但在这种情况下,它不工作(第一行呈现蓝色)

 私人无效dataGridViewInwards_KeyDown(对象发件人,KeyEventArgs E)
    {
         如果(e.Key code == Keys.Up和放大器;&安培; dataGridViewParties.SelectedRows.Count> 0安培;&放大器; dataGridViewParties.SelectedRows [0] .Index == 0)
        {
            textBoxPartySearch.Focus();
            dataGridViewParties.ClearSelection();
            dataGridViewParties.CurrentCell = NULL;
        }
        否则,如果(e.Key code == Keys.Up和放大器;&安培; dataGridViewParties.SelectedRows.Count == 0)
        {
            textBoxPartySearch.Focus();
        }
    }
 

解决方案

要实现这一目标随着 ClearSelection ,你将需要设置多了一个属性。

在此尝试的 DataBindingComplete

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

修改的

根据您的意见

您可以修改code为

 如果(e.Key code == Keys.Up和放大器;&安培; dataGridView1.CurrentCell.RowIndex == 0)
{
     this.ActiveControl = textBoxPartySearch;
     dataGridView1.Refresh();
     dataGridView1.ClearSelection();
     dataGridView1.CurrentCell = NULL;
     e.Handled =真实;
}
 

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.

How to get rid of it?

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

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

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.

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

解决方案

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

Try this in the DataBindingComplete

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

EDIT

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