How to check which item is selected in a datagridview combobox?

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 2022 Views - Last Post: 31 January 2013 - 02:39 AM Rate Topic: -----

#1 thanzeem7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 14-January 13

How to check which item is selected in a datagridview combobox?

Posted 22 January 2013 - 12:36 AM

I try to check which data is selected in a datagridview combobox

Dim status As Double
    For x As Integer = 0 To ATCGRID.Rows.Count - 1
        If ATCGRID.Rows(x).Cells(2).Value = "Full" Then
            status = 1
        ElseIf ATCGRID.Rows(x).Cells(2).Value = "Empty" Then
            status = 0
        ElseIf ATCGRID.Rows(x).Cells(2).Value = "Half" Then
            status = 0.5
        End If
        MessageBox.Show(status)
    Next x

But it shows error

Conversion from string "Full" to type "Double" is not valid.

How i check this?? Please check this code and correct me....

Is This A Good Question/Topic? 0
  • +

Replies To: How to check which item is selected in a datagridview combobox?

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3710
  • View blog
  • Posts: 5,958
  • Joined: 08-June 10

Re: How to check which item is selected in a datagridview combobox?

Posted 22 January 2013 - 12:46 AM

Looks like the value of the cell is a Double. So when you try to compare it to a string, the system can't convert it.

Are you sure you are using the correct cell? Keep in mind that the cell index is zero based, so cell 2 is actually the third cell.
Was This Post Helpful? 0
  • +
  • -

#3 thanzeem7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 14-January 13

Re: How to check which item is selected in a datagridview combobox?

Posted 22 January 2013 - 01:14 AM

View PostAtli, on 22 January 2013 - 12:46 AM, said:

Looks like the value of the cell is a Double. So when you try to compare it to a string, the system can't convert it.

Are you sure you are using the correct cell? Keep in mind that the cell index is zero based, so cell 2 is actually the third cell.

yea.. I am using correct Cell...
Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3710
  • View blog
  • Posts: 5,958
  • Joined: 08-June 10

Re: How to check which item is selected in a datagridview combobox?

Posted 22 January 2013 - 01:42 AM

OK. Have you tried setting a breakpoint at that line to see what the value actually is?
Was This Post Helpful? 0
  • +
  • -

#5 thanzeem7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 14-January 13

Re: How to check which item is selected in a datagridview combobox?

Posted 22 January 2013 - 02:29 AM

View PostAtli, on 22 January 2013 - 01:42 AM, said:

OK. Have you tried setting a breakpoint at that line to see what the value actually is?


I try with this code in a button click event normally..
For x As Integer = 0 To ATCGRID.Rows.Count - 1
                If ATCGRID.Rows(x).Cells(2).Value.ToString() = "Present" Then
                    status = 1
                    MsgBox(status)
                Else
                    If ATCGRID.Rows(x).Cells(2).Value.ToString() = "Halfday" Then
                        status = 0.5
                        MsgBox(status)
                    Else
                        If ATCGRID.Rows(x).Cells(2).Value.ToString() = "Absent" Then
                            status = 0
                            MsgBox(status)
                        End If
                    End If
                End If
next x 

but the click event for saving data from datagrid view to sql database. It shows the conversion error.
Was This Post Helpful? 0
  • +
  • -

#6 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3710
  • View blog
  • Posts: 5,958
  • Joined: 08-June 10

Re: How to check which item is selected in a datagridview combobox?

Posted 22 January 2013 - 02:45 AM

But did you check the value using the debugger, like I suggested? If you don't know what the value actually is, you can't really fix this.
Was This Post Helpful? 0
  • +
  • -

#7 thanzeem7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 14-January 13

Re: How to check which item is selected in a datagridview combobox?

Posted 22 January 2013 - 03:05 AM

View PostAtli, on 22 January 2013 - 02:45 AM, said:

But did you check the value using the debugger, like I suggested? If you don't know what the value actually is, you can't really fix this.


