To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
Article :: Building Dynamic Systems with Expressions in .NET
How Is XML Like An Interface?
Understanding Covariance and Contravariance
Print VS 2010 Keyboard Shortcut References in Letter (8.5x11in) and A4 (210×297mm) Sizes
Updated Productivity Power Tools



Go Back   VBForums > Visual Basic > Visual Basic .NET

Reply Post New Thread
 
Thread Tools Display Modes
Old Nov 2nd, 2006, 03:49 AM   #1
met0555
Frenzied Member
 
met0555's Avatar
 
Join Date: Jul 06
Posts: 1,186
met0555 is an unknown quantity at this point (<10)
read a row from database

hi, well i want to know how can i read from a datagrid view a row and show it in a richtext box???


tanks
met0555 is offline   Reply With Quote
Old Nov 2nd, 2006, 03:57 AM   #2
Kimmy4
Fanatic Member
 
Join Date: Aug 06
Posts: 734
Kimmy4 is on a distinguished road (20+)
Re: read a row from database

Hi there,
How many rows does your dgv contain? And do you just want to read the first row?
Kimmy4 is offline   Reply With Quote
Old Nov 2nd, 2006, 04:06 AM   #3
Kimmy4
Fanatic Member
 
Join Date: Aug 06
Posts: 734
Kimmy4 is on a distinguished road (20+)
Re: read a row from database

I've just tested this code to a messagebox and it works
VB Code:
  1. Dim sb As New StringBuilder()
  2.         Dim row As DataGridViewRow = Me.DataGridView1.Rows(0) 'Reads from the first row
  3.         For Each column As DataGridViewColumn In DataGridView1.Columns
  4.             sb.Append(row.Cells(column.Index).Value.ToString)
  5.             sb.Append(ControlChars.NewLine)
  6.         Next
  7.         RichTextBox1.Text = sb.Tostring
So i'm guessing it will work with a richtextbox. If you need to read from everyrow in your dgv then you'll need to replace the line
VB Code:
  1. Dim row as DatagridviewRow = datagridview1.rows(0)
with the code
VB Code:
  1. For each row as Datagridviewrow in datagridview1.rows
  2. next
and place it around the for loop for the columns.
Hope this helps.

________________________________________________________________________
If i've helped then please rate this post! Thanx
Kimmy4 is offline   Reply With Quote
Old Nov 2nd, 2006, 04:43 AM   #4
Kimmy4
Fanatic Member
 
Join Date: Aug 06
Posts: 734
Kimmy4 is on a distinguished road (20+)
Re: read a row from database

if this has solved your problem please don't forget to mark the thread [RESOLVED]!
Kimmy4 is offline   Reply With Quote
Old Nov 2nd, 2006, 05:46 AM   #5
met0555
Frenzied Member
 
met0555's Avatar
 
Join Date: Jul 06
Posts: 1,186
met0555 is an unknown quantity at this point (<10)
Re: read a row from database

i think it's what i needed it. but u see what i wanna do is kind of library so when the user click on a row ex. the music title i want to show the lyrics on the rich text box. and also when he click on other music title i want to show the music lyrics of that music on richtext box so i think that i have to put this
VB Code:
  1. For each row as Datagridviewrow in datagridview1.rows
  2. next
???

Last edited by met0555; Nov 2nd, 2006 at 05:50 AM.
met0555 is offline   Reply With Quote
Old Nov 2nd, 2006, 12:08 PM   #6
Kimmy4
Fanatic Member
 
Join Date: Aug 06
Posts: 734
Kimmy4 is on a distinguished road (20+)
Re: read a row from database

Hi, sorry about the long delay in replying.
So just to clear things up. The user will click on a row. Will this be the row header or just a cell in that row? Then you want a richtextbox to display all the text from the rest of the columns in that row? When the user clicks on another row do you want it to replace the text currently in the richtextbox or display that text in a new richtextbox?
Also could you just explain to me the layout of your dgv....for example how many columns and rows it has (if known) and whether you know the names of the columns and rows or whether you need to use the index of them?
If you could just give me as much detail and information as possible because i dont quite understand what exactly you need. Thanks
Kimmy4 is offline   Reply With Quote
Old Nov 2nd, 2006, 12:27 PM   #7
met0555
Frenzied Member
 
met0555's Avatar
 
Join Date: Jul 06
Posts: 1,186
met0555 is an unknown quantity at this point (<10)
Re: read a row from database

ok, here it is how it will work : it's a lyrics library that i'm making it have 5 coulmn Artist, Title, Genre, Album, Lyrics . the form will be divided into two , with a datagrid and with a richtextbox and some buttons to go back... . on the datagrid view we will only see the Artist, Title Genre and Album the lyrics will be shown on the datagrid view. so when the user change the artist name the lyrics will have to match with the artist and music title name. i think i'm clear but if u need addtional info just ask me.

tanks
met0555 is offline   Reply With Quote
Old Nov 2nd, 2006, 12:43 PM   #8
stanav
PowerPoster
 
stanav's Avatar
 
Join Date: Jul 06
Location: Providence, RI - USA
Posts: 7,309
stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)
Re: read a row from database

If you're not already loaded your data table at form_load, it's just a mater of creating a select statement with appropriate criteria (artist name, song title...) and query the database to get the lyrics. If the lyrics is already in your data table, then just load it to the text box base on the selected row on the datagridview.
stanav is offline   Reply With Quote
Old Nov 2nd, 2006, 12:51 PM   #9
met0555
Frenzied Member
 
met0555's Avatar
 
Join Date: Jul 06
Posts: 1,186
met0555 is an unknown quantity at this point (<10)
Re: read a row from database

----If the lyrics is already in your data table, then just load it to the text box base on the selected row on the datagridview. ---- i'm on this case, well, my problem is i don't know the code to retrive a specific data from the database and show it in the rich text box. the lyrics is in comlumn 5
met0555 is offline   Reply With Quote
Old Nov 3rd, 2006, 03:43 AM   #10
Kimmy4
Fanatic Member
 
Join Date: Aug 06
Posts: 734
Kimmy4 is on a distinguished road (20+)
Re: read a row from database

If the lyrics are already in your data table then you could use the code
VB Code:
  1. Dim rowIndex as integer = DataGridView1.CurrentCellAddress.Y 'This will get the row index of the current selected cell
  2. Dim row as datagridviewrow = datagridview1.rows(rowIndex)
  3. dim lyrics as string = row.cells(4).value.tostring 'Get the value of the fifth column on the row selected
  4. RichTextBox1.clear()
  5. Richtextbox1.text = lyrics

In order to query the database you would need to use the select statement:

select lyrics from tablename where artist = 'cellvalue in artist column' and title = 'cellvalue in title column' and album = 'cellvalue in album column'

and then display the results in the richtextbox. You can get the values of each cell by using the same method as above but changing the column index from 5 to whatever column index is relevant. Don't forget the column indices start from 0 so the 1st column has an index of 0, the 2nd column has an index of 1 etc....

Hope this helps

Last edited by Kimmy4; Nov 3rd, 2006 at 04:18 AM.
Kimmy4 is offline   Reply With Quote
Old Nov 3rd, 2006, 05:48 AM   #11
met0555
Frenzied Member
 
met0555's Avatar
 
Join Date: Jul 06
Posts: 1,186
met0555 is an unknown quantity at this point (<10)
Re: read a row from database

ok, tanks, but there is something that i didn't understand. if i only put this code will it work Dim rowIndex as integer = DataGridView1.CurrentCellAddress.Y 'This will get the row index of the current selected cell
VB Code:
  1. Dim row as datagridviewrow = datagridview1.rows(rowIndex)
  2. dim lyrics as string = row.cells(4).value.tostring 'Get the value of the fifth column on the row selected
  3. RichTextBox1.clear()
  4. Richtextbox1.text = lyrics

or i have to put the select.... ? and if i need a select statment , do i only need one or more?

(The lyrics are allready stored i the database)
met0555 is offline   Reply With Quote
Old Nov 3rd, 2006, 05:58 AM   #12
Kimmy4
Fanatic Member
 
Join Date: Aug 06
Posts: 734
Kimmy4 is on a distinguished road (20+)
Re: read a row from database

Do you actually import the lyrics to the dgv or do u need to get them from the database? and if you do import them to the dgv then are the visible?
Kimmy4 is offline   Reply With Quote
Old Nov 3rd, 2006, 06:02 AM   #13
met0555
Frenzied Member
 
met0555's Avatar
 
Join Date: Jul 06
Posts: 1,186
met0555 is an unknown quantity at this point (<10)
Re: read a row from database

actualy i import it from the database
met0555 is offline   Reply With Quote
Old Nov 3rd, 2006, 06:02 AM   #14
Kimmy4
Fanatic Member
 
Join Date: Aug 06
Posts: 734
Kimmy4 is on a distinguished road (20+)
Re: read a row from database

so are the lyrics visible in the dgv?
Kimmy4 is offline   Reply With Quote
Old Nov 3rd, 2006, 06:07 AM   #15
met0555
Frenzied Member
 
met0555's Avatar
 
Join Date: Jul 06
Posts: 1,186
met0555 is an unknown quantity at this point (<10)
Re: read a row from database

nop, they are not visible. on the datagrid view only the title ... are visible . but if we can make it visible but hide it to the user i think it will make simple the code.
met0555 is offline   Reply With Quote
Old Nov 3rd, 2006, 06:09 AM   #16
Kimmy4
Fanatic Member
 
Join Date: Aug 06
Posts: 734
Kimmy4 is on a distinguished road (20+)
Re: read a row from database

is there any chance you could post part of your code that imports to the dgv so i can see how you are doing it. Then i could advise you on how to do display them in the richtextbox. Because if the lyrics can be accessed without having to do a select statement then that would be easier.
Kimmy4 is offline   Reply With Quote
Old Nov 3rd, 2006, 06:17 AM   #17
met0555
Frenzied Member
 
met0555's Avatar
 
Join Date: Jul 06
Posts: 1,186
met0555 is an unknown quantity at this point (<10)
Re: read a row from database

well i didn't connet yet the program with the database, but the code that i will you it the one that u gave me.
VB Code:
  1. Dim row as datagridviewrow = datagridview1.rows(rowIndex)
  2. dim lyrics as string = row.cells(4).value.tostring 'Get the value of the fifth column on the row selected
  3. RichTextBox1.clear()
  4. Richtextbox1.text = lyrics
met0555 is offline   Reply With Quote
Old Nov 3rd, 2006, 06:20 AM   #18
Kimmy4
Fanatic Member
 
Join Date: Aug 06
Posts: 734
Kimmy4 is on a distinguished road (20+)
Re: read a row from database

the code i have supplied gets the values from the dgv AFTER all the information has been imported from the database. Could you post all of your code could i dont understand what you have already done so cant tell you what you need to do next. Where does all the information (ie the title, artist, album etc.) come from in the first place?
Kimmy4 is offline   Reply With Quote
Old Nov 3rd, 2006, 06:27 AM   #19
met0555
Frenzied Member
 
met0555's Avatar
 
Join Date: Jul 06
Posts: 1,186
met0555 is an unknown quantity at this point (<10)
Re: read a row from database

well, i did the database with 5 column , and on the program i added the richtextbox and when i connect the database i will add the datagrid view. the title genre... will come from the database and will be show on the datagrid view. there is no big code that i put in the program form1.show me.hide...)
met0555 is offline   Reply With Quote
Old Nov 3rd, 2006, 06:30 AM   #20
Kimmy4
Fanatic Member
 
Join Date: Aug 06
Posts: 734
Kimmy4 is on a distinguished road (20+)
Re: read a row from database

