Saturday, April 30, 2011

DropDownList in Grid View

Demo.aspx
<asp:TemplateField HeaderText="FlatNo" HeaderStyle-Width="200px">
                 <ItemTemplate>
                                    <asp:Label ID="lblFlatName" runat="server" Text='<%# Eval("Flatname") %>' Width="150px"></asp:Label>
                </ItemTemplate>
               <EditItemTemplate>                                  
                                    <asp:DropDownList ID="ddlFlateName" runat="server" Width="150px" >
                                    </asp:DropDownList>
              </EditItemTemplate>
</asp:TemplateField>

Demo.aspx.cs
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
                DropDownList ddlTemp = (DropDownList)e.Row.FindControl("ddlFlateName");
                string s = "SELECT [FLATID], [FLATNO] FROM [FLATDETAIL] where Projectcode='" + Mpid + "' ORDER BY [FLATNO] ";
                DataTable dt = obj.GetTable(s);
                ddlTemp.DataSource = dt;
                ddlTemp.DataTextField = "FLATNO";
                ddlTemp.DataValueField = "FLATID";
                ddlTemp.DataBind();
            }
        }

    }

1 comment :

  1. protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    if ((e.Row.RowState & DataControlRowState.Edit) > 0)
    {
    Label lableTemp = (Label)e.Row.FindControl("Label5");

    DropDownList ddlTemp = (DropDownList)e.Row.FindControl("ddlFlateName");

    string s = "SELECT [FLATID], [FLATNO] FROM [FLATDETAIL] where Projectcode='" + Mpid + "' ORDER BY [FLATNO] ";
    DataTable dt = obj.GetTable(s);
    ddlTemp.DataSource = dt;
    ddlTemp.DataTextField = "FLATNO";
    ddlTemp.DataValueField = "FLATID";
    ddlTemp.Text = lableTemp.Text;
    ddlTemp.DataBind();
    }
    }
    }

    ReplyDelete