I'll give my full code block and database design...

Private Sub BTNSAVE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNSAVE.Click
        DTPATTENDANCE.Value = Date.Now
        DTPATTENDANCE.Value = Format(DTPATTENDANCE.Value, "dd/MM/yyyy")
        Dim query As SqlCommand
        getConnect()
        Try
            Conn.Open()
            Dim id As Integer
            Dim status As Double
            Dim name As String
            Dim atdate As DateTime
            Dim remark As String
            For x As Integer = 0 To ATCGRID.Rows.Count - 1
                If ATCGRID.Rows(x).Cells(2).Value.ToString() = "Present" Then
                    status = 1
                    MsgBox(status)
                Else
                    If ATCGRID.Rows(x).Cells(2).Value.ToString() = "Halfday" Then
                        status = 0.5
                        MsgBox(status)
                    Else
                        If ATCGRID.Rows(x).Cells(2).Value.ToString() = "Absent" Then
                            status = 0
                            MsgBox(status)
                        End If
                    End If
                End If
                id = ATCGRID.Rows(x).Cells(0).Value
                name = ATCGRID.Rows(x).Cells(1).Value
                atdate = DTPATTENDANCE.Value
                remark = ATCGRID.Rows(x).Cells(3).Value
                Dim strSQL As String = "INSERT INTO ATTENDANCE(EMP_ID,EMP_NAME,AT_DATE,AT_STATUS,AT_REMARK)VALUES(@EMP_ID,@EMP_NAME,@AT_DATE,@AT_STATUS,@AT_REMARK)"
                query = New SqlCommand(strSQL, Conn)
                query.Parameters.AddWithValue("@EMP_ID", id)
                query.Parameters.AddWithValue("@EMP_NAME", name)
                query.Parameters.AddWithValue("@AT_DATE", atdate)
                query.Parameters.AddWithValue("@AT_STATUS", status)
                query.Parameters.AddWithValue("@AT_REMARK", remark)
                query.ExecuteNonQuery()
                Conn.Dispose()
            Next x
            Conn.Close()
            MessageBox.Show("Registered Successfully!", "Register", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As SqlException
            MsgBox(ex.Message, MsgBoxStyle.Critical, "SQL Error")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "General Error")
        End Try
    End Sub



This is my database table design
Posted Image
Was This Post Helpful? 0
  • +
  • -

#8 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3710
  • View blog
  • Posts: 5,958
  • Joined: 08-June 10

Re: How to check which item is selected in a datagridview combobox?

Posted 22 January 2013 - 03:19 AM

That's not what I was asking about.

Do you know how to use the Visual Studio debugger? If not, you should really take the time to learn that. It's a vital skill for a .NET developer. It'll help you solve a LOT of issues that may otherwise be tricky to figure out. (Such as this one.)

I suggest you start by reading through this tutorial.

Once you've gotten a handle on the basics of debugging, use breakpoints to see what the value of the cell you are comparing in your code actually is. Once you know that, you can start figuring out how to use it.

P.S.
I'd avoid using all upper case names for variables. As a general rule, such names are reserved for constants by most programmers. - It's of course up to you, but it tends to be best not to fight traditions like that.
Was This Post Helpful? 0
  • +
  • -

#9 erik venn  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 14
  • Joined: 17-January 13

Re: How to check which item is selected in a datagridview combobox?

Posted 22 January 2013 - 04:31 AM

it seems like you are doing something wrong... your status index seems to be wrong...

im not sure but try this

Dim status As Double 

     For x As Integer = 0 To ATCGRID.Rows.Count - 1  

         If ATCGRID.Rows(x).Cells(1).Value = "Full" Then 

             status = 1  

         ElseIf ATCGRID.Rows(x).Cells(1).Value = "Empty" Then 

             status = 0  

         ElseIf ATCGRID.Rows(x).Cells(1).Value = "Half" Then 

             status = 0.5  
            else
            status = 2
         End If 
         MessageBox.Show(status)  

     Next x 



