Hi,

I have set the content of a listbox through databinding.

myListBox.DataSource = myDataSource
myListBox.DataTextField = "column01"
myListBox.DataValueField = "column02"
myListBox.DataBind()

I have then set the selected index of the listbox using a loop

Dim myValue As Integer = [I]someValue[/I]
Dim myListItem As ListItem = New ListItem
For Each myListItem In myListBox.Items
If myListItem.Value = myValue Then
myListItem.Selected = True
End If
Next

It seems a little strange that you can so easily define a datasource but have to resort to a loop to define the selected index.

Is there no way to define the selected index when the datasource is bound? What if I have multple selections to define, would I have to expand my loop to set these?

Thanks,

Fido.

Member Avatar for DearDhruv

Friend I am Giving u a code please go through following ...

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.UI.WebControls
Imports System.Web.UI.Control

Partial Class _Default
    Inherits System.Web.UI.Page
    Dim a As New SqlConnection()
    Dim cmd As New SqlCommand()
  
    Dim uid1 As String
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        a.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|Database.mdf;Integrated Security=True;User Instance=True"

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        
        Dim atLeastOneRowDeleted As Boolean = False
        For Each row As GridViewRow In GridView1.Rows
            Dim cb As CheckBox = row.FindControl("chb1")
            If cb IsNot Nothing AndAlso cb.Checked Then
                atLeastOneRowDeleted = True
                Dim ID As String = Convert.ToString(GridView1.DataKeys(row.RowIndex).Value)
                cmd.CommandText = "delete from log where id = '" & ID & "'"
                cmd.ExecuteNonQuery()

            End If
        Next
        a.Close()
        GridView1.DataBind()
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click

        For Each row As GridViewRow In GridView1.Rows
            Dim cb As CheckBox = row.FindControl("chb1")
            If cb IsNot Nothing Then
                cb.Checked = True
            End If
        Next
    End Sub

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        For Each row As GridViewRow In GridView1.Rows
            Dim cb As CheckBox = row.FindControl("chb1")
            If cb IsNot Nothing AndAlso cb.Checked Then
                cb.Checked = False
            End If
        Next
    End Sub

End Class

I think this code will be able yo solve your problem....

>What if I have multple selections to define, would I have to expand my loop to set these?

No methods to bind ListBox with multiselect options. You have to use loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.