Populating dropdown within a datagrid

ninel

Active member
Joined
Mar 23, 2005
Messages
32
Location
Land O Lakes, Florida
Programming Experience
3-5
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:
VB.NET:
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

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
 
You could create a new List Item, then add it to the DDL and set it as default.

Example

Dim tempLI As List Item = New List Item
tempLI.Text = "-SELECT-"
tempLI.Value = 0 <----------- or another value that isn't being used
ddlNetworkAccessGroup.Items.Add(tempLI)
ddlNetworkAccessGroup.SelectedValue = 0
 
Back
Top