Fill GridView DropDownList at Runtime

Hello tyrodeveloper's, if you need a DropDownList located into GridView control, you need use the "OnRowDataBound" event.
First we need design the GridView:
<div>
    <h1>Users List</h1>
    <asp:GridView ID="grdUsers" runat="server" AutoGenerateColumns="False" 
        onrowdatabound="grdUsers_RowDataBound" DataKeyNames="userName">
    <Columns>
        <asp:BoundField DataField="userName" HeaderText="User Name" />
        <asp:TemplateField>
            <HeaderTemplate>Country</HeaderTemplate>
            <ItemTemplate>
                <asp:DropDownList ID="cboCountryId" runat="server">
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    </asp:GridView>
    <br />
    <asp:Button ID="btnGetValues" runat="server" Text="Get the values" 
        onclick="btnGetValues_Click" />
    <br />
    <asp:Label ID="lblValues" runat="server" Text=""></asp:Label>
</div>

Fill the GridView with data:
protected void Page_Load(object sender, EventArgs e)
{
    //DataTable
    DataTable dt = new DataTable();
    //Columns
    DataColumn useName = dt.Columns.Add("userName",
        Type.GetType("System.String"));
    useName.AllowDBNull = false;
    useName.Unique = true;
    DataColumn countryId = dt.Columns.Add("countryId",
        Type.GetType("System.Int32"));
    countryId.AllowDBNull = false;
    countryId.Unique = false;
    //Add Data
    DataRow dr = dt.NewRow();
    dr["userName"] = "mx";
    dr["countryId"] = "0";
    dt.Rows.Add(dr);
    dr = dt.NewRow();
    dr["userName"] = "us";
    dr["countryId"] = "1";
    dt.Rows.Add(dr);
    dr = dt.NewRow();
    dr["userName"] = "cn";
    dr["countryId"] = "2";
    dt.Rows.Add(dr);
    dr = dt.NewRow();
    dr["userName"] = "ar";
    dr["countryId"] = "3";
    dt.Rows.Add(dr);
    dr = dt.NewRow();
    dr["userName"] = "uk";
    dr["countryId"] = "4";
    dt.Rows.Add(dr);
    dr = dt.NewRow();
    dr["userName"] = "br";
    dr["countryId"] = "5";
    dt.Rows.Add(dr);
    //BindData
    grdUsers.DataSource = dt;
    grdUsers.DataBind();
}

This code is to Fill the DropDownList:
protected void FillCombo(DropDownList cbo) {
    ListItem li;
    li = new ListItem("México", "0");
    cbo.Items.Add(li);
    li = new ListItem("USA", "1");
    cbo.Items.Add(li);
    li = new ListItem("China", "2");
    cbo.Items.Add(li);
    li = new ListItem("Argentina", "3");
    cbo.Items.Add(li);
    li = new ListItem("United Kingdom", "4");
    cbo.Items.Add(li);
    li = new ListItem("Brasil", "5");
    cbo.Items.Add(li);
}

To Fill the Control at runtime:
protected void grdUsers_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DropDownList cbo = (DropDownList)e.Row.FindControl("cboCountryId");
        //Fill the combo
        FillCombo(cbo);
        //Select the value
        cbo.SelectedIndex =
            cbo.Items.IndexOf(cbo.Items.FindByValue(DataBinder.
            Eval(e.Row.DataItem, "countryId").ToString()));
        cbo.Dispose();
    }
}

To get the DropDownList Selected Item for each row:
void GridLoop()
{

    try
    {
        for (int I = 0; I <= grdUsers.Rows.Count - 1; I++)
        {
            //Get the object
            DropDownList cbo =
                (DropDownList)grdUsers.Rows[I].FindControl("cboCountryId");
            lblValues.Text +=
                String.Format("User Name: {0}<br />" +
                "Selected Text: {1}<br />" +
                "Selected Value: {2}<br />" +
                "<hr />",
                grdUsers.DataKeys[I].Value.ToString(),
                cbo.SelectedItem.Text,
                cbo.SelectedValue.ToString());
        }
    }
    catch (Exception ex)
    {
        throw (ex);
    }
}

Getting the values:
protected void btnGetValues_Click(object sender, EventArgs e)
{
    try
    {
        GridLoop();
    }
    catch (Exception ex)
    {
        lblValues.Text = ex.Message;
    }
}


Please Click on the +1 button

No hay comentarios:

Publicar un comentario