How Can I prevent DataGrid Sorting?
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace MdiDemo
{
public class frmMain : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
private uDataGrid myDataGrid;
private DataSet myDataSet;
public frmMain()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.myDataGrid = new uDataGrid();
((System.ComponentModel.ISupportInitialize)(this.myDataGrid)).BeginInit();
this.SuspendLayout();
//
// myDataGrid
//
this.myDataGrid.AllowSorting = false;
this.myDataGrid.DataMember = "";
this.myDataGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.myDataGrid.Location = new System.Drawing.Point(0, 0);
this.myDataGrid.Name = "myDataGrid";
this.myDataGrid.Size = new System.Drawing.Size(600, 416);
this.myDataGrid.TabIndex = 0;
this.myDataGrid.MouseUp += new System.Windows.Forms.MouseEventHandler(this.myDataGrid_MouseUp);
//
// frmMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(640, 437);
this.Controls.Add(this.myDataGrid);
this.Name = "frmMain";
this.Text = "frmMain";
this.Load += new System.EventHandler(this.frmMain_Load);
((System.ComponentModel.ISupportInitialize)(this.myDataGrid)).EndInit();
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new frmMain());
}
private void MakeDataSet()
{
myDataSet = new DataSet("myDataSet");
DataTable tCust = new DataTable("EastCoastSales");
DataColumn cCustID = new DataColumn("CustID", typeof(int));
DataColumn cCustName = new DataColumn("CustName");
tCust.Columns.Add(cCustID);
tCust.Columns.Add(cCustName);
myDataSet.Tables.Add(tCust);
DataRow newRow1;
for(int i = 1; i < 8; i++)
{
newRow1 = tCust.NewRow();
newRow1["custID"] = i;
tCust.Rows.Add(newRow1);
}
tCust.Rows[0]["custName"] = "custName1";
tCust.Rows[1]["custName"] = "custName2";
tCust.Rows[2]["custName"] = "custName3";
tCust.Rows[3]["custName"] = "custName4";
tCust.Rows[4]["custName"] = "custName5";
tCust.Rows[5]["custName"] = "custName6";
tCust.Rows[6]["custName"] = "custName7";
myDataGrid.DataSource = myDataSet.Tables["EastCoastSales"];
}
private void frmMain_Load(object sender, System.EventArgs e)
{
MakeDataSet();
myDataGrid.AllowRowMove = true;
}
private void myDataGrid_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(myDataGrid.RowHasMoved)
{
for(int i = 0,iCount = myDataSet.Tables["EastCoastSales"].Rows.Count;i < iCount ;i ++)
{
//myDataSet.Tables["EastCoastSales"].Rows[i]["custID"] = i + 1;
}
}
}
}
}