Doubt in datagrid view

prakash77

Active member
Joined
Mar 12, 2010
Messages
29
Programming Experience
Beginner
hi friends,
This s one part of code in my lib proj for find bookrecord..... But i find one difficult in this.. when i select the record based on the BOOKID which s matched my id i want to display that record in a datagrid view alone. How can i set a single record to the datagrid view??

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim c As String
c = InputBox("Enter the ID of the Book", "Search Box")
Dim cmd As New OleDbCommand("select * from bookdetails where bookid=" & c, con)
Dim dr As OleDbDataReader = cmd.ExecuteReader()
How can i set a single matched record in my datagrid view ??????
??????
End Sub

Pls Help me friends..... waiting for ur reply....:D
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,493
Location
Sydney, Australia
Programming Experience
10+
How do you show multiple records in a DataGridView? You populate a DataTable and bind it to the grid. How do you show one record in a DataGridView? The same way. You can use a DataAdapter and call Fill or you can use a DataReader and call Load on the DataTable.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,493
Location
Sydney, Australia
Programming Experience
10+
I mean what I said: have you ever populated a DataTable? If you have used a TableAdapter then you have, because that's what TableAdapters are for. That said, a TableAdapter encapsulates the Connection and DataAdapter. You've already got a DataReader though, so just use that. As I said, call Load on your DataTable. In order to have a DataTable, you must create one. Then you can call Load and pass the DataReader. The table will then be populated with the data from the reader.
 

prakash77

Active member
Joined
Mar 12, 2010
Messages
29
Programming Experience
Beginner
Still doesnt load into datagrid

I created a table..but still it doesnt load into gridview...why??help pls??


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Table1 As DataTable
Table1 = New DataTable("bookdetails1")
Dim Row1 As DataRow
Try
Dim sno As DataColumn = New DataColumn("SNo")
sno.DataType = System.Type.GetType("System.String")
Table1.Columns.Add(sno)

Dim bookid As DataColumn = New DataColumn("Bookid")
bookid.DataType = System.Type.GetType("System.String")
Table1.Columns.Add(bookid)

Dim bookname As DataColumn = New DataColumn("Bookname")
bookname.DataType = System.Type.GetType("System.String")
Table1.Columns.Add(bookname)

Dim authorname As DataColumn = New DataColumn("Authorname")
authorname.DataType = System.Type.GetType("System.String")
Table1.Columns.Add(authorname)

Dim publishername As DataColumn = New DataColumn("Publishername")
publishername.DataType = System.Type.GetType("System.String")
Table1.Columns.Add(publishername)

Dim category As DataColumn = New DataColumn("Category")
category.DataType = System.Type.GetType("System.String")
Table1.Columns.Add(category)

Dim price As DataColumn = New DataColumn("Price")
price.DataType = System.Type.GetType("System.String")
Table1.Columns.Add(price)

Dim copies As DataColumn = New DataColumn("Totalcopies")
copies.DataType = System.Type.GetType("System.String")
Table1.Columns.Add(copies)

Dim available As DataColumn = New DataColumn("Available")
available.DataType = System.Type.GetType("System.String")
Table1.Columns.Add(available)

Catch ex As Exception
MsgBox("Table couldnt be created", MsgBoxStyle.Information, "INFORMATION")
End Try

Try
con.Open()
Dim c As String
c = InputBox("Enter the ID of the Book", "Search Box")
If c = "" Then
MsgBox("Please Specify the BOOK ID")
Else
Dim cmd As New OleDbCommand("select * from bookdetails where bookid='" & c & "'", con)
Dim dr As OleDbDataReader = cmd.ExecuteReader()
While dr.Read
row1.Item("SNo") = dr(0)
row1.Item("Bookid") = dr(1)
row1.Item("Bookname") = dr(2)
row1.Item("Authorname") = dr(3)
row1.Item("Publishername") = dr(4)
row1.Item("Category") = dr(5)
row1.Item("Price") = dr(6)
row1.Item("Totalcopies") = dr(7)
row1.Item("Available") = dr(8)
Table1.Rows.Add(row1)
End While
Dim ds As New DataSet
ds.Tables.Add(Table1)
DataGridView1.<<<<<<<<<<<?????
End If
Catch ex As Exception
MsgBox("The ID is INVALID", MsgBoxStyle.Information, "INFORMATION")
Finally
con.Close()
End Try
End Sub
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,493
Location
Sydney, Australia
Programming Experience
10+
I really bothers me when people ask for help and then disregard the advice provided. I said:
In order to have a DataTable, you must create one. Then you can call Load and pass the DataReader.
Here's where you're creating the DataTable:
VB.NET:
Dim Table1 As DataTable
Table1 = New DataTable("bookdetails1")
Now, where are you calling Load and passing the DataReader? All the rest of that code is unnecessary. All you have to do is call Load on the DataTable and pass the DataReader, exactly as I said. That will build the schema and load the data. There are plenty of examples online that I can see:

datatable load datareader vb.net - Google Search

I also said:
You populate a DataTable and bind it to the grid.
Again, a simple search would provide the answers required:

bind datatable to datagridview vb.net - Google Search
 
Top Bottom