(1)将DataGrid1的AllowSorting和AutoGenerateColumns都设为true
(2)在DataGrid1_SortCommand中写
string mystr2;
if (TextBox1.Text=="")
{
TextBox1.Text=e.SortExpression.ToString();
mybind(e.SortExpression.ToString());
return;
}
mystr2=e.SortExpression.ToString();
if (TextBox1.Text!=mystr2)
{
mybind(e.SortExpression.ToString());
TextBox1.Text=e.SortExpression.ToString();
return;
}
if (TextBox1.Text==mystr2)
{
if (TextBox1.Text.LastIndexOf("mystr2")==-1)
{
mybind(mystr2+" "+"DESC");
TextBox1.Text=mystr2+" "+"DESC";
return;
}
else
{
mybind(mystr2);
mybind(mystr2+" "+"ASC");
TextBox1.Text=mystr2+" "+"ASC";
}
}
(3)自定义函数
private void mybind(string sort)
{
DataGrid1.PagerStyle.Mode=PagerMode.NumericPages;
string constr=System.Configuration.ConfigurationSettings.AppSettings["SQLConnectionString"];
String command="select * from comeout order by "+sort+"";
OleDbConnection myConn = new OleDbConnection(constr) ;
myConn.Open ( ) ;
//创建一个 DataSet
DataSet myDataSet = new DataSet ( ) ;
//用 OleDbDataAdapter 得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter (command,constr ) ;
//把Dataset绑定person数据表
myCommand.Fill (myDataSet,command) ;
//关闭此OleDbConnection
//DataRow dr = ds.Tables["comeout"].Rows[1];
myConn.Close () ;
//DataGrid1.AutoGenerateColumns = false;
DataGrid1.Columns.Clear();
DataGrid1.DataSource = myDataSet.Tables[0];
DataGrid1.DataBind();
}
872

被折叠的 条评论
为什么被折叠?



