Banner Ad

Wednesday, January 22, 2014

Grid View : Binding Dropdown inside a Gridview

By Francis   Posted at   9:15 AM   Gridview Sample No comments
In this gridview tutorial, I’m going to explain how to bind a drop down list with in a gridview. The drop down list located inside gridview in a “Template” Column. Paste the below code in an aspx page.

 
 
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="CountryStateDemo.aspx.vb" 

Inherits="Gridview_CountryStateDemo" %> 

<!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>Gridview - Country State Demo</title> 

</head> 

<body> 

<form id="form1" runat="server"> 

<div> 

<asp:GridView runat="server" ID="dgView" AutoGenerateColumns="false" > 

<Columns> 

<asp:BoundField DataField="city_name" HeaderText="City" /> 

<asp:TemplateField HeaderText="Country"> 

<ItemTemplate> 

<asp:DropDownList runat="server" ID="ddlCountry"> 

</asp:DropDownList> 

</ItemTemplate> 

</asp:TemplateField> 

</Columns> 

</asp:GridView> 

</div> 

</form> 

</body> 

</html> 
Paste the below code in aspx.vb page:
Imports System.Data
Imports System.Data.SqlClient
Partial Class Gridview_CountryStateDemo
    Inherits System.Web.UI.Page
    Private dsCountry As New DataSet
    Private dsCity As New DataSet
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' Check that page is loaded first time or not
        ' That is the page is not posted back
        If Not Page.IsPostBack Then
            GetDataFromDB()
        End If
    End Sub

    ''' 
    ''' Get the Data From DB
    ''' 
    ''' DataSet
    ''' 
    Private Function GetDataFromDB() As DataSet
        ' Declare con string and other sql objects
        Dim strConString As String = "Data Source=Francis-pc;Initial Catalog=Francis;Integrated Security=True"
        Dim sqlcon As New SqlConnection(strConString)
        Dim sqlcmd As New SqlCommand()
        sqlcmd.Connection = sqlcon
        sqlcmd.CommandText = "select City_id, city_name from tblCity"
        Dim sd As New SqlDataAdapter()
        ' Open the connection
        sqlcon.Open()
        ' Execute the select statement
        sd.SelectCommand = sqlcmd
        ' Fill it on the dataset
        sd.Fill(dsCity)
        ' Close the connection
        sqlcon.Close()
        ' Assign the datasource for the grid
        dgView.DataSource = dsCity
        ' Bind the grid
        dgView.DataBind()
    End Function

    ''' 
    ''' This event is fired when the "DataBind" to the grid. Fired for each row for the datasource
    ''' 
    ''' 
    ''' 
    ''' 
    Protected Sub dgView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles dgView.RowDataBound
        ' Check current row is a datarow 
        If e.Row.RowType = DataControlRowType.DataRow Then
            ' ' Declare con string and other sql objects
            Dim strConString As String = "Data Source=Francis-pc;Initial Catalog=Francis;Integrated Security=True"
            Dim sqlcon As New SqlConnection(strConString)
            Dim sqlcmd As New SqlCommand()
            sqlcmd.Connection = sqlcon
            sqlcon.Open()
            sqlcmd.CommandText = "select country_id, country_name from tblCountry"
            Dim sd As New SqlDataAdapter()
            sd.SelectCommand = sqlcmd
            'Fill the country dataset
            sd.Fill(dsCountry)
            sqlcon.Close()
            ' Get the "Country" drop down object in THIS row
            Dim ddlCountry As DropDownList = CType(e.Row.FindControl("ddlCountry"), DropDownList)
            ' Assign the "Country" dataset to the dropdown
            ddlCountry.DataSource = dsCountry
            ddlCountry.DataTextField = "country_name"
            ddlCountry.DataValueField = "country_id"
            ' Bind the dropdown
            ddlCountry.DataBind()
        End If
    End Sub

End Class


Happy Coding!!

About Francis

Francis, an Associate at Cognizant. Having 7+ Years of experience in Microsoft web technologies.

0 comments :

Please give your valuable comments to improve the contents

Connect with Us