GridView 中如何实现编辑 删除(在编辑状态中 嵌套DropDownList)
时间:2010-11-23 来源:吃螺丝
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CellPadding="4"
ForeColor="#333333" GridLines="None" Width="650px" onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"
FooterStyle-BackColor="#990000" Font-Bold="true" DataKeyNames="UserID" onrowdatabound="GridView1_RowDataBound">
<columns>
<asp:BoundField DataField="UserID" HeaderText="UserID" ReadOnly="true" />
<asp:BoundField DataField="UserName" HeaderText="UserName" />
<asp:BoundField DataField="UserPwd" HeaderText="UserPwd" />
<%--<asp:BoundField DataField="Gender" HeaderText="Gender" />--%>
<asp:TemplateField HeaderText="Gender">
<EditItemTemplate>
<asp:DropDownList ID="Gender" runat="server">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<%# Eval("Gender")%>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="PhoneNumber" HeaderText="PhoneNumber" />
<asp:BoundField DataField="Address" HeaderText="Address" />
<asp:CommandField HeaderText="Select" ShowSelectButton="True" />
<asp:CommandField HeaderText="Edit" ShowEditButton="true" />
<asp:CommandField HeaderText="Delete" ShowDeleteButton="true" />
</columns>
<rowstyle forecolor="#000066" />
<selectedrowstyle backcolor="#669999" font-bold="true" forecolor="White" />
<pagerstyle backcolor="White" forecolor="#000066" horizontalalign="Left" />
<headerstyle backcolor="#006699" font-bold="true" forecolor="White" />
</asp:GridView>
后台部分代码代码: //关键是几个事件中的代码
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sqlstr = "delete from [Kiros].[Static_User] where UserID=" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "";
MyDBHelper Kiros = new MyDBHelper();
Kiros.Excute_Command(sqlstr);
GridView_Bind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex; //显示编辑状态
GridView_Bind();
}
////更新事件
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string ProcNameModify = "[Kiros].[ModifyUserInfo]";
MyDBHelper Kiros2 = new MyDBHelper();
string U1 = GridView1.Rows[e.RowIndex].Cells[0].Text;
string U2 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
string U3 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
bool U4 = Convert.ToBoolean(((DropDownList)(GridView1.Rows[e.RowIndex].Cells[3].FindControl("Gender"))).SelectedValue);
string U5 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim();
string U6 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim();
UserInfo UI2 = new UserInfo(U1, U2, U3, U4, U5, U6);
UserInfo_Params UP2 = new UserInfo_Params();
Kiros2.Proc_InsertRows_Excute(ProcNameModify, UP2.UserInfoToParams(UI2));
GridView1.EditIndex = -1;
GridView_Bind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1; //关闭编辑状态
GridView_Bind();
}
protected void GridView_Bind() //给GridView绑定数据源
{
string ProcNameUI = "[Kiros].[ShowUserInfo]";
MyDBHelper Kiros = new MyDBHelper();
DataSet DS = new DataSet();
DS = Kiros.Proc_ShowRows_DataSet(DS, ProcNameUI);
GridView1.DataSource = DS;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit))
{
DropDownList dll = (DropDownList)e.Row.FindControl("Gender");
dll.Items.Add(new ListItem("Male", "true"));
dll.Items.Add(new ListItem("Female", "false"));
}
}
以上要注意的是 RowDataBound事件,在编辑状态中给DropDownList绑定值的时候,一定要加上判断语句:
if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit))
来判断是否是编辑状态,GridView有几种状态,可以查询MSDN