Question How do I show the selected cell in textboxes?

stresss

Member
Joined
Aug 11, 2008
Messages
21
Programming Experience
Beginner
Hi all!

Okay, this is how my form work.

I have
  • a bounded Datagridview with a table which have 3 fields, MsgID, Message and Type ( Single or Dual ),
  • and two MsgID textboxes,
  • 2 Message textboxes,
  • 2 radio buttons ( Single & Dual ).
I have two panels, namely Top ( to display the top msg) and Bottom ( to display the bottom msg ), each panel have 1 MsgID textbox, 1 Message textbox.

When I select the Single radio button, it will disable the Bottom panel and when I click on the cell ( the CellClick event) , it will only display data into the Top panel and only if the Type is 'Single'. Then when I select the Dual radiobutton, the bottom part will be enabled. So when I select a cell from the datagridview, if the Type = Single, it will display all data in the Top panel, whereas if the Type = Dual, it will display all data in the Bottom panel.

I am having this problem for quite some time already and I still can't solve it, so your help would be much appreciated! :D. Thanks a million in advance!.
 
Hello.

Where is your problem? Can't you figure out how this works?

This is a simple If statement using the Events of the Radiobuttons. Depending on which gets checked you set the Bottom Panel Visible or True or False. Within the DataGridView Event you determine if the lower Panel is visible or not (or you use the radiobuttons).

Bobby
 
The problem I'm having right is that when let's say, I select a cell with Type = "Single", it will display in both Panels, which is not what I want. I want those data with Type = "Single" to be displayed in the Top panel, and those data with Type = "Dual" to be displayed in the Bottom panel.
 
Hmm as in the data type? it's nvarchar.

Below is the example of my codes. Hope it can give you a better idea :).


VB.NET:
Dim mySQL As String
        Dim mySQL2 As String

        Dim sin As String = "Single"
        If rbSingle.Checked = True Then
            sin = sin & rbSingle.Text
        End If

        Dim dua As String = "Dual"
        If rbDual.Checked = True Then
            dua = dua & rbDual.Text
        End If

        'If SelCategory Is Nothing Then

        '    MessageBox.Show("Please select a category")

        'End If

        Dim connOver As New SqlConnection _
        ("data source=MCCCU-PC13; initial catalog=BPLRT;" & _
        "integrated security=true")


        Dim dr As SqlDataReader
        Dim RecordCount As Integer


        Try

            connOver.Open()

            mySQL = "select * from CreationMessage where Type= '" & Trim(sin) & "'"
            mySQL2 = "select * from CreationMessage where Type = '" & Trim(dua) & "'"

            Dim cmd As New SqlCommand(mySQL & mySQL2, connOver)

            dr = cmd.ExecuteReader

            While dr.Read()

                If dr("Type") = sin Then

                    Dim i As Integer

                    i = DataGridView1.CurrentRow.Index

                    txtMsgID.Text = DataGridView1.Item(0, i).Value
                    txtDuration.Text = DataGridView1.Item(1, i).Value
                    ComboStyle.Text = DataGridView1.Item(4, i).Value
                    ComboFF.Text = DataGridView1.Item(5, i).Value
                    ComboJustify.Text = DataGridView1.Item(6, i).Value
                    txtMsgText.Text = DataGridView1.Item(7, i).Value

                    RecordCount = RecordCount + 1

                End If
            End While

            dr.NextResult()
            RecordCount = 0

            While dr.Read()

                If dr("Type") = dua Then

                    Dim p As Integer

                    p = DataGridView1.CurrentRow.Index

                    txtMsgID1.Text = DataGridView1.Item(0, p).Value
                    txtDuration.Text = DataGridView1.Item(1, p).Value
                    ComboStyle1.Text = DataGridView1.Item(4, p).Value
                    ComboFF1.Text = DataGridView1.Item(5, p).Value
                    ComboJustify1.Text = DataGridView1.Item(6, p).Value
                    txtMsgText1.Text = DataGridView1.Item(7, p).Value

                    RecordCount = RecordCount + 1

                End If

            End While

            dr.Close()
            connOver.Close()
        Catch ex As Exception

        End Try
 
Ahhh...that's not working that way ;)

As far as I know are NextResult() and Read() doing the same thing, setting the Reader to the next dataset.

Try this instead:
VB.NET:
' $code_above

