help with datagrid code

In this forums, you can get help for any programming language.

help with datagrid code

Postby pharaon » Sat Jan 23, 2010 3:39 am


this is the code and it work fine

Code: Select all
Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
        Dim sql1 As String = "select Verse1 from Bible where ChapterNum =" & Val(ComboBox2.Text)
        Dim data2 As New OleDbDataAdapter(sql1, CON)
        Dim ds1 As New DataSet
        ds1.Clear()
        data2.Fill(ds1, "Bible")
        CON.Close()
        DataGridView1.DataSource = ds1
        DataGridView1.DataMember = "Bible"


but the problem is that the datagrid display the whole verses column for the whole bible
and when i change the code to

Code: Select all
DataGridView1.DataMember = ("Bible,ChapterNum")


the datagrid display nothing
how to fix this and make the datagrid display the verses for chapter i only choose in combobox2 not the verses for the whole bible
pharaon
imFiles Newbie
imFiles Newbie
 
Posts: 48
Joined: Sat May 30, 2009 4:39 pm

Re: help with datagrid code

Postby autopilot » Sat Jan 23, 2010 12:21 pm

what is the DB you are connecting to? if you only want a single verse, wouldn't it be better to query the DB for just the verse and not the whole chapter?
User avatar
autopilot
Forum Moderator
Forum Moderator
 
Posts: 356
Joined: Sat Sep 23, 2006 7:19 pm

Re: help with datagrid code

Postby pharaon » Sat Jan 23, 2010 2:54 pm

it's access 2003 database
i dont want to display only 1 verse
i want to display the verses of the chapter i choose in the combobox2 which could be 30 to 50 verse for each chapter
but the data grid display the whole bible verses which is 31102
pharaon
imFiles Newbie
imFiles Newbie
 
Posts: 48
Joined: Sat May 30, 2009 4:39 pm

Re: help with datagrid code

Postby autopilot » Sun Jan 24, 2010 7:05 pm