So is there anything currently in the datagridview? Do you currently know how to connect to the database? What database do you need to connect to (are you using oracle/excel/something different to get all the information from?)?
Kimmy4 is offline   Reply With Quote
Old Nov 3rd, 2006, 06:44 AM   #21
met0555
Frenzied Member
 
met0555's Avatar
 
Join Date: Jul 06
Posts: 1,186
met0555 is an unknown quantity at this point (<10)
Re: read a row from database

well ya i connect correctly, i just connected but, i was sking my self if i have to add the lyrics to the datagrid view or not. U see i want the lyrics to be view only on the richtextbox . i did it with acces 2003.
met0555 is offline   Reply With Quote
Old Nov 3rd, 2006, 06:51 AM   #22
Kimmy4
Fanatic Member
 
Join Date: Aug 06
Posts: 734
Kimmy4 is on a distinguished road (20+)
Re: read a row from database

OK. I think i understand now. What you can do is add the lyrics to the datagridview but then set that column's visible property to false. This way ithey will be in the dgv but the user wont be able to see them. Then you should probably create a button and call it 'Get lyrics' and inside the button place the code to get the lyrics from the dgv. This way you wont have to continually make a connection to get the lyrics because they will always be there (that just wont be apparent to the user). And because you are using a button you will only need to use the code once just make sure the user clicks on a cell in the row they require and the current row index will change accordingly. The only thing you will need to change in the code i provided above is that you will need to reference the datagridview by saying form1.datagridview1, so that it knows you are refering to that dgv.
Is this what you needed?
Kimmy4 is offline   Reply With Quote
Old Nov 3rd, 2006, 06:56 AM   #23
met0555
Frenzied Member
 
met0555's Avatar
 
Join Date: Jul 06
Posts: 1,186
met0555 is an unknown quantity at this point (<10)
Re: read a row from database

so finaly the code will be this
VB Code:
  1. Dim row as datagridviewrow = datagridview1.rows(rowIndex)
  2. dim lyrics as string = row.cells(4).value.tostring 'Get the value of the fifth column on the row selected
  3. RichTextBox1.clear()
  4. Richtextbox1.text = lyrics
and i will put this code under a button. and i will not need of any other code like select * from... ??

Last edited by met0555; Nov 3rd, 2006 at 07:40 AM.
met0555 is offline   Reply With Quote
Old Nov 3rd, 2006, 06:59 AM   #24
Kimmy4
Fanatic Member
 
Join Date: Aug 06
Posts: 734
Kimmy4 is on a distinguished road (20+)
Re: read a row from database

No you wont need any sql code because you should have already put all the information into the dgv. You are just getting the code from the dgv instead. Just remember to put the name of the form everytime you use datagridview1 otherwise it wont know which dgv you are refering to.
Kimmy4 is offline   Reply With Quote
Old Nov 3rd, 2006, 07:42 AM   #25
met0555
Frenzied Member
 
met0555's Avatar
 
Join Date: Jul 06
Posts: 1,186
met0555 is an unknown quantity at this point (<10)
Re: read a row from database

ok, tanks man
met0555 is offline   Reply With Quote
Old Nov 3rd, 2006, 07:57 AM   #26
Kimmy4
Fanatic Member
 
Join Date: Aug 06
Posts: 734
Kimmy4 is on a distinguished road (20+)
Re: read a row from database

Does it work?
Kimmy4 is offline   Reply With Quote
Old Nov 3rd, 2006, 08:07 AM   #27
met0555
Frenzied Member
 
met0555's Avatar
 
Join Date: Jul 06
Posts: 1,186
met0555 is an unknown quantity at this point (<10)
Re: read a row from database

i'm adding data to the database, but i soon as i'm done i will let u know
met0555 is offline   Reply With Quote
Old Nov 3rd, 2006, 09:07 AM   #28
stanav
PowerPoster
 
stanav's Avatar
 
Join Date: Jul 06
Location: Providence, RI - USA
Posts: 7,309
stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)
Re: read a row from database

