if(!Discon)
{
result=0;
}
else
{
result=1;
}
string SQLStatement="UPDATE Products "+
"SET ProductName=''"+ProductName+"'', "+
"QuantityPerUnit=''"+QuantityPerUnit+"'', "+
"UnitPrice ="+UnitPrice.Substring(UnitPrice.IndexOf("¥")+1)+", "+
"UnitsInStock ="+UnitsInStock+", "+
"UnitsOnOrder ="+UnitsOnOrder+", "+
"ReorderLevel ="+ReorderLevel+", "+
"Discontinued ="+result+
" WHERE ProductID ="+ProductID;
string myConnectionString = "server=localhost;uid=xjb;pwd=xjb;database=Northwind";
SqlConnection myConnection = new SqlConnection(myConnectionString);
SqlCommand myCommand = new SqlCommand(SQLStatement,myConnection);
myCommand.CommandTimeout = 15;
myCommand.CommandType = CommandType.Text;
try
{
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
}
catch(Exception ee)
{
throw ee ;
}
MyDataGrid.EditItemIndex = -1;
BindGrid();
}
接下来的BindGrid()调用私有函数GetProductData取得DataSet对象并绑定到DataGrid控件。
/// <summary>
/// 接受数据库数据并再次绑定
/// </summary>
protected void BindGrid()
{
MyDataGrid.DataSource=GetProductData().Tables["Products"].DefaultView;
MyDataGrid.DataBind();
}
用户在DataGrid中向前或向后移动时激活MyDataGrid_PageIndexChanged事件,因为DataGrid 不能自动的获取新页的索引号,所以我们只能手动取得索引号。
/// <summary>
/// 分页操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void MyDataGrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
MyDataGrid.CurrentPageIndex=e.NewPageIndex;
BindGrid();
}
用户在任何时候想对数据分类时,就激活下面的Sort_Grid事件。例如,如果用户点击field headers,事件就将被激活,并且把数据分成我们想要的分类。 我们需要DataView对象去为e.SortExpression.ToString()方法分类,返回的是被点击域标题的分类。
/// <summary>
/// 分类
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Sort_Grid(Object sender, DataGridSortCommandEventArgs e)
{
DataView dv= new DataView(GetProductData().Tables["Product