Link to home
Start Free TrialLog in
Avatar of printmedia
printmedia

asked on

Get the selectedvalue for the right clicked row in checkedlistbox

Hi all.

I have a checkedlistbox that I populate via the code below. I also added a ContextMenuStrip that when the end user right clicks a row (regardless if it's checked or not) I want to be able to edit the text. To do this I need to get the SelectedValue but unfortunately with the code below, the SelectedValue (i.e. StepID) is blank.

What am I doing wrong? Thank you in advance.

Private Sub LoadSteps()
CheckedListBox_OpportunitySteps.Items.Clear()

        Dim con As New SqlConnection
        Dim cmd As New SqlCommand

        con.ConnectionString = "Data Source=myserver;Initial Catalog=mydatabase;Integrated Security=True"

        con.Open()

        cmd.Connection = con

        cmd.CommandText = "SELECT Step, Completed, StepID FROM CRMOpportunitySteps WHERE OpportunityID = @OpportunityID ORDER BY StepID"
        cmd.Parameters.AddWithValue("@OpportunityID", opportunityID)

        Dim sda As New SqlDataAdapter(cmd)
        Dim dt As New DataTable()

        sda.Fill(dt)

        CheckedListBox_OpportunitySteps.DisplayMember = "Step"
CheckedListBox_OpportunitySteps.ValueMember = "StepID"

If dt.Rows.Count > 0 Then
            For i As Integer = 0 To dt.Rows.Count - 1
 CheckedListBox_OpportunitySteps.Items.Add(CStr(dt.Rows(i).Item(0)), dt.Rows(i).Item(1))

            Next
 End If
    End Sub

Private Sub EditToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles EditToolStripMenuItem.Click

        
        Dim itm As String

        itm = InputBox("Enter Step", "New Step", CheckedListBox_OpportunitySteps.SelectedItem)

        If itm.Trim <> "" Then UpdateStep(itm)

        LoadSteps()
    End Sub

    Sub UpdateStep(ByVal newItem As String)
        Dim con As New SqlConnection
        Dim cmd As New SqlCommand
        Dim adapter As New SqlDataAdapter()
        Dim ds As New DataSet()

        con.ConnectionString = "Data Source=myServer;Initial Catalog=mydatabase;Integrated Security=True"

        con.Open()

        cmd.Connection = con

        newItem = newItem.Trim()

        cmd.CommandText = "UPDATE CRMOpportunitySteps SET Step = @Step WHERE StepID = @StepID"
        cmd.Parameters.AddWithValue("@StepID", CheckedListBox_OpportunitySteps.SelectedValue)
        cmd.Parameters.AddWithValue("@Step", newItem)

        cmd.ExecuteNonQuery()

        cmd.Dispose()
        con.Close()
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial