Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Populating dropdown within a datagrid

Author  Topic 

ninel
Posting Yak Master

141 Posts

Posted - 2006-05-26 : 13:21:29
I have a datagrid on my web form that needs to contain a dropdown within each row.

Here is the code I have so far:

[Code]
HTML: Just the template column of datagrid:
<asp:TemplateColumn HeaderText="Network Access Group">
<ItemTemplate>
<asp:DropDownList id="ddlNetworkAccessGroup" DataSource="<%#BindState()%>" DataTextField="sDescription" DataValueField="imsNetworkAccessGroupId " runat="server" Font-Name="Tahoma" Font-Size ="x-small" />
</ItemTemplate>
</ asp:TemplateColumn>

Code Behind:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim bPopulateGrid As Boolean
Try

If Not Page.IsPostBack Then
If Request.Cookies.Count = 1 Then
Response.Redirect("login.aspx")
ElseIf Request.Cookies.Count >= 2 Then
BindState()
End If
End If

Catch ex As Exception
lblResult.Text = ex.Message
End Try
End Sub

Public Function BindState()
Dim myConnection As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("connString"))

Dim myCommand As SqlCommand = New SqlCommand("uspGetNetworkAccessGroup", myConnection)

myCommand.CommandType = CommandType.StoredProcedure
myConnection.Open()
Return myCommand.ExecuteReader(CommandBehavior.CloseConnection)
End Function
[/code]



This works perfectly....The only thing is I need the first entry in every dropdownlist to be "-SELECT-"

How can I insert that string into every dropdown?



Thanks,
Ninel




JBelthoff
Posting Yak Master

173 Posts

Posted - 2006-05-26 : 15:19:07
After you bind the data to the DropDownList you can insert a row at position 0 like this.

ddlNetworkAccessGroup.Items.Insert(0, New ListItem("- Select -", "- Select -"))

Just be sure to handle the Error if the Select is not changed.

Enjoy!



JBelthoff
D
odge, Duck, Dip, Dive & Dodge
If a man can dodge a wrench, he can dodge a ball!
Asp Web Hoting Provider
Go to Top of Page
   

- Advertisement -