Click here to Skip to main content
15,891,787 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello
When a column heading is clicked in a DataGridView, the default behavior is to order the grid rows based on the clicked column.I need to disable This..plz help
Posted

Try this

C#
foreach (DataGridViewColumn column in dataGridView.Columns)
{
    column.SortMode = DataGridViewColumnSortMode.NotSortable;
}
 
Share this answer
 
Comments
ridoy 1-Aug-13 8:14am    
good one,+5
ErBhati 1-Aug-13 9:13am    
Where i put this code
in form_load it not works
i found solutions

if (e.RowIndex >= 0)
 
Share this answer
 
v2
Comments
Ali Kweyu 27-May-22 14:38pm    
Thanks I did this and it works
if (guna2DataGridViewstudent.Columns[e.ColumnIndex].Name == "Delete")
{
try
{
if (e.RowIndex >= 0)
{
DialogResult confirm = MessageBox.Show("Are you sure you want to delete ", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (confirm == DialogResult.Yes)
{
int id;
id = Convert.ToInt32(guna2DataGridViewstudent.Rows[e.RowIndex].Cells["Student_ID"].Value);


connect.openConnect();
try
{
SqlCommand command = new SqlCommand(" DELETE FROM Students WHERE Student_ID=@idtype", connect.GetConnection);
//command.Parameters = Add("", SqlDbType.VarChar).Value.TypeName;
command.Parameters.Add("@idtype", SqlDbType.VarChar).Value = id;
int result = command.ExecuteNonQuery();
if (result > 0)
{

MessageBox.Show("data deleted");
showTable();

}
else
{
MessageBox.Show("data fail");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
connect.closeConnect();
}
}
}
}catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
If you can extend the DatagridView you can override the Sort method with am empty one. This disables Sort for the DatagridView entirely.
C#
public override void Sort(DataGridViewColumn dataGridViewColumn, ListSortDirection direction)
{
      //base.Sort(dataGridViewColumn, direction);
}

Note: You cannot even programmatically sort any column.
 
Share this answer
 

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