The problem was in your SQL Querys. I changed several things. First, rather then using booknum, I set it up to use EnglishName to fill ComboBox1. Then I changed it to display verses based on selected EnglishName and ChapterNum. Here is the changed code with comments on my changes:
Code: Select all
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' This is not needed
        'CON.Open()
        ' Added EnglishName to the Query
        Dim sql As String = "SELECT ArabicName,EnglishName FROM BibleBookNames"
        Dim Data1 As New OleDbDataAdapter(sql, CON)
        Dim DS As New DataSet
        DS.Clear()
        Data1.Fill(DS, "BibleBookNames")
        ComboBox1.DataSource = DS
        ' Changed the ValueMember to display EnglishName instead of BookNum
        ComboBox1.ValueMember = ("BibleBookNames.EnglishName")
        ' This is not needed
        'CON.Close()
    End Sub
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        ' Use Try-Catch to avoid problems with null returns
        Try
            ' Redefined the SQL Query to pull chapers per book name
            Dim sql1 As String = "SELECT DISTINCT Bible.ChapterNum " & _
            "FROM Bible INNER JOIN BibleBookNames ON Bible.BookNum = BibleBookNames.BookNum " & _
            "WHERE (((BibleBookNames.EnglishName)=""" & ComboBox1.Text & """));"

            Dim data2 As New OleDbDataAdapter(sql1, CON)
            Dim ds1 As New DataSet
            ds1.Clear()
            data2.Fill(ds1, "Bible")
            Dim s As String = ds1.Tables.Item(0).TableName
            ComboBox2.DataSource = ds1
            ComboBox2.ValueMember = "Bible.ChapterNum"
        Catch
        End Try
    End Sub
    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
        ' Use Try-Catch to avoid problems with null returns
        Try
            ' Redefined Query to pull Verses based on book name & chapter selected
            Dim sql1 As String = "SELECT Bible.Verse2 " & _
            "FROM Bible INNER JOIN BibleBookNames ON Bible.BookNum = BibleBookNames.BookNum " & _
            "WHERE (((BibleBookNames.EnglishName)=""" & ComboBox1.Text & """) AND ((Bible.ChapterNum)=" & ComboBox2.Text & "));"

            Dim data2 As New OleDbDataAdapter(sql1, CON)
            Dim ds1 As New DataSet
            ds1.Clear()
            data2.Fill(ds1, "Bible")
            CON.Close()
            DataGridView1.DataSource = ds1
            DataGridView1.DataMember = "Bible"
        Catch
        End Try
    End Sub
User avatar
autopilot
Forum Moderator
Forum Moderator
 
Posts: 356
Joined: Sat Sep 23, 2006 7:19 pm

Re: help with datagrid code

Postby pharaon » Sun Jan 24, 2010 9:36 pm

THANKS so much auto you been great help to me man really helpful
but i dont understand why you put this in the combobox1 code

Dim s As String = ds1.Tables.Item(0).TableName

can you explain that code to me please
thanks alot agin
pharaon
imFiles Newbie
imFiles Newbie
 
Posts: 48
Joined: Sat May 30, 2009 4:39 pm

Re: help with datagrid code

Postby autopilot » Sun Jan 24, 2010 10:37 pm

sorry that was for my testing and learning. forgot to remove it... it is not used or needed.
User avatar
autopilot
Forum Moderator
Forum Moderator
 
Posts: 356
Joined: Sat Sep 23, 2006 7:19 pm

Re: help with datagrid code

Postby pharaon » Mon Jan 25, 2010 3:31 pm

ok man
any way what i want to do with this program is to send bible verses to paltalk room
your tutorial about sending text to room is good it's what i'll use but what standing in front of my now is
i want to get the value of the datagridview cell into textbox or richtextbox so i can send it to the paltalkroom
i used this code
Code: Select all
    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        TextBox1.Text = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
    End Sub

but the problem is i have to click each cell with the mouse so it's text appear in the textbox which is really annoying
i want to make button in the program when i click on it
it send the current textbox text to the paltalk room and also move the selection to the next datagridview cell and make the textbox read the value without click on the cell each time

you got my idea?
pharaon
imFiles Newbie
imFiles Newbie
 
Posts: 48
Joined: Sat May 30, 2009 4:39 pm

Re: help with datagrid code

Postby autopilot » Thu Jan 28, 2010 10:46 pm

i think this is what you want to do:

add button1 to the form
Code: Select all
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        DataGridView1.CurrentCell = DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex + 1).Cells(DataGridView1.CurrentCell.ColumnIndex)
        TextBox1.Text = DataGridView1.CurrentCell.Value
    End Sub

You will still have to code how to send it to the room.

Also note change in this event (last line before catch):
Code: Select all
    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
        ' Use Try-Catch to avoid problems with null returns
        Try
            ' Redefined Query to pull Verses based on book name & chapter selected
            Dim sql1 As String = "SELECT Bible.Verse2 " & _
            "FROM Bible INNER JOIN BibleBookNames ON Bible.BookNum = BibleBookNames.BookNum " & _
            "WHERE (((BibleBookNames.EnglishName)=""" & ComboBox1.Text & """) AND ((Bible.ChapterNum)=" & ComboBox2.Text & "));"

            Dim data2 As New OleDbDataAdapter(sql1, CON)
            Dim ds1 As New DataSet
            ds1.Clear()
            data2.Fill(ds1, "Bible")
            CON.Close()
            DataGridView1.DataSource = ds1
            DataGridView1.DataMember = "Bible"
            'Add this to update the textbox content when the datagrid is updated
            TextBox1.Text = DataGridView1.CurrentCell.Value
        Catch
        End Try
    End Sub
User avatar
autopilot
Forum Moderator
Forum Moderator
 
Posts: 356
Joined: Sat Sep 23, 2006 7:19 pm

Re: help with datagrid code

Postby autopilot » Fri Jan 29, 2010 11:55 pm

also, you will need to add some code to handle what to do when it gets to the last verse in a chapter.
User avatar
autopilot
Forum Moderator
Forum Moderator
 
Posts: 356
Joined: Sat Sep 23, 2006 7:19 pm

Re: help with datagrid code

Postby pharaon » Sun Jan 31, 2010 10:13 pm

ok i did that thanks
can you tell me how to make the verse number and the verse both of them display in the textbox..so when i send to the room i want to send the verse number and the verse and if it possible to send also the book name and the chapter number for example
Code: Select all
 [ Gn:1:1 ]-[ In the beginning God created the heaven and the earth. ]
pharaon
imFiles Newbie
imFiles Newbie
 
Posts: 48
Joined: Sat May 30, 2009 4:39 pm


Return to Programming Help

 


  • Related topics
    Replies
    Views
    Last post

Who is online

Users browsing this forum: No registered users and 0 guests