Bind data to Dropdownlist inside gridview
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ShowFooter="true"
DataKeyNames="Slno" OnRowDataBound="GridView1_RowDataBound" Height="202px" Width="347px">
<Columns>
<asp:TemplateField HeaderText="Item Name">
<ItemTemplate>
<table width="100%">
<tr>
<td width="20%">
<asp:Label ID="lblname" runat="server" Text='<%#Eval("Slno") %>' />
</td>
<td width="20%">
<asp:Label ID="lblprice" runat="server" Text='<%#Eval("Price") %>' />
</td>
<td width="20%">
<asp:Label ID="lbltotitems" runat="server" Text='<%#Eval("TotItems") %>' />
</td>
<td width="20%">
<asp:Label ID="lbltprice" runat="server" Text='<%#Eval("TotPrice") %>' />
</td>
<td width="20%">
<asp:DropDownList ID="ddlgender" runat="server" AppendDataBoundItems="true"
DataTextField="Gender" DataValueField="Slno" AutoPostBack="True"
SelectedValue='<%# DataBinder.Eval(Container.DataItem,"Gender")%>'>
<asp:ListItem>m</asp:ListItem>
<asp:ListItem>f</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</table>
</ItemTemplate>
<HeaderTemplate>
<table width="100%">
<tr>
<td>
Item name
</td>
<td>
Price
</td>
<td>
Total items
</td>
<td>
Total price
</td>
<td>
Gender
</td>
</tr>
</table>
</HeaderTemplate>
<%-- <asp:TemplateField HeaderText="Item Price">
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total Items">
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total Price">
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>--%>
<%-- <EditItemTemplate>
<table>
<tr>
<td> <asp:Label ID="lblsno" Text="Slno" runat="server"/><asp:TextBox ID="txtsno" runat="server" Text='<%#Eval("Slno") %>' /></td>
<td> <asp:Label ID="Label1" Text="Name" runat="server"/><asp:TextBox ID="txtname" runat="server" Text='<%#Eval("Name") %>' /></td>
</tr>
<tr>
<td><asp:Label ID="Label2" Text="Item" runat="server"/><asp:TextBox ID="txtitem" runat="server" Text='<%#Eval("Item") %>' /></td>
<td><asp:Label ID="Label3" Text="Price" runat="server"/><asp:TextBox ID="txtprice" runat="server" Text='<%#Eval("Price") %>' /></td>
</tr>
<tr>
<td> <asp:Label ID="Label4" Text="Total Items" runat="server"/><asp:TextBox ID="txttotitems" runat="server" Text='<%#Eval("TotItems") %>' /></td>
<td> <asp:Label ID="Label5" Text="Total Price" runat="server"/><asp:TextBox ID="txttotprice" runat="server" Text='<%#Eval("TotPrice") %>' /></td>
</tr>
<tr>
<td> <asp:Label ID="Label6" Text="Discount" runat="server"/><asp:TextBox ID="txtdisc" runat="server" Text='<%#Eval("Discount") %>' /></td>
<td> <asp:Label ID="Label7" Text="Grand Total" runat="server"/><asp:TextBox ID="txtgrandtotal" runat="server" Text='<%#Eval("GrandTotal") %>' /></td>
</tr>
</table>
</EditItemTemplate>--%>
</asp:TemplateField>
<%-- <asp:TemplateField>
<EditItemTemplate>
<asp:Button ID="Updatebtn" CommandName="Update" runat="server" Width="50px" Text="Update"/>
<asp:Button ID="Cancelbtn" CommandName="Cancel" runat="server" Width="50px" Text="Cancel"/>
</EditItemTemplate>
<ItemTemplate>
<asp:Button ID="Editbtn" CommandName="Edit" runat="server" Width="50px" Text="Edit"/>
<asp:Button ID="Delbtn" CommandName="Delete" runat="server" Width="50px" Text="Delete"/>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="Addbtn" CommandName="AddNew" runat="server" Width="50px" Text="New"/>
</FooterTemplate>
</asp:TemplateField>--%>
</Columns>
</asp:GridView>
code behind code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindEmployeeDetails();
}
}
private void BindEmployeeDetails()
{
con.Open();
SqlCommand cmd = new SqlCommand("Select * from Catogery", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
//foreach (GridViewRow row in GridView1.Rows)
//{
// DropDownList ddlcity = (DropDownList)(GridView1.Rows[row.RowIndex].FindControl("ddlgender"));
// ddlcity.DataSource = ds;
// ddlcity.DataValueField = "Slno";
// ddlcity.DataTextField = "Gender";
// ddlcity.DataBind();
//}
}
DataKeyNames="Slno" OnRowDataBound="GridView1_RowDataBound" Height="202px" Width="347px">
<Columns>
<asp:TemplateField HeaderText="Item Name">
<ItemTemplate>
<table width="100%">
<tr>
<td width="20%">
<asp:Label ID="lblname" runat="server" Text='<%#Eval("Slno") %>' />
</td>
<td width="20%">
<asp:Label ID="lblprice" runat="server" Text='<%#Eval("Price") %>' />
</td>
<td width="20%">
<asp:Label ID="lbltotitems" runat="server" Text='<%#Eval("TotItems") %>' />
</td>
<td width="20%">
<asp:Label ID="lbltprice" runat="server" Text='<%#Eval("TotPrice") %>' />
</td>
<td width="20%">
<asp:DropDownList ID="ddlgender" runat="server" AppendDataBoundItems="true"
DataTextField="Gender" DataValueField="Slno" AutoPostBack="True"
SelectedValue='<%# DataBinder.Eval(Container.DataItem,"Gender")%>'>
<asp:ListItem>m</asp:ListItem>
<asp:ListItem>f</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</table>
</ItemTemplate>
<HeaderTemplate>
<table width="100%">
<tr>
<td>
Item name
</td>
<td>
Price
</td>
<td>
Total items
</td>
<td>
Total price
</td>
<td>
Gender
</td>
</tr>
</table>
</HeaderTemplate>
<%-- <asp:TemplateField HeaderText="Item Price">
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total Items">
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total Price">
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>--%>
<%-- <EditItemTemplate>
<table>
<tr>
<td> <asp:Label ID="lblsno" Text="Slno" runat="server"/><asp:TextBox ID="txtsno" runat="server" Text='<%#Eval("Slno") %>' /></td>
<td> <asp:Label ID="Label1" Text="Name" runat="server"/><asp:TextBox ID="txtname" runat="server" Text='<%#Eval("Name") %>' /></td>
</tr>
<tr>
<td><asp:Label ID="Label2" Text="Item" runat="server"/><asp:TextBox ID="txtitem" runat="server" Text='<%#Eval("Item") %>' /></td>
<td><asp:Label ID="Label3" Text="Price" runat="server"/><asp:TextBox ID="txtprice" runat="server" Text='<%#Eval("Price") %>' /></td>
</tr>
<tr>
<td> <asp:Label ID="Label4" Text="Total Items" runat="server"/><asp:TextBox ID="txttotitems" runat="server" Text='<%#Eval("TotItems") %>' /></td>
<td> <asp:Label ID="Label5" Text="Total Price" runat="server"/><asp:TextBox ID="txttotprice" runat="server" Text='<%#Eval("TotPrice") %>' /></td>
</tr>
<tr>
<td> <asp:Label ID="Label6" Text="Discount" runat="server"/><asp:TextBox ID="txtdisc" runat="server" Text='<%#Eval("Discount") %>' /></td>
<td> <asp:Label ID="Label7" Text="Grand Total" runat="server"/><asp:TextBox ID="txtgrandtotal" runat="server" Text='<%#Eval("GrandTotal") %>' /></td>
</tr>
</table>
</EditItemTemplate>--%>
</asp:TemplateField>
<%-- <asp:TemplateField>
<EditItemTemplate>
<asp:Button ID="Updatebtn" CommandName="Update" runat="server" Width="50px" Text="Update"/>
<asp:Button ID="Cancelbtn" CommandName="Cancel" runat="server" Width="50px" Text="Cancel"/>
</EditItemTemplate>
<ItemTemplate>
<asp:Button ID="Editbtn" CommandName="Edit" runat="server" Width="50px" Text="Edit"/>
<asp:Button ID="Delbtn" CommandName="Delete" runat="server" Width="50px" Text="Delete"/>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="Addbtn" CommandName="AddNew" runat="server" Width="50px" Text="New"/>
</FooterTemplate>
</asp:TemplateField>--%>
</Columns>
</asp:GridView>
code behind code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindEmployeeDetails();
}
}
private void BindEmployeeDetails()
{
con.Open();
SqlCommand cmd = new SqlCommand("Select * from Catogery", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
//foreach (GridViewRow row in GridView1.Rows)
//{
// DropDownList ddlcity = (DropDownList)(GridView1.Rows[row.RowIndex].FindControl("ddlgender"));
// ddlcity.DataSource = ds;
// ddlcity.DataValueField = "Slno";
// ddlcity.DataTextField = "Gender";
// ddlcity.DataBind();
//}
}
Comments