DataGrid 控件

用到的DataGrid属性:OnEditCommand,OnUpdateCommand,OnCancelCommand

4.EditCommandColumn

用到的属性:HeaderText,EditText,CancelText,UpdateText,ButtonType

注意:在定义return ds.Tables[“score”].DefaultView表名的时候,一定要和数据库所使用表名一致。在不一直的情况下,Edit和Cancel函数可以正常执行,但在执行Update函数时会出错。

今天的代码:

<%@ Page Language=”C#” ContentType=”text/html” ResponseEncoding=”gb2312″ %>
<%@ Import Namespace=”System.Data” %>
<%@ Import Namespace=”System.Data.OleDb” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
<script runat=server>
OleDbConnection Myconn;
public void Page_Load(Object sender, EventArgs e)
{
// 连接语句
string Myconnstring = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=” + Server.MapPath(“.”) + “..\\data\\score.mdb;”;
Myconn = new OleDbConnection(Myconnstring);
Myconn.Open();

if (!Page.IsPostBack)
{
BindGrid();
}

}

//创建当前数据的默认视图
ICollection CreateTable()
{
string strsel = “select * from score”;
OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strsel, Myconn);
DataSet ds = new DataSet();
MyAdapter.Fill(ds, “score”);
return ds.Tables[“score”].DefaultView;
}
public void BindGrid()
{
score.DataSource = CreateTable();
score.DataBind();
}

//处理Edit命令
public void DataGrid_EditCommand(Object sender, DataGridCommandEventArgs e)
{
score.EditItemIndex = (int)e.Item.ItemIndex;
BindGrid();
}

//处理Cancel命令
public void DataGird_CancelCommand(Object sender, DataGridCommandEventArgs e)
{
score.EditItemIndex = -1;
BindGrid();
}

//处理Update命令
public void DataGrid_UpdateCommand(Object sender, DataGridCommandEventArgs e)
{
//取得已更新数据
string strname = e.Item.Cells[1].Text;
int intmath = Int32.Parse(((TextBox)e.Item.Cells[2].Controls[0]).Text);
int intenglish = Int32.Parse(((TextBox)e.Item.Cells[3].Controls[0]).Text);
int intchinese = Int32.Parse(((TextBox)e.Item.Cells[4].Controls[0]).Text);

//更新数据库
string strupdate=”Update score set s_Math=”+intmath+”,s_English=”+intenglish+”,s_Chinese=”+intchinese+” Where s_name='”+strname+”‘”;
OleDbCommand Mycommand = new OleDbCommand(strupdate, Myconn);
Mycommand.ExecuteNonQuery();

score.EditItemIndex = -1;
BindGrid();
}
</script>
<title>EditCommandColumn</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:DataGrid ID=”score” runat=server HeaderStyle-BackColor=”#aaaadd” AlternatingItemStyle-BackColor=”#addeae” AutoGenerateColumns=false OnEditCommand=”DataGrid_EditCommand” OnCancelCommand=”DataGird_CancelCommand” OnUpdateCommand=”DataGrid_UpdateCommand”>
<Columns>
<asp:EditCommandColumn HeaderText=”Play area” EditText=”Edit” CancelText=”Cancel” UpdateText=”Update” ButtonType=”PushButton”/>
<asp:BoundColumn HeaderText=”Name” DataField=”s_name” ReadOnly=true/>
<asp:BoundColumn HeaderText=”Math” DataField=”s_Math”/>
<asp:BoundColumn HeaderText=”English” DataField=”s_English”/>
<asp:BoundColumn HeaderText=”Chinese” DataField=”s_Chinese”/>
</Columns>
</asp:DataGrid>
</div>
</form>
</body>
</html>
</html>

发表评论

邮箱地址不会被公开。 必填项已用*标注