web analytics

How to disable sorting feature of DataGrid?

Options

codeling 1595 - 6639
@2016-01-27 13:26:28

To disable the sorting feature of DataGrid control, you have to set the AllowSorting property to false. If you bind DataGrid to a DataTable using the DataGridTableStyle, you must also set the DataGridTableStyle.AllowSorting to false. See the sample below.

DataTable dt = new DataTable();

private void DGAllowSort_Load(object sender, EventArgs e)

{

 dt.Columns.Add("pid");
 
 dt.Columns.Add("name");
 
 dt.Columns.Add("descr");
 
 dt.Columns.Add("other");

 

 for (int i = 0; i < 20; i++)
 
 {
 
     dt.Rows.Add(i.ToString("0000"), "name" + i.ToString(), "descr" + i.ToString(),

                 "other" + i.ToString());              
 
 }

 

 this.dataGrid1.DataSource = dt;
 
 
 
 DataGridTableStyle ts = new DataGridTableStyle();
 
 ts.GridColumnStyles.Clear();
 
 ts.PreferredRowHeight = 24;
 
 DataGridTextBoxColumn aColumnTextColumn;
 
 ts.MappingName = dt.TableName;
 
 int numCols = dt.Columns.Count;
 
 for (int i = 0; i < numCols; i++)
 
 {
 
     aColumnTextColumn = new DataGridTextBoxColumn();
 
     aColumnTextColumn.MappingName = dt.Columns[i].ColumnName;
 
     aColumnTextColumn.HeaderText = dt.Columns[i].ColumnName;
 
     aColumnTextColumn.Alignment = HorizontalAlignment.Center;
 
     aColumnTextColumn.NullText = "";
 
     aColumnTextColumn.Width = 125;
 
     ts.GridColumnStyles.Add(aColumnTextColumn);
 
 }
 
 this.dataGrid1.TableStyles.Clear();
 
 this.dataGrid1.TableStyles.Add(ts);

 this.dataGrid1.AllowSorting = false;

 ts.AllowSorting = false;

}

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com