Menu ▼



Posted By :
avatarmrkraju
Total Views : 887

Bind data to DropDownList in GridView

This example shows you how to bind data to DropDownList in GridView based on the Grid Row Value. In this example we will bind the States to DropDownList based on Country in Row.

In the GridView RowDataBound we will find the DropDownList and Country then fetch the state list for the Country and bind the state list to DropDownList.

RowDataBound event

    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int CountryID = int.Parse(((HiddenField)e.Row.FindControl("hidCountryID")).Value);
            DropDownList ddl = (DropDownList)e.Row.FindControl("ddlState");
            ddl.DataSource = GetStates(CountryID);
            ddl.DataTextField = "State";
            ddl.DataValueField = "StateID";
            ddl.DataBind();
        }
    }

Example
Dropdown binding in GridView

CS Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using System.Text;
using System.Collections;

namespace WebApp
{
    public partial class Sample : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillGrid();
            }
        }

        void FillGrid()
        {
            gv.DataSource = GetCountries();
            gv.DataBind();
        }

        protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int CountryID = int.Parse(((HiddenField)e.Row.FindControl("hidCountryID")).Value);
                DropDownList ddl = (DropDownList)e.Row.FindControl("ddlState");
                ddl.DataSource = GetStates(CountryID);
                ddl.DataTextField = "State";
                ddl.DataValueField = "StateID";
                ddl.DataBind();
            }
        }

        DataTable GetCountries()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("CountryID", typeof(int));
            dt.Columns.Add("Country", typeof(string));
            dt.Rows.Add(1, "Australia");
            dt.Rows.Add(2, "New Zealand");
            dt.Rows.Add(3, "India");
            dt.Rows.Add(4, "USA");
            return dt;
        }

        DataTable dtStates = null;

        DataTable GetStates(int CountryID)
        {
            if (dtStates == null)
            {
                dtStates = new DataTable();
                dtStates.Columns.Add("CountryID", typeof(int));
                dtStates.Columns.Add("StateID", typeof(string));
                dtStates.Columns.Add("State", typeof(string));
                dtStates.Rows.Add(1, 1, "Albury");
                dtStates.Rows.Add(1, 2, "Dubbo");
                dtStates.Rows.Add(1, 3, "Sydney");
                dtStates.Rows.Add(2, 1, "Auckland");
                dtStates.Rows.Add(2, 2, "Hamilton");
                dtStates.Rows.Add(2, 3, "Wellington");
                dtStates.Rows.Add(3, 1, "Delhi");
                dtStates.Rows.Add(3, 2, "Hyderabad");
                dtStates.Rows.Add(3, 3, "Mumbai");
                dtStates.Rows.Add(4, 1, "Chicago");
                dtStates.Rows.Add(4, 2, "New York");
                dtStates.Rows.Add(4, 3, "Washington DC");
            }
            dtStates.DefaultView.RowFilter ="CountryID=" + CountryID;
            return dtStates.DefaultView.ToTable();
        }


    }

}

ASPX Page

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Sample.aspx.cs" Inherits="WebApp.Sample" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" 
            CellPadding="5" CellSpacing="5" ForeColor="#333333" GridLines="Both" 
            onrowdatabound="gv_RowDataBound"  >
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <Columns>
                <asp:TemplateField HeaderText="Country">
                    <ItemTemplate>
                        <asp:HiddenField ID="hidCountryID" runat="server" Value='<%#Eval("CountryID") %>' />
                        <asp:Label ID="lblCountry" runat="server" Text='<%#Eval("Country") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="State">
                    <ItemTemplate>
                        <asp:DropDownList ID="ddlState" runat="server" style="width:100%">
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>






comments powered by Disqus


Search dotnetlearners.com