Click here to Skip to main content
14,882,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I have a grid view in which there are different feilds and every field is a template field. On iten template I have labels and on edit item template there are drop dwn lists.
My prob is that gv is binding perfectly and on the click of edit button it is taking me to edit the grid view...
but the drop down lists are not populated... means say if i have a field active with Yes or no and label has its value as yes, I want when i click on the edit button ddl should also show the yes option populated automatically. Do i have to do this on row databound event for each drop dwn list ?

Hi ,
You have to set SelectedValue with your columns
Check this Example .

           <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
            <asp:BoundField DataField="dd" HeaderText="dd" SortExpression="dd" />
            <asp:TemplateField HeaderText="ddd" SortExpression="ddd">
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("ddd") %>'></asp:Label>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ddd") %>'></asp:TextBox>
            <asp:TemplateField HeaderText="cate" SortExpression="cate">
                    <asp:DropDownList ID="DropDownList1" runat="server"
                        DataSourceID="SqlDataSource12" DataTextField="cate" DataValueField="id"
                        SelectedValue="<%# Bind('cate') %>">
                    <asp:SqlDataSource ID="SqlDataSource12" runat="server"
                        ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
                        SelectCommand="SELECT * FROM [cate]"></asp:SqlDataSource>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("cate") %>'></asp:Label>
            <asp:CommandField ShowEditButton="True" />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
        SelectCommand="SELECT * FROM [View_1]"></asp:SqlDataSource>
           <br />
           <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button"
                   style="height: 26px" />

Best Regards
Taresh Uppal 14-May-12 1:50am
No you are getting me wrong...this drop downlist needs to populate through a string whose value cmes in the datatable which is binding the gv
Taresh Uppal 14-May-12 1:51am
suppose if datatable has string strActive = "yes"; than drop dwn list should show Yes ...
Mohamed Mitwalli 14-May-12 2:11am
You want the DropdownList Bind with the Exact Value that appear in Grid isn't
So you have set SelectedValue propriety
Mohamed Mitwalli 14-May-12 2:13am
Try it and let me know .
in row databound event

Dim dtPriceSource As New DataTable()
                    dtPriceSource = CType(Session("123"), DataTable)
                    Dim ddl As DropDownList
                    ddl = New DropDownList()
                   ddl = CType(e.Row.FindControl("ddl"), DropDownList)
                    Dim mypricesouce As String = ddl.Text
                    ddl.DataSource = dtPriceSource
                   ddl.Items.Insert(0, New ListItem(" ", 0))
                    If IsDBNull(Gridview1.DataKeys(e.Row.RowIndex).Values(0)) = True Then
                        ddl.SelectedValue = 0
                       ddl.SelectedValue = Gridview1.DataKeys(e.Row.RowIndex).Values(0)
                    End If
here is my code which I am using during gv row editing to pupulate the ddl...
protected void gvAllClients_RowEditing(object sender, GridViewEditEventArgs e)
        gvAllClients.EditIndex = e.NewEditIndex;
        DataTable dtResult = (DataTable)(Session["gvAll_Client_fill"]);

        string strDatatype = Convert.ToString(dtResult.Rows[0]["DataType"]);
        string strComb = Convert.ToString(dtResult.Rows[0]["Combination"]);
        string strActive = Convert.ToString(dtResult.Rows[0]["IsActive"]);
        GridViewRow row = gvAllClients.Rows[e.NewEditIndex];
        DropDownList ddlDataType = (DropDownList)(row.FindControl("ddlDataTypeAll"));
        if (strDatatype == "EXPORT")
            ddlDataType.SelectedItem.Text = "EXPORT";

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