绑定数据:
sda.Fill(ds,"mydevice");
ds.Tables[0].DefaultView.Sort=this.sortstr;
//this.deviceTabControl.Focus();
this.DeviceGrid.DataSource=ds.Tables[0].DefaultView;
//this.DeviceGrid.Refresh();
if(ds.Tables[0].Rows.Count -1<this.currentindex ){
this.DeviceGrid.CurrentRowIndex=0;
//this.DeviceGrid.Select(0);
}
else{
this.DeviceGrid.CurrentRowIndex=this.currentindex;
//this.DeviceGrid.Select(this.currentindex);
}
处理排序:
private void DeviceGrid_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) {
if(e.Button.Equals(MouseButtons.Left)){
if(this.DeviceGrid .HitTest(e.X,e.Y).Row==-1){
int index=this.DeviceGrid .HitTest(e.X,e.Y).Column ;
if(index==-1){
return;
}
DataView tmpds=(DataView)DeviceGrid.DataSource;
string tmpsort=tmpds.Table.Columns[index].ColumnName;
if(this.sortstr==""){
this.sortstr=tmpsort+" asc";
}
else{
if(this.sortstr.StartsWith(tmpsort)){
if(this.sortstr.EndsWith(" asc")){
this.sortstr=tmpsort+" desc";
}
else{
this.sortstr=tmpsort+" asc";
}
}
else{
this.sortstr=tmpsort+" asc";
}
}
}
//MessageBox.Show(sortstr);
}
}