Click here to Skip to main content
15,119,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to bind two DropDownList into a GridView in ASP.NET.

I did bind one DropDownList with Databound event, but the problem is how can I bind the second DropDownList in the GridView?

One DropDownList completely binds with database, but the other does not bind with the same event or GridView.

my HTML Code:

XML
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        &nbsp;<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:customerConnectionString %>"
            SelectCommand="SELECT [id], [name], [gender], [city], [state] FROM [cust_info]">
        </asp:SqlDataSource>
        <asp:GridView ID="GridView1" runat="server" ShowFooter="true" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound">
<Columns>

<asp:TemplateField HeaderText="Name" SortExpression="Name"> <EditItemTemplate>
  <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
  <asp:TextBox ID="txtNewName" runat="server"></asp:TextBox> </FooterTemplate>
<ItemTemplate>
  <asp:Label ID="Label2" runat="server" Text='<%# Bind("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Gender">
<EditItemTemplate>
  <asp:DropDownList ID="cmbGender" runat="server" SelectedValue='<%# Eval("gender") %>'>

  </asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
  <asp:Label ID="lbGender" runat="server" Text='<%# Bind("gender") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
  <asp:DropDownList ID="cmbNewGender" runat="server" > </asp:DropDownList>


</FooterTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="City">
<EditItemTemplate>
  <asp:TextBox ID="txtCity" runat="server" Text='<%# Bind("city") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
  <asp:TextBox ID="txtNewCity" runat="server" ></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
  <asp:Label ID="Label3" runat="server" Text='<%# Bind("city") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="State" SortExpression="State">
<EditItemTemplate>
  <asp:Label ID="Label1" runat="server" Text='<%# Eval("state") %>'></asp:Label>
</EditItemTemplate>
<FooterTemplate>
  <asp:TextBox ID="txtNewState" runat="server" ></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
  <asp:Label ID="Label4" runat="server" Text='<%# Bind("state") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Type">
<EditItemTemplate>
  <asp:DropDownList ID="cmbType" runat="server" DataTextField="Type" DataValueField="Type" SelectedValue='<%# Eval("type") %>'> </asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
  <asp:Label ID="Label5" runat="server" Text='<%# Eval("type") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
  <asp:DropDownList ID="cmbNewType" runat="server" DataTextField="Type" DataValueField="Type"> </asp:DropDownList>

</FooterTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Edit" ShowHeader="False">
<EditItemTemplate>
  <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
  <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
  <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="AddNew" Text="Add New"></asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
  <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" ShowHeader="True" />

</Columns>
</asp:GridView>




    </div>
    </form>
</body>
</html>


and my c# code is :


C#
public DataTable FetchCustomerType()
    {
        string sql = "Select Distinct type From cust_info";
        SqlDataAdapter da = new SqlDataAdapter(sql, conn);
        DataTable dt = new DataTable();
        da.Fill(dt);
        return dt;
    }

    public DataTable FetchCustomerGender()
    {
        string sql = "Select Distinct gender From cust_info";
        SqlDataAdapter da = new SqlDataAdapter(sql, conn);
        DataTable dt = new DataTable();
        da.Fill(dt);
        return dt;
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList cmbtype = (DropDownList)e.Row.FindControl("cmbType");
            DropDownList cmbgender = (DropDownList)e.Row.FindControl("cmbGender");
            if (cmbtype != null && cmbgender !=null)
            {

                cmbgender.DataSource = FetchCustomerGender();
                cmbtype.DataSource = FetchCustomerType();
                cmbtype.DataBind();
                cmbgender.DataBind();
                cmbgender.SelectedValue = GridView1.DataKeys[e.Row.RowIndex].Values[2].ToString();
                cmbtype.SelectedValue = GridView1.DataKeys[e.Row.RowIndex].Values[1].ToString();

            }
        }



        if (e.Row.RowType == DataControlRowType.Footer)
        {
            DropDownList cmbnewtype = (DropDownList)e.Row.FindControl("cmbNewType");
            DropDownList cmbnewgndr = (DropDownList)e.Row.FindControl("cmbNewGender");
            cmbnewtype.DataSource = FetchCustomerType();
            cmbnewgndr.DataSource = FetchCustomerGender();
            cmbnewtype.DataBind();
            cmbnewgndr.DataBind();
        }

        
        
            
    }

i am tlking abt "cmbgender" and "cnbnewgender"

Where should I put the other DropDownList coding into this code which executes properly?

what is the specific changes into this problem?

Thank you.
Posted
Updated 3-Mar-12 9:40am
v3
Comments
CRDave1988 1-Mar-12 1:36am
   
r u talking about cmbNewType?
djharry2 1-Mar-12 2:40am
   
yes i m talking abt cmbnewtype sir how can i bind other dropdownlist

into this example pls suggest me pls give me solution for this problem
Varun Sareen 1-Mar-12 3:35am
   
Please check your function/method:-FetchCustomerType(), whether it is returning value or not.
djharry2 1-Mar-12 3:46am
   
ya in FetchCustomerType() it is returning value but in same way i did in other FetchCustomerGender() whn i execute the code output is like Sysyte.Data.DataRowView thr is no value come out from data base pls suggest me thx
Varun Sareen 1-Mar-12 3:49am
   
Couldn't get you what you are trying to say but actually what i have understood from your explanation that the data is not getting filled up in the datatable and so it is not able to bind the combo box.

May be you are trying to fetch a column which is not present in the database or trying to bind a database column which you have not specified in the design time with the control :)

Try to correct that.
Mario Majčica 1-Mar-12 3:47am
   
You should also first set DataSource and only then call the DataBind method. Is problem maybe there?
Mahmud Hasan 3-Mar-12 14:41pm
   
Question edited to make C# block more readable.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900