而不是boundfiled
,我更喜欢使用TemplateField
它的简单性。
测试示例代码:从页脚添加新记录并更新所选行
默认.aspx:
<asp:GridView ID="gvstatus" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None"
onrowcancelingedit="gvstatus_RowCancelingEdit"
onrowediting="gvstatus_RowEditing" onrowupdating="gvstatus_RowUpdating"
onselectedindexchanged="gvstatus_SelectedIndexChanged" ShowFooter="True"
onrowcommand="gvstatus_RowCommand" Width="600px" AllowPaging="True"
onpageindexchanging="gvstatus_PageIndexChanging">
<Columns>
<asp:TemplateField HeaderText="SrNo " HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID" Visible="false">
<ItemTemplate>
<asp:Label ID="lblid" runat="server" Text='<%# Bind("columnname_id") %>'> </asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EmpName">
<ItemTemplate>
<asp:Label ID="lblEmpName" runat="server" Text='<%# Bind("columnname_EmpName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmpName" runat="server" Text='<%# Bind("columnname_EmpName") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtfEmpName" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="empSalary" >
<ItemTemplate>
<asp:Label ID="lblempSalary" runat="server" Text='<%# Bind("columnname_EmpSalary") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtempSalary" runat="server" Text='<%# Bind("columnname_EmpSalary") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtfempSalary" runat="server" ></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False" ItemStyle-Width="190px">
<ItemTemplate>
<asp:Button ID="btnedit" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit"></asp:Button>
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btnupdate" runat="server" CausesValidation="True"
CommandName="Update" Text="Update"></asp:Button>
<asp:Button ID="btncancel" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel"></asp:Button>
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="btnadd" runat="server" Text="Add" CommandName="Add" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="#A86E07" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#A86E07" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#d9d9d9" />
<AlternatingRowStyle BackColor="White" ForeColor="#A86E07" />
</asp:GridView>
代码隐藏:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
gvBind(); //Bind gridview
}
}
public void gvBind()
{
SqlDataAdapter dap = new SqlDataAdapter("select id, empName,empSalary from myTable", conn);
DataSet ds = new DataSet();
dap.Fill(ds);
gvstatus.DataSource = ds.Tables[0];
gvstatus.DataBind();
}
protected void gvstatus_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//Update the select row from girdview
lblmsg.Text = "";
try
{
GridViewRow row = (GridViewRow)gvstatus.Rows[e.RowIndex];
Label lblid = (Label)gvstatus.Rows[e.RowIndex].FindControl("lblid");
TextBox txtname = (TextBox)gvstatus.Rows[e.RowIndex].FindControl("txtEmpName");
TextBox txtSalary = (TextBox)gvstatus.Rows[e.RowIndex].FindControl("txtempSalary");
string empName = txtname.Text;
string empSalary = txtSalary.Text;
string lblID=lblid.Text;
int result = UpdateQuery(empName, empSalary,lblID);
if (result > 0)
{
lblmsg.Text = "Record is updated successfully.";
}
gvstatus.EditIndex = -1;
gvBind();
}
catch (Exception ae)
{
Response.Write(ae.Message);
}
}
protected void gvstatus_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvstatus.EditIndex = -1;
gvBind();
}
protected void gvstatus_RowEditing(object sender, GridViewEditEventArgs e)
{
lblmsg.Text = "";
gvstatus.EditIndex = e.NewEditIndex;
gvBind();
}
protected void gvstatus_RowCommand(object sender, GridViewCommandEventArgs e)
{
//Add new record to database form girdview footer
if (e.CommandName == "Add")
{
string empName = ((TextBox)gvstatus.FooterRow.FindControl("txtfempName")).Text;
string empSalry = ((TextBox)gvstatus.FooterRow.FindControl("txtfempSalary")).Text;
int result = InsertNewRecord(empName, empSalry);
if (result > 0)
{
lblmsg.Text = "Record is added successfully.";
}
gvstatus.EditIndex = -1;
gvBind();
}
}
public void UpdateQuery(string empName, string empSalary, string lblID)
{
SqlCommand cmd = new SqlCommand("update myTable set empName='" + empName + "',empSalary='" + empSalary + "' where id='" + lblID + "'", conn);
conn.Open();
int temp = cmd.ExecuteNonQuery();
conn.Close();
return temp;
}
public void InsertNewRecord(string empName, string empSalary)
{
SqlCommand cmd = new SqlCommand("your insert query ", conn);
conn.Open();
int temp = cmd.ExecuteNonQuery();
conn.Close();
return temp;
}
http://satindersinght.blogspot.in/2012/08/how-to-addupdate-record-using-gridview.html
如果您觉得有帮助,请标记答案:)