datagrid Info

 

日期:2006-07-19 07:23:52  來源:www.cngr.cn

如何在DataGrid控件中實現編輯、刪除、分類以及分頁操作

減小字體 增大字體

{
if(!IsPostBack)
{
BindGrid();
}
}
上面展現的是一種非常好的技術,當頁面不是PostBack狀態時,就綁定數據。這意味着,一旦頁面被請求數據將被綁定。
繼續看程序:
/// <summary>
/// 這個函數返回關於產品細節的DataSet
/// </summary>
///<returns></returns>
private DataSet GetProductData()
{
///SQLStatement是一個SQL語句(string型的)
string SQLStatement="SELECT Products.ProductID, Products.ProductName, Products.QuantityPerUnit, Products.UnitPrice, "+
"Products.UnitsInStock, Products.UnitsOnOrder, Products.ReorderLevel, Products.Discontinued "+
"FROM Products"; :
///聲明 SqlConnection對象:myConnection
SqlConnection myConnection=new SqlConnection(@"server=(local)\NetSDK;”+
”database=NorthWind;uid=northwind;pwd=northwind;");
///聲明Command對象:myCommand
SqlDataAdapter myCommand = new SqlDataAdapter(SQLStatement,myConnection);
///設置Command命令的類型爲Text類型
myCommand.SelectCommand.CommandType=CommandType.Text;
///創建DataSet對象實例
myDataSet = new DataSet();
///把從表Products返回的數據填充myData
myCommand.Fill(myDataSet, "Products");
///最後返回myDataSet對象
return myDataSet;
}
這段代碼執行給定的SQL語句訪問數據庫,私有函數GetProductData返回一個包含數據記錄的DataSet。下一步,讓我們看如何編輯記錄:
/// <summary>
/// 這個函數只有當用戶點擊Edit按鈕時纔會被激活
/// </summary>
/// <paramname="sender"></param>
/// <paramname="E"></param>
protected void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs E)
{
///找出被選定項目的索引(ItemIndex),並且進一步綁定數據
MyDataGrid.EditItemIndex = (int)E.Item.ItemIndex;
BindGrid();
}
通過上面代碼所附帶的註解大家也能明白MyDataGrid_Edit函數的功能:當用戶點擊Edit按鈕時激活MyDataGrid_Edit函數,並且程序找到所要編輯的記錄的索引,把該索引號分配給DataGrid的EditItemIndex屬性。
如果用戶點擊Cancel按鈕,將調用我們在上面的.aspx文件中提到的MyDataGrid_Cancel函數,程序如果分配給DataGrid屬性 EditItemIndex的值爲-1,就意味着用戶沒有選擇Edit,程序如下:
/// <summary>
/// 用戶點擊Cancel按鈕時激活MyDataGrid函數
/// </summary>
/// <paramname="sender"></param>
/// <paramname="E"></param>
protected void MyDataGrid_Cancel(Object sender, DataGridCommandEventArgs E)
{
MyDataGrid.EditItemIndex = -1;
BindGrid();
}
下面的代碼像我們展現瞭如何從DataGrid中刪除一條選中的記錄。我們知道Web控件DataGrid有一DataKeyField屬性,事實上它就包含了每條記錄的ProductID字段值。您一定會問如何通過DataKeyField屬性得到DataGrid中選中記錄的ProductID值呢?下面這段代碼會讓您釋然的:
-----
int ProductID =(int)MyDataGrid.DataKeys[(int)E.Item.ItemIndex];
-----
MyDataGrid_Delete函數代碼如下:
/// <summary>
///從DataSet中刪除一條記錄
/// </summary>
/// <param name="sender"></param>
/// <param name="E"></param>
protected void MyDataGrid_Delete(Object sender, DataGridCommandEventArgs E)
{
int ProductID =(int)MyDataGrid.DataKeys[(int)E.Item.ItemIndex];
string SQLStatement="Delete Products WHERE ProductID="+ProductID;
string myConnectionString = "server=localhost;uid=sa;pwd=;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();
}
下面的代碼用來更新NorthWind數據庫的產品信息,
我們可以使用下面這項技術檢索值:
-------------------
bool Discon=((CheckBox)E.Item.FindControl("Discontinued")).Checked;
-------------------
這時我們使用FinControl()方法就能得到Discontinued CheckBox的值.
/// <summary>
///更新記錄
/// </summary>
/// <param name="sender"></param>
/// <param name="E"></param>
protected void MyDataGrid_Update(Object sender, DataGridCommandEventArgs E)
{
int ProductID =(int)MyDataGrid.DataKeys[(int)E.Item.ItemIndex];
string ProductName = ((TextBox)E.Item.Cells[3].Controls[0]).Text;
string QuantityPerUnit=((TextBox)E.Item.Cells[4].Controls[0]).Text;
string UnitPrice = ((TextBox)E.Item.Cells[5].Controls[0]).Text;
Int16 UnitsInStock=Int16.Parse(((TextBox)E.Item.Cells[6].Controls[0]).Text);
Int16 UnitsOnOrder=Int16.Parse(((TextBox)E.Item.Cells[7].Controls[0]).Text);
Int16 ReorderLevel=Int16.Parse(((TextBox)E.Item.Cells[8].Controls[0]).Text);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章