Show me the code you use for the followings:
1. The select statement you use to get data
2. where and how you bind your datagridview to the data table
Also please be clear how you want to display the lyrics... Do you want to display it when the user click on any cell in the grid or only when they actually select a row?
stanav is offline   Reply With Quote
Old Nov 3rd, 2006, 09:24 AM   #29
Kimmy4
Fanatic Member
 
Join Date: Aug 06
Posts: 734
Kimmy4 is on a distinguished road (20+)
Re: read a row from database

Hi stanav,
will it really matter whether they click on the cell or the row as the line
VB Code:
  1. Dim rowIndex as integer = datagridview1.currentcelladdress.y
will give him the same index as would clicking on the row? I'm just asking out of curiosity...would the code i've suggested not work then (i admit i haven't tested it....but it should achieve the results required)
Kimmy4 is offline   Reply With Quote
Old Nov 3rd, 2006, 09:42 AM   #30
met0555
Frenzied Member
 
met0555's Avatar
 
Join Date: Jul 06
Posts: 1,186
met0555 is an unknown quantity at this point (<10)
Re: read a row from database

well i use this for searching

VB Code:
  1. Private Sub butOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles but_ok.Click
  2.         Dim strConnection As String = "Data Source=your SQL data source;Initial Catalog=your database; Integrated Security=True"
  3.         Dim cn As SqlClient.SqlConnection = New SqlClient.SqlConnection(strConnection)
  4.         Dim ds As New DataSet
  5.         Dim strSelect As String
  6.         'strSelect As String
  7.                         strSelect = "SELECT  * FROM " & YourTable & " WHERE [Search Field] = '" & Me.txtName.Text & "'"
  8.         Dim dscmd As New SqlClient.SqlDataAdapter(strSelect, cn)
  9.         dscmd.Fill(ds, "your table")
  10.         Me.Datagrid1.DataSource = ds
  11.         Me.Datagrid1.DataMember = "your table"
  12.         Dim con As Integer
  13.         con = Me.BindingContext(ds, "your table").Count
  14.         If con = 0 Then
  15.             MessageBox.Show("Recourd could not be found")
  16.         End If
  17.     End Sub

2. where and how you bind your datagridview to the data table : if u mean how i connect the dgv with the database table then : i will first connect the database then i will drag and drop it to the form.

well, i want the lyrics to be viewed when a row is clicked on the dgv. the selected row artis name title .. must much with the lyrics that is going to be view on the richtextbox.
met0555 is offline   Reply With Quote
Old Nov 3rd, 2006, 09:46 AM   #31
Kimmy4
Fanatic Member
 
Join Date: Aug 06
Posts: 734
Kimmy4 is on a distinguished road (20+)
Re: read a row from database

Have you tried the code and method that i suggested earlier yet....i'm fairly confident it will give you the results you need?
Kimmy4 is offline   Reply With Quote
Old Nov 3rd, 2006, 09:54 AM   #32
met0555
Frenzied Member
 
met0555's Avatar
 
Join Date: Jul 06
Posts: 1,186
met0555 is an unknown quantity at this point (<10)
Re: read a row from database

i don't think taht i will try it son 'cuz the database will have more then 60000 data in it and to add that it will take time, and also the dtabase will be stored in my resource folder so it's betetter to add then database after i'm donw ith the datas.
met0555 is offline   Reply With Quote
Old Nov 3rd, 2006, 09:55 AM   #33
stanav
PowerPoster
 
stanav's Avatar
 
Join Date: Jul 06
Location: Providence, RI - USA
Posts: 7,309
stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)
Re: read a row from database

OK... So you use "SELECT * FROM..." that means that everything is already in your data table. Now let's suppose that your table have the following fields in it: [ArtistName],[AlbumName],[SongTitle], and [SongLyrics] And you only want to show artistname, albumname, and songtitle on the datagridview. When your user click on any cell in the grid, you want to display the song lyrics on a a richtextbox for the song that is on that row. Is that correct?
stanav is offline   Reply With Quote
Reply

Go Back   VBForums > Visual Basic > Visual Basic .NET


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 07:55 PM.





Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.