Ans:-
Here i have taken one GridView and in Gridview i have taken DropDownList.
Now i have to bind/get data from DB in to DropDownList which is placed in Gridview.
See the below is UI of small application..
DDLInGrdView.aspx:-
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="DDLInGrdView.aspx.vb" Inherits="DDLInGrdView" %>
<!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>DDL in GridView Page</title>
</head>
<body>
<form id="form1" runat="server">
<center>
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="sid" HeaderText="sid" SortExpression="sid" />
<asp:BoundField DataField="sname" HeaderText="sname" SortExpression="sname" />
<asp:TemplateField HeaderText="city" SortExpression="sid">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" ></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<div>
<asp:Label ID="lblTest" runat="server" Text="Label"></asp:Label>
</div>
</center>
</form>
</body>
</html>
DDLInGrdView.aspx.vb:-
Imports System
Imports System.Data
Imports System.Web.UI.WebControls
Imports System.Data.SqlClient
Partial Class DDLInGrdView
Inherits System.Web.UI.Page
Dim obj As New Class1
Dim cn As SqlConnection
Dim cmd As SqlCommand
Dim da As SqlDataAdapter
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Me.BindData()
End If
End Sub
Private Sub BindData()
cn = New SqlConnection(obj.GetConnection())
cn.Open()
cmd = New SqlCommand("select * from state", cn)
da = New SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds)
cn.Close()
GridView1.DataSource = ds
GridView1.DataBind()
End Sub
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
cn = New SqlConnection(obj.GetConnection())
cn.Open()
Dim ddl As DropDownList = DirectCast(e.Row.FindControl("DropDownList1"), DropDownList)
Dim sid As Integer = Convert.ToInt32(e.Row.Cells(0).Text)
cmd = New SqlCommand("select * from city where sid=" & sid, cn)
da = New SqlDataAdapter(cmd)
Dim ds As New DataSet
da.Fill(ds)
cn.Close()
ddl.DataSource = ds
ddl.DataTextField = "cname"
ddl.DataValueField = "cname"
ddl.DataBind()
ddl.Items.Insert(0, New ListItem("-Select-", "0"))
End If
End Sub
End Class
Here i have taken one GridView and in Gridview i have taken DropDownList.
Now i have to bind/get data from DB in to DropDownList which is placed in Gridview.
See the below is UI of small application..
DDLInGrdView.aspx:-
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="DDLInGrdView.aspx.vb" Inherits="DDLInGrdView" %>
<!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>DDL in GridView Page</title>
</head>
<body>
<form id="form1" runat="server">
<center>
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="sid" HeaderText="sid" SortExpression="sid" />
<asp:BoundField DataField="sname" HeaderText="sname" SortExpression="sname" />
<asp:TemplateField HeaderText="city" SortExpression="sid">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" ></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<div>
<asp:Label ID="lblTest" runat="server" Text="Label"></asp:Label>
</div>
</center>
</form>
</body>
</html>
Imports System
Imports System.Data
Imports System.Web.UI.WebControls
Imports System.Data.SqlClient
Partial Class DDLInGrdView
Inherits System.Web.UI.Page
Dim obj As New Class1
Dim cn As SqlConnection
Dim cmd As SqlCommand
Dim da As SqlDataAdapter
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Me.BindData()
End If
End Sub
Private Sub BindData()
cn = New SqlConnection(obj.GetConnection())
cn.Open()
cmd = New SqlCommand("select * from state", cn)
da = New SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds)
cn.Close()
GridView1.DataSource = ds
GridView1.DataBind()
End Sub
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
cn = New SqlConnection(obj.GetConnection())
cn.Open()
Dim ddl As DropDownList = DirectCast(e.Row.FindControl("DropDownList1"), DropDownList)
Dim sid As Integer = Convert.ToInt32(e.Row.Cells(0).Text)
cmd = New SqlCommand("select * from city where sid=" & sid, cn)
da = New SqlDataAdapter(cmd)
Dim ds As New DataSet
da.Fill(ds)
cn.Close()
ddl.DataSource = ds
ddl.DataTextField = "cname"
ddl.DataValueField = "cname"
ddl.DataBind()
ddl.Items.Insert(0, New ListItem("-Select-", "0"))
End If
End Sub
End Class
In Class1.vb:-
Imports Microsoft.VisualBasic
Imports System.Data.SqlClient
Public Class Class1
Dim sqlcmd As SqlCommand
Dim sqldr As SqlDataReader
Public Function GetConnection() As String
Dim conStr As String = "Server=.;database=SubsDB;UID=sa;Password=123;Pooling=true;Min Pool Size=2;Max Pool Size=1000"
Return conStr
End Function
End Class
Create Table:-
create table state(sid int primary key,sname varchar(30))
create table city(cid int primary key,cname varchar(30),sid int foreign key references state(sid))
No comments:
Post a Comment