📄 datagrid.aspx
字号:
<%@ Page Debug="true" Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<title>Modify and Delete Data</title>
<script language="C#" runat="server" >
// Public variable
SqlConnection connection;
SqlCommand command;
SqlDataAdapter adapter;
SqlCommandBuilder commandBuilder;
DataSet dataSet;
private void DataBind()
{
grid1.DataSource = dataSet;
grid1.DataBind();
}
private void PrepareConnection()
{
string dataSource = "Data Source=localhost;";
string security = "user id=sa; password=;";
string initialCatalog = "initial catalog=pubs;";
string cnnString = dataSource + security + initialCatalog;
connection = new SqlConnection(cnnString);
// Create Data Command
string strSql = "select * from [authors]";
command = new SqlCommand(strSql, connection);
// Create Data Adapter and its commands
adapter = new SqlDataAdapter();
adapter.SelectCommand = command;
commandBuilder = new SqlCommandBuilder(adapter);
adapter.UpdateCommand = commandBuilder.GetUpdateCommand();
adapter.DeleteCommand = commandBuilder.GetDeleteCommand();
adapter.InsertCommand = commandBuilder.GetInsertCommand();
}
public void Page_Load()
{
PrepareConnection();
dataSet = new DataSet();
try
{
connection.Open();
adapter.Fill(dataSet);
}
catch(SqlException e)
{
Response.Write(e.ToString());
return;
}
finally
{
connection.Close();
}
if(!IsPostBack)
DataBind();
}
void OnUpdateCommand(object sender, DataGridCommandEventArgs arg)
{
// Modify dataset
DataRow dataRow = dataSet.Tables[0].Rows[arg.Item.ItemIndex];
dataRow["au_fname"] = ((TextBox)arg.Item.Cells[0].Controls[0]).Text;
dataRow["au_lname"] = ((TextBox)arg.Item.Cells[1].Controls[0]).Text;
dataRow["phone"] = ((TextBox)arg.Item.Cells[2].Controls[0]).Text;
dataRow["city"] = ((TextBox)arg.Item.Cells[3].Controls[0]).Text;
dataRow["state"] = ((DropDownList)arg.Item.Cells[4].Controls[1]).SelectedItem.Text;
// Update database
PrepareConnection();
try
{
connection.Open();
adapter.Update(dataSet.Tables[0]);
}
catch(SqlException e)
{
Response.Write(e.ToString());
return;
}
finally
{
connection.Close();
}
dataSet.Tables[0].AcceptChanges();
// Update Display
grid1.EditItemIndex = -1;
DataBind();
}
void OnCancelCommand(Object sender, DataGridCommandEventArgs e)
{
grid1.EditItemIndex = -1;
DataBind();
}
void OnEditCommand(Object sender, DataGridCommandEventArgs e)
{
grid1.EditItemIndex = e.Item.ItemIndex;
DataBind();
// Set the selected item in the dropdownlist
string state =((Label)e.Item.Cells[4].Controls[1]).Text;
DropDownList list = (DropDownList)grid1.Items[grid1.EditItemIndex].Cells[4].Controls[1];
foreach(ListItem item in list.Items)
{
if(item.Text == state)
{
item.Selected = true;
break;
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<h3>使用DataGrid编辑与修改数据</h3>
<asp:DataGrid id="grid1" runat="server" AutoGenerateColumns="false"
OnEditCommand="OnEditCommand" OnCancelCommand="OnCancelCommand"
OnUpdateCommand="OnUpdateCommand">
<Columns>
<asp:BoundColumn DataField="au_fname" HeaderText="作者姓" />
<asp:BoundColumn DataField="au_lname" HeaderText="作者名" />
<asp:HyperLinkColumn DataNavigateUrlField ="phone" DataTextField="phone" HeaderText="电话" />
<asp:BoundColumn DataField="city" HeaderText="城市" />
<asp:TemplateColumn HeaderText="州">
<ItemTemplate>
<asp:Label Text='<%# DataBinder.Eval(Container.DataItem, "state") %>' runat="server"/>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server">
<asp:ListItem value="CA"/>
<asp:ListItem value="IN"/>
<asp:ListItem value="KS"/>
<asp:ListItem value="MD"/>
<asp:ListItem value="MI"/>
<asp:ListItem value="OR"/>
<asp:ListItem value="TN"/>
<asp:ListItem value="UT"/>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn EditText="修改" CancelText="取消" UpdateText="更新" HeaderText="命令"/>
</Columns>
</asp:DataGrid>
</form>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -