Remove blue color bar from DataGridView WinForms

When I populate the DataGridView row row by row (by calling the add function), the top row turns blue. It is not selected because I also tried ClearSelection (), but it did not work. This gives the wrong illusion of selecting the first row.

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; performed.

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: , 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();
        }
    }
+1
5

ClearSelection,

DataBindingComplete

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

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

, .

, , , .

, , . , , , . , click, :

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

, ClearSelection , , , datagrid, , , . , ...

+4

ClearSelection() VisibleChanged :

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

, : DataGridView dataGridView.ClearSelection().

, :

DataGridViews, . . , , DataGridView ClearSelection(), .

DataGridView , , , . , , DataGridView, ClearSelection() , .

+1

Form.Shown :

dataGridView1.ClearSelection();
dataGridView1.CurrentCell = null;
+1
source

All Articles