我必须在网格视图中添加一个下拉列表并与数据库绑定。
代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet1TableAdapters.emplTableTableAdapter tb;
tb = new DataSet1TableAdapters.emplTableTableAdapter();
DataTable dt = new DataTable();
dt = tb.GetEmpID();
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "empId";
DropDownList1.DataValueField = "empId";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("--Select Employee ID--", ""));
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DataSet1TableAdapters.emplTableTableAdapter tb;
tb = new DataSet1TableAdapters.emplTableTableAdapter();
DataTable dt = new DataTable();
dt = tb.GetGrid(int.Parse(DropDownList1.SelectedValue));
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
//Set the edit index.
GridView1.EditIndex = e.NewEditIndex;
//Bind data to the GridView control.
BindData();
}
private void BindData()
{
DataSet1TableAdapters.emplTableTableAdapter tb;
tb = new DataSet1TableAdapters.emplTableTableAdapter();
DataTable dt = new DataTable();
dt = tb.GetGrid(int.Parse(DropDownList1.SelectedValue));
GridView1.DataSource = dt;
GridView1.DataBind();
}
ASPX:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> </asp:DropDownList>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateEditButton="True" OnRowEditing="GridView1_RowEditing">
<Columns>
<asp:BoundField DataField="empId" HeaderText="Employee ID" ReadOnly="True" SortExpression="empId" />
<asp:BoundField DataField="empName" HeaderText="Employee Name" ReadOnly="True"
SortExpression="empName" />
<asp:TemplateField HeaderText="Employee Designation" SortExpression="empDesig">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("empDesig") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
表:
现在我得到如下所示的输出:
我需要使用db empDesig列绑定下拉列表。下面是一个示例输出设计,我真正期待的是。
答案 0 :(得分:1)
您可以使用EditItemTemplate,如下所示
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField HeaderText="City">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataTextField="<TextColumnNameInDataSource>" DataValueField="<ValueColumnNameInDataSource>"
/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
在正常模式下,您将看到ItemTemplate中的控件,当您单击编辑并且行处于编辑模式时,您将看到EditItemTemplate中的任何内容。同时,它们都具有DataSource,DataTextField,DataValueField属性,用于填充值dropdownlist和SelectedValue属性,用于显示所选值。
答案 1 :(得分:1)
将DataKey和onrowupdating添加到您的aspx Gridview和
<asp:Gridview id = "dg1" onrowupdating="dg1_RowUpdating" DataKeyNames="ID"
<Columns>
<asp:TemplateField HeaderText="City">
<EditItemTemplate>
<asp:DropDownList ID="ddl_City" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbcity" runat="server" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</Gridview>
然后是代码背后:
protected void dg1_RowUpdating(object sender, GridViewUpdateEventArgs ee)
{
int ID = Convert.ToInt32(dg1.DataKeys[e.RowIndex].Value.ToString());
TextBox contactname= (TextBox)dg1.Rows[e.RowIndex].FindControl("contact");
DropDownList ddl_city= (DropDownList)dg1.Rows[e.RowIndex].FindControl("City");
}
protected void dg1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
}
}
答案 2 :(得分:0)
使用此代码
<asp:GridView ID="GridView1" runat="server">
<asp:BoundField DataField="Contact_Name" HeaderText="Contact Name" />
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> </asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</asp:GridView>