如何在GridView中将下拉列表与数据库绑定
问题描述:
我正在使用Visual Studio2008.我有一个带有4个templatefield的gridview.我想将下拉列表与数据库绑定.这是我的源代码.
I m ussing Visual studio2008.I have a gridview with 4 templatefield .i wants to bind the dropdown with database. here is my sourcecode.
<asp:GridView ID="GridView1" runat="server"
onselectedindexchanged="GridView1_SelectedIndexChanged"
onrowdatabound="GridView1_RowDataBound" DataKeyNames="Group_ID" AutoGenerateColumns="false">
<columns>
<asp:TemplateField HeaderText="Gender"><itemtemplate>
<asp:DropDownList ID="DropDownList1" runat="server" >
<asp:ListItem >Mr
<asp:ListItem>Ms
</itemtemplate>
<asp:TemplateField HeaderText="Name">
<itemtemplate>
<asp:TextBox ID="nametxt" runat="server">
</itemtemplate>
<asp:TemplateField HeaderText="MobileNo">
<itemtemplate>
<asp:TextBox ID="mobilenotxt" runat="server">
</itemtemplate>
<asp:TemplateField HeaderText="EmailID">
<itemtemplate>
<asp:TextBox ID="Emailidtxt" runat="server">
</itemtemplate>
<asp:TemplateField HeaderText="City">
<itemtemplate>
<asp:TextBox ID="Citytxt" runat="server">
</itemtemplate>
<asp:TemplateField HeaderText="Group">
<itemtemplate>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true" DataTextField=''<%# Bind("Group_Name") %>'' DataValueField=''<%# Bind("Group_ID") %>''>
</itemtemplate>
</columns>
<footerstyle backcolor="#CCCC99" />
<rowstyle backcolor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<pagerstyle backcolor="#F7F7DE" forecolor="Black" horizontalalign="Right" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<alternatingrowstyle backcolor="White" />
我需要的是何时页面将被放格,gridview将显示带有相应项的下拉列表.其他文本框应为空.
我的代码背后是.我希望我的页面像
My need is when page will be loded gridview will display with dropdown with respectiveitem. other textboxes shouldbe empty.
my codebehind is .i want my page like
if (e.Row.RowType == DataControlRowType.DataRow)
{
obj.da = new SqlDataAdapter("select Group_ID, Group_Name from dbo.Group_Details where MobileNo=''" + Session["MobNo"].ToString() + "''", obj.cn);
ds.Reset();
obj.da.Fill(ds);
DropDownList groupdwn = (DropDownList)e.Row.FindControl("DropDownList2");
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
groupdwn.DataSource = ds;
groupdwn.DataValueField = "Group_ID";
groupdwn.DataTextField = "Group_Name";
groupdwn.DataBind();
groupdwn.Items.Insert(0, new ListItem("Select", "0"));
}
else
{
groupdwn.DataSource = null;
groupdwn.DataBind();
groupdwn.Items.Insert(0, new ListItem("Select", "0"));
}
请给我答案
plz provide me the answer
答
设计代码:
Design Code:
<asp:TemplateField HeaderText="Status">
<itemtemplate>
<asp:DropDownList ID="ddlstatus" runat="server" Height="16px" Width="55px">
<asp:ListItem>InActive
<asp:ListItem>Active
</itemtemplate>
在aspx.cs文件中
In aspx.cs file
foreach (GridViewRow row in gvuseconfirm .Rows)
{
string id = row.Cells[1].Text.ToString ();
string name=row.Cells[3].Text.ToString ();
string dropDownListText = ((DropDownList)row.FindControl("ddlstatus")).SelectedItem.Value;
if (dropDownListText == "Active")
{
SqlCommand cmd = new SqlCommand("update strUserregistration set struserstatus=''" + dropDownListText + "''where intuserid=''" + id + "''", (SqlConnection)Application.Get("con"));
cmd.ExecuteNonQuery();
lblmessage.Text = name+" "+ "Is Activated";
}
else
{
SqlCommand cmd = new SqlCommand("update strUserregistration set struserstatus=''" + dropDownListText + "''where intuserid=''" + id + "''", (SqlConnection)Application.Get("con"));
cmd.ExecuteNonQuery();
lblmessage.Text = name+" "+ "Is InActive";
}
}
试试这个
问候,
Anilkumar.D
Try this
Regards,
Anilkumar.D