Was This Post Helpful? 0
  • +
  • -

#10 erik venn  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 14
  • Joined: 17-January 13

Re: How to check which item is selected in a datagridview combobox?

Posted 22 January 2013 - 04:39 AM

with the wrong status index i mean if your (status ia at column2) in
your datagrid then (index would be 1)

in the similar way if (status is at column3) then (index 2)
Was This Post Helpful? 0
  • +
  • -

#11 thanzeem7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 14-January 13

Re: How to check which item is selected in a datagridview combobox?

Posted 29 January 2013 - 01:58 AM

View Posterik venn, on 22 January 2013 - 04:31 AM, said:

it seems like you are doing something wrong... your status index seems to be wrong...

im not sure but try this

Dim status As Double 

     For x As Integer = 0 To ATCGRID.Rows.Count - 1  

         If ATCGRID.Rows(x).Cells(1).Value = "Full" Then 

             status = 1  

         ElseIf ATCGRID.Rows(x).Cells(1).Value = "Empty" Then 

             status = 0  

         ElseIf ATCGRID.Rows(x).Cells(1).Value = "Half" Then 

             status = 0.5  
            else
            status = 2
         End If 
         MessageBox.Show(status)  

     Next x 




MessageBox shows 2 only...... :butbut:
Was This Post Helpful? 0
  • +
  • -

#12 kai_itz me  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 159
  • Joined: 03-August 12

Re: How to check which item is selected in a datagridview combobox?

Posted 29 January 2013 - 02:34 AM

can you post snapshot of your datagrid.. if u can
Was This Post Helpful? 0
  • +
  • -

#13 thanzeem7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 14-January 13

Re: How to check which item is selected in a datagridview combobox?

Posted 29 January 2013 - 06:27 AM

View Postkai_itz me, on 29 January 2013 - 02:34 AM, said:

can you post snapshot of your datagrid.. if u can

Posted Image
Was This Post Helpful? 0
  • +
  • -

#14 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: How to check which item is selected in a datagridview combobox?

Posted 29 January 2013 - 06:57 AM

Why are you checking Cells(1) in that code. The combobox resides in cell index 2 Nevermind, that wasn't your code I was looking at.

Try this
For x as Integer = 0 to 5
  MsgBox(ATCGRID.Rows(x).Cells(2).Value)
Next


this should report what is in the cells for the first 6 values

Do they show the correct values you expect?

This post has been edited by CharlieMay: 29 January 2013 - 07:02 AM

Was This Post Helpful? 0
  • +
  • -

#15 kai_itz me  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 159
  • Joined: 03-August 12

Re: How to check which item is selected in a datagridview combobox?

Posted 29 January 2013 - 10:11 PM

@thanzeem thankyou.. i just want to check the index of your column(status),thats why i asked for snapshot.. index is 2.

Also Mr.CharlieMay has already solve your problem.. you can use the above code that will work for you

' it will show (5 values) as your grid shows u need only 5
For x as Integer = 0 to 4
  MsgBox(ATCGRID.Rows(x).Cells(2).Value)
Next





In Order to Check status for A particular value use below code

Dim status As Double 

     For x As Integer = 0 To 4  

         If ATCGRID.Rows(x).Cells(2).Value = "Full" Then 

             status = 1  

         ElseIf ATCGRID.Rows(x).Cells(2).Value = "Empty" Then 

             status = 0  

         ElseIf ATCGRID.Rows(x).Cells(2).Value = "Half" Then 

             status = 0.5  
            else
            
          status = 2
         End If 
         MessageBox.Show(status)  




STATUS WILL BE APPEARED AS 2 IF THERE EXITS ANY VALUE OTHER THAN (FULL , HALF, EMPTY)

This post has been edited by kai_itz me: 29 January 2013 - 11:32 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2