Hi all,
How to Bind DropDownList in GridView in asp.NET ?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Shalini JunejaPosted Jul 15, 2011, 5:24 AM
Rohatash KumarPosted Jul 14, 2011, 7:11 AM
Eddie,
This code Can be helpful.
create table info
(
username varchar(50),
Email varchar(40)
)
go
insert into info values('andrew','[email protected]')
go
insert into info values('Rahul','[email protected]')
go
insert into info values('Rohatash','[email protected]')
go
insert into info values('yardi','[email protected]')
go
select* from info;
ASPX code
<div >
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
asp:GridView>
<br />
<br />
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>rohatashasp:ListItem>
<asp:ListItem>andrewasp:ListItem>
<asp:ListItem>yardiasp:ListItem>
<asp:ListItem>Rahulasp:ListItem>
<asp:ListItem>monuasp:ListItem>
asp:DropDownList>
<asp:Label ID="Label1" runat="server" Text="Label">asp:Label>
<br />
<br />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="ShowRecord"
Height="37px" />
div>
Now double click on the Button Control and add the following code.
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim str As String = "Data Source=.;uid=sa;pwd=Password$2;database=dropdowndatabase"
Dim con As New SqlConnection(str)
con.Open()
Dim cmd As New SqlCommand("select * from info where username = '" + DropDownList1.SelectedItem.Text + "'", con)
'Dim cmd As New SqlCommand("select * from info")
Dim Adpt As New SqlDataAdapter(cmd)
Dim ds As DataSet = New DataSet()
If (Adpt.Fill(ds, "info")) Then
GridView1.DataSource = ds.Tables(0)
GridView1.DataBind()
Label1.Text = "Found"
GridView1.Visible = True
Else
Label1.Text = "Not found"
GridView1.Visible = False
End If
End Sub