<asp:Gridview ID="Gridview1" Runat="server" DataSourceID="SqlDataSource1" DataKeyNames="CustomerID" AutoGenerateColumns="False" ShowFooter="True">
<Columns>
?。糰sp:TemplateField>
?。糏temTemplate>
?。糰sp:Label ID="CustomerIDLabel" Runat="Server"><%# Eval("CustomerID") %></asp:Label>
?。?ItemTemplate>
?。糉ooterTemplate>
?。糰sp:TextBox ID="CustomerIDTextBox" Runat="server"></asp:TextBox>
?。?FooterTemplate>
?。?asp:TemplateField>
?。糰sp:TemplateField>
?。糏temTemplate>
?。糰sp:Label ID="CompanyNameLabel" Runat="Server"><%# Eval("CompanyName") %></asp:Label>
?。?ItemTemplate>
?。糉ooterTemplate>
?。糰sp:TextBox ID="CompanyNameTextBox" Runat="server"></asp:TextBox>
?。?FooterTemplate>
?。?asp:TemplateField>
?。糰sp:TemplateField>
?。糉ooterTemplate>
?。糰sp:DropDownList ID="ContactTitleDropDownList" Runat="server" DataSourceID="SqlDataSource2" DataTextField="ContactTitle" DataValueField="ContactTitle">
?。?asp:DropDownList>
?。糰sp:SqlDataSource ID="SqlDataSource2" Runat="server" SelectCommand="SELECT DISTINCT [ContactTitle] FROM [Customers]"
ConnectionString="server=localhost;uid=sa;password=xxx;database=northwind">
?。?asp:SqlDataSource>
?。糰sp:Button ID="Button1" Runat="server" Text="Add" OnClick="Button1_Click" />
?。糰sp:Button ID="CancelButton1" Runat="server" Text="Cancel" OnClick="CancelButton1_Click" />
?。?FooterTemplate>
?。糏temTemplate>
?。糰sp:DropDownList ID="ContactTitleDropDown" SelectedValue=’<%# Bind("ContactTitle") %>’ Runat="Server" DataSourceID="SqlDataSource3" DataTextField="ContactTitle" DataValueField="ContactTitle" ></asp:DropDownList>
?。糰sp:SqlDataSource ID="SqlDataSource3" Runat="server" SelectCommand="SELECT DISTINCT [ContactTitle] FROM [Customers]"
ConnectionString="server=localhost;uid=sa;password=xxxx;database=northwind" EnableCaching="True">
?。?asp:SqlDataSource>
?。?ItemTemplate>
?。?asp:TemplateField>
</Columns>
</asp:Gridview>
<script runat="server">
void CancelButton1_Click(object sender, EventArgs e)
{
Gridview1.ShowFooter = false;
}
void AddButton1_Click(object sender, EventArgs e)
{
Gridview1.ShowFooter = true;
}
//點(diǎn)add按鈕時(shí),將新增的記錄更新到數據庫中去
void Button1_Click(object sender, EventArgs e)
{
TextBox customerID = Gridview1.FooterRow.FindControl("CustomerIDTextBox") as TextBox;
TextBox companyName = Gridview1.FooterRow.FindControl("CompanyNameTextBox") as TextBox;
DropDownList ContactTitle = Gridview1.FooterRow.FindControl("ContactTitleDropDownList") as DropDownList;
SqlDataSource1.InsertParameters["CustomerID"].DefaultValue = customerID.Text;
SqlDataSource1.InsertParameters["CompanyName"].DefaultValue = companyName.Text;
SqlDataSource1.InsertParameters["ContactTitle"].DefaultValue=ContactTitle.SelectedValue;
SqlDataSource1.Insert();
}
</script>
?。糰sp:SqlDataSource ID="SqlDataSource1" Runat="server"
InsertCommand="INSERT INTO [Customers] ([CustomerID], [CompanyName], [ContactTitle]) VALUES (@CustomerID, @CompanyName, @ContactTitle)"
SelectCommand="SELECT top 5 [CustomerID], [CompanyName], [ContactTitle] FROM [Customers]"
ConnectionString="server=localhost;uid=sa;password=XXXXX;database=northwind">
<InsertParameters>
<asp:Parameter Type="String" Name="CustomerID"></asp:Parameter>
<asp:Parameter Type="String" Name="CompanyName"></asp:Parameter>
<asp:Parameter Type="String" Name="ContactTitle"></asp:Parameter>
</InsertParameters>
</asp:SqlDataSource>
其中,必須設置insertcommand和selectcommand屬性,設置數據提取和插入的語(yǔ)句,并且要設置好insertparameters集合中,各字段的類(lèi)型和名稱(chēng)即可。
2、一次性更新所有的Gridview記錄
我們經(jīng)常會(huì )遇到這樣的情況,在Gridview中列出的所有記錄中,有時(shí)要同時(shí)修改多條記錄,并且將其保存到數據庫中去。那么在Gridview中應該如何實(shí)現呢?在Gridview中,有兩種實(shí)現的方法,下面分別進(jìn)行介紹:
先來(lái)看下第一種方法,本方法是使用sqldatasource來(lái)更新所有記錄,但這個(gè)方法比較慢,因為每更新一條記錄都要建立數據連接并執行updatecommand,會(huì )影響性能。其主要代碼如下:
<script runat="server">
void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < Gridview1.Rows.Count; i++)
{
GridviewRow row = Gridview1.Rows[i];
SqlDataSource1.UpdateParameters[0].DefaultValue = ((TextBox)row.Cells[0].FindControl("TextBox2")).Text;
SqlDataSource1.UpdateParameters[1].DefaultValue = ((TextBox)row.Cells[1].FindControl("TextBox3")).Text;
SqlDataSource1.UpdateParameters[2].DefaultValue = Gridview1.DataKeys[i].Value.ToString();
SqlDataSource1.Update();
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
?。糵orm id="form1" runat="server">
?。糳iv>
?。糰sp:Gridview ID="Gridview1" Runat="server" DataSourceID="SqlDataSource1" DataKeyNames="CustomerID" AutoGenerateColumns="False">
?。糃olumns>
?。糰sp:TemplateField SortExpression="CustomerID" HeaderText="CustomerID">
?。糏temTemplate>
?。糰sp:TextBox Runat="server" Text=’<%# Bind("CustomerID") %>’ ID="TextBox1"></asp:TextBox>
?。?ItemTemplate>
?。?asp:TemplateField>
?。糰sp:TemplateField SortExpression="CompanyName" HeaderText="CompanyName">
?。糏temTemplate>
?。糰sp:TextBox Runat="server" Text=’<%# Bind("CompanyName") %>’ ID="TextBox2"></asp:TextBox>
?。?ItemTemplate>
?。?asp:TemplateField>
?。糰sp:TemplateField SortExpression="ContactName" HeaderText="ContactTitle">
?。糏temTemplate>
?。糰sp:TextBox Runat="server" Text=’<%# Bind("ContactTitle") %>’ ID="TextBox3"></asp:TextBox>
?。?ItemTemplate>
?。?asp:TemplateField>
?。?Columns>
?。?asp:Gridview>
<asp:SqlDataSource ID="SqlDataSource1" Runat="server"
SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName], [ContactTitle] FROM [Customers]"
UpdateCommand="UPDATE [Customers] SET [CompanyName] = @CompanyName, [ContactTitle] = @ContactTitle WHERE [CustomerID] = @CustomerID"
ConnectionString="server=localhost;uid=sa;password=xxxx;database=northwind">
<UpdateParameters>
<asp:Parameter Type="String" Name="CompanyName"></asp:Parameter>
<asp:Parameter Type="String" Name="ContactTitle"></asp:Parameter>
<asp:Parameter Type="String" Name="CustomerID"></asp:Parameter>
</UpdateParameters>
</asp:SqlDataSource>
<asp:Button ID="Button1" Runat="server" Text="Button" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AppConnectionString1"].ConnectionString);
SqlCommand command = new SqlCommand("SELECT [CustomerID], [CompanyName], [ContactName], [ContactTitle] FROM [Customers]", con);
con.Open();
Gridview1.DataSource = command.ExecuteReader();
Gridview1.DataBind();
con.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
StringBuilder query = new StringBuilder();
for (int i = 0; i < Gridview1.Rows.Count; i++)
{
GridviewRow row = Gridview1.Rows[i];
string value1 = ((TextBox)row.Cells[0].FindControl("TextBox2")).Text.Replace("’", "’’");
string value2 = ((TextBox)row.Cells[1].FindControl("TextBox3")).Text.Replace("’", "’’");
string value3 = Gridview1.DataKeys[i].Value.ToString();
query.Append("UPDATE [Customers] SET [CompanyName] = ’").Append(value1).Append("’ , [ContactTitle] = ’")
.Append(value2).Append("’ WHERE [CustomerID] = ’").Append(value3).Append("’;\n");
}
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AppConnectionString1"].ConnectionString);
SqlCommand command = new SqlCommand(query.ToString(), con);
con.Open();
command.ExecuteNonQuery();
con.Close();
}
}
<connectionStrings>
<add name="NorthwindConnectionString" connectionString="Data Source=LIAO;Initial Catalog=Northwind;User ID=sa;Password=xxxx" providerName="System.Data.SqlClient"/>
</connectionStrings>
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AppConnectionString1"].ConnectionString);
聯(lián)系客服