Click here to Skip to main content
15,746,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
As I said I would like to know programmable click specific column header in datagridview of C#.

Normally other cells are possible to set focus like belows but how can do for column header?

This is for sorting operation of datagridview by clicking column header but I need programmable clicking.
Please advise me..

What I have tried:

datagridview.CurrentCell = datagridview.Rows[-1].Cell["A"];
Updated 15-May-23 21:29pm

1 solution

Why click? The DataGridView has a Sort method, which accepts a DataGridViewColumn to sort by:
private void MyButton_Click(object sender, EventArgs e)
    if (int.TryParse(MyTextBox.Text, out int colNo))
        myDataGridView.Sort(myDataGridView.Columns[colNo], ListSortDirection.Ascending);

private void MyOtherButton_Click(object sender, EventArgs e)
    if (int.TryParse(MyTextBox.Text, out int colNo))
        myDataGridView.Sort(myDataGridView.Columns[colNo], ListSortDirection.Descending);
Share this answer
Member 14630536 16-May-23 3:55am    
Thanks a lot..
Member 14630536 16-May-23 3:57am    
Okay I see... but if there happen to set focus on a specific columns?
OriginalGriff 16-May-23 6:11am    
No, sorry, that makes no sense - please try to ask it again in a different way.
Member 14630536 16-May-23 20:26pm    
Normally User can click column header and then How can I make this by programmably?
OriginalGriff 17-May-23 0:45am    
Clicking on a column header sorts the data by the column, if that is allowed.
You have a way to programmatically sort the data by a specific column.
So why do you need to click on a column programmatically? What extar function does it do that you don't think you can emulate?

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