Click here to Skip to main content
15,917,604 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

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 ?
Posted

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

ASP.NET
<div>
           <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
        onrowdeleting="GridView1_RowDeleting">
        <Columns>
            <asp:BoundField DataField="dd" HeaderText="dd" SortExpression="dd" />
            <asp:TemplateField HeaderText="ddd" SortExpression="ddd">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("ddd") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ddd") %>'></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="cate" SortExpression="cate">
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server"
                        DataSourceID="SqlDataSource12" DataTextField="cate" DataValueField="id"
                        SelectedValue="<%# Bind('cate') %>">
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="SqlDataSource12" runat="server"
                        ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
                        SelectCommand="SELECT * FROM [cate]"></asp:SqlDataSource>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("cate") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:CommandField ShowEditButton="True" />
        </Columns>
    </asp:GridView>
    <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" />
</div>


Best Regards
M.Mitwalli
 
Share this answer
 
Comments
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


VB
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.DataBind()
                   ddl.Items.Insert(0, New ListItem(" ", 0))
                    If IsDBNull(Gridview1.DataKeys(e.Row.RowIndex).Values(0)) = True Then
                        ddl.SelectedValue = 0
                    Else
                       ddl.SelectedValue = Gridview1.DataKeys(e.Row.RowIndex).Values(0)
                    End If
 
Share this answer
 
v2
here is my code which I am using during gv row editing to pupulate the ddl...
C#
protected void gvAllClients_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvAllClients.EditIndex = e.NewEditIndex;
        gvall_Client_fill();
        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";
        }
 
Share this answer
 

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