📄 datagrid4.aspx
字号:
<% @ Page Language="C#" %>
<% @ Import Namespace="System.Data" %>
<% @ Import Namespace="System.Data.OleDb" %>
<Script Language="C#" Runat="Server">
OleDbConnection MyConn;
public void Page_Load(Object src,EventArgs e)
{
//连接语句
string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0;
Data Source="+Server.MapPath(".")+"..\\db1.mdb;";
MyConn = new OleDbConnection(MyConnString);
MyConn.Open();
if(!Page.IsPostBack)
{
BindGrid();
}
}
ICollection CreateTable()
{
string strSel = "select * from Score";
DataSet ds = new DataSet();
OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel,MyConn);
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 DataGrid_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 intChinese = Int32.Parse(((TextBox)e.Item.Cells[2].Controls[0]).Text);
int intMath = Int32.Parse(((TextBox)e.Item.Cells[3].Controls[0]).Text);
int intEnglish = Int32.Parse(((TextBox)e.Item.Cells[4].Controls[0]).Text);
//更新数据库中的数据
string strUpdate = "Update Score Set
Chinese="+intChinese+",Math="+intMath+",English="+intEnglish+" Where Name='"+strName+"'";
OleDbCommand MyComm = new OleDbCommand(strUpdate,MyConn);
MyComm.ExecuteNonQuery();
score.EditItemIndex = -1;
BindGrid();
}
</script>
<html>
<head>
<title></title>
</head>
<body>
<form runat="server">
<center>
<b>EditCommandColumn实例</b>
<asp:DataGrid id="score" runat="server"
HeaderStyle-BackColor="#aaaadd"
AlternatingItemStyle-BackColor="#eeeeee"
AutoGenerateColumns="False"
OnEditCommand="DataGrid_EditCommand"
OnUpdateCommand="DataGrid_UpdateCommand"
OnCancelCommand="DataGrid_CancelCommand"
>
<Columns>
<asp:EditCommandColumn HeaderText="操作区"
EditText="编辑"
UpdateText="更新"
CancelText="取消"
ButtonType="PushButton"
/>
<asp:BoundColumn HeaderText="name" DataField="Name" ReadOnly="True" />
<asp:BoundColumn HeaderText="chinese" DataField="Chinese" />
<asp:BoundColumn HeaderText="math" DataField="Math" />
<asp:BoundColumn HeaderText="english" DataField="English" />
</Columns>
</asp:DataGrid>
</center>
</form>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -