先放置一个隐藏的textbox, 并设置两个变量标记一下:
private bool inteditmode=false;
private bool intupdatemode=false;
..............
.............
//数据排序
private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
try
{
DataGrid.HitTestInfo hittest=dataGrid1.HitTest(e.X,e.Y);
if(hittest.Type==DataGrid.HitTestType.ColumnHeader)
{
txtedit.Visible=false;
inteditmode=false;
intupdatemode=false;
DataTable datatable =(DataTable)dataGrid1.DataSource;
DataView dataview =datatable.DefaultView;
string columnname=datatable.Columns[hittest.Column].ColumnName;
if(dataview.Sort==columnname)
dataview.Sort=columnname+"DESC";
else
dataview.Sort=columnname;
}
}
catch(Exception er)
{
MessageBox.Show(er.Message);
}
}
//数据编辑
private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
{
inputPanel1.Enabled=true;
try
{
if(!intupdatemode)
{
if(inteditmode && !dataGrid1.CurrentCell.Equals(editcell))
{
intupdatemode=true;
dataGrid1.Visible=false;
DataGridCell currentcell =dataGrid1.CurrentCell;
dataGrid1[editcell.RowNumber,editcell.ColumnNumber]=txtedit.Text;
dataGrid1.CurrentCell=currentcell;
dataGrid1.Visible=true;
intupdatemode=false;
txtedit.Visible=false;
inteditmode=false;
}
editcell=dataGrid1.CurrentCell;
txtedit.Text =(string)dataGrid1[editcell.RowNumber,editcell.ColumnNumber];
Rectangle cellpos=dataGrid1.GetCellBounds(editcell.RowNumber,editcell.ColumnNumber);
txtedit.Left=cellpos.Left-1;
txtedit.Top =cellpos.Top +dataGrid1.Top-1;
txtedit.Width=cellpos.Width+2;
txtedit.Height=cellpos.Height+2;
txtedit.Visible=true;
inteditmode=true;
count();
}
}
catch(Exception e3)
{
MessageBox.Show(e3.Message);
inputPanel1.Enabled=false;
}
}