dr = cmd.ExecuteReader

            While dr.Read()
                    Dim i As Integer = DataGridView1.CurrentRow.Index

                If dr("Type") = sin Then
                    txtMsgID.Text = DataGridView1.Item(0, i).Value
                    txtDuration.Text = DataGridView1.Item(1, i).Value
                    ComboStyle.Text = DataGridView1.Item(4, i).Value
                    ComboFF.Text = DataGridView1.Item(5, i).Value
                    ComboJustify.Text = DataGridView1.Item(6, i).Value
                    txtMsgText.Text = DataGridView1.Item(7, i).Value
                Else
                    txtMsgID1.Text = DataGridView1.Item(0, i).Value
                    txtDuration.Text = DataGridView1.Item(1, i).Value
                    ComboStyle1.Text = DataGridView1.Item(4, i).Value
                    ComboFF1.Text = DataGridView1.Item(5, i).Value
                    ComboJustify1.Text = DataGridView1.Item(6, i).Value
                    txtMsgText1.Text = DataGridView1.Item(7, i).Value
                End If

                RecordCount = RecordCount + 1
            End While

' $code_beyond

Bobby

P.s.: Why are you using the Datareader anyway? You never read from it...
 
Last edited:
Hmmm. it's still not working, it didn't loop into the Else part. Cus I put messageboxes to show where did it stop, and it stops just before the Else part. I tried selecting cells with type "Single" and also with type "Dual", but I could only show it on the top panel, even when it's Dual.

My previous code was able to loop until the end of the While loop, but it displayed same data, be it Single or Dual, at the Top and Bottom Panel. Any idea why?

Sorry to trouble you again man!

ps: oh.. I tot I am reading from the Datareader. Haha. :). How do I read from it?
 
Last edited:
Use Debug.Print to let you print the Value of dr("Type") during runtime. Maybe he's missing String Comparison...try add a .ToString() to the DataReader.

Something else I just found is your query.
VB.NET:
            mySQL = "select * from CreationMessage where Type= '" & Trim(sin) & "'"
            mySQL2 = "select * from CreationMessage where Type = '" & Trim(dua) & "'"

            Dim cmd As New SqlCommand(mySQL & mySQL2, connOver)

It seems to me that this can never work...you put the two queries straight behind each other...isn't this throwing an Exception? It should 'cause the syntax is not correct.

VB.NET:
            mySQL = "select * from CreationMessage where Type= '" & Trim(sin) & "'"
            mySQL2 = " and Type = '" & Trim(dua) & "'"

            Dim cmd As New SqlCommand(mySQL & mySQL2, connOver)

I was wrong about the DataReader...you read from it for the If statement.

But this seems a little odd to me...you only need the DataReader to determine what is Single or Dual...the rest comes from a DataGridView. Maybe it would be easier for you to pull a invisible column into the DataGridView which is holding the type, so that you can through the DataReader out of the window.

Bobby
 
I changed the SQL statements but when i clicked a cell to show, nothing comes out. Hmmm.. how do i use Debug.Print or .ToString?

I was thinking is it because the Type column is not the primary key, that's why it can't read?

I'm not sure whether is my datareader can't read it or there's a problem with my SQL statement. I tried to switch from this:

mySQL = "select * from CreationMessage where Type= '" & Trim(sin) & "'"
mySQL2 = "select * from CreationMessage where Type = '" & Trim(dua) & "'"

to this:

mySQL = "select * from CreationMessage where Type= '" & Trim(dua) & "'"
mySQL2 = "select * from CreationMessage where Type = '" & Trim(sin) & "'"

And after the change, when I clicked on a cell to show, it showed it at the Bottom panel, when previously, it shown only in the Top Panel.

So I'm abit confused now.

Sorry for the trouble again! I'm a noob in this. :eek:
 
As I said...as far as I can see it this SQL-Statement is not correct! At least not if you put them straight behind each other.

Try instead of the two SQL-Statements a single one whcih is looking liks this:
VB.NET:
"select * from CreationMessage where Type= '" & Trim(sin) & "' and Type = '" & Trim(dua) & "'"

Bobby
 
Hey sorry, was away for 3 days. Hmmm I changed it to single SQL statement as you stated, but it couldn't work, it can't display the selected cell. I think the datareader didn't read it as it didn't even start the while loop.

I wonder is there a problem with my datareader? or is it my data table? Sorry for the never ending questions!
 
Back
Top