Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
446 views
in Technique[技术] by (71.8m points)

c# - Remove blue colored row from DataGridView WinForms

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();
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I tried the above methods but nothing worked for me.

Finally, I just decided to set the default colors for selected cells to be the same as non-selected cells.

Then, in the cell click method, I set them back. That way nothing appears selected until its clicked, which was sufficient for my application. Here's the code I used in the click method to set everything back to normal:

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

Annoyingly, the ClearSelection method actually works just fine if I put it in a button, but if I create the control which contains the datagrid, load some data into it, and then try to clear it right then, it doesn't work. I wish I knew why...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...