Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
is it possible to somehow set the DataGridView so that no row is selected at startup?

What I have tried:

I don't know if it has it in Properties, for example. Or I have to solve it somehow more complicated.

Thanks for the advice and tips

David
Posted
Updated 29-Aug-22 23:16pm

1 solution

There isn't a way to automate it, no - but it's pretty easy to do. Just call the DataGridView.ClearSelection Method (System.Windows.Forms) | Microsoft Docs[^] once you have set the DataSource:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    try
        {
        using (SqlDataAdapter da = new SqlDataAdapter("Select * from MyTable", con))
            {
            using (DataTable dt = new DataTable())
                {
                da.Fill(dt);
                myDataGridView.DataSource = dt;
                myDataGridView.ClearSelection();   
                }
            }
        }
    catch (Exception ex)
        {
        Debug.WriteLine(ex.ToString());
        }
    }
 
Share this answer
 
v2
Comments
dejf111 30-Aug-22 7:31am    
ClearSelection was very good advice. Thank you so much!
OriginalGriff 30-Aug-22 7:38am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900