[RESOLVED] [2008] datagridview single row
hey guys
how can i make the datagridview contains 1 row?
because i have a toolbar that have two button(forward,backward) and i need the datagridview to be single row because each row is a record in The DB .
and when i pree the forward button i will go to the next record.
and btw, which is better this way or make the datagridview display all the records at once ????
:)
Re: [2008] datagridview single row
Its all according to your requirements. if you want all the records to be displayed then count the rows from the database table and display them or single row.
You can add rows to datagridview this way.
firstly put the datagridview's AllowUsertoAddRows and AllowUsertoDeleteRows property to False then add rows.
vb Code:
'for single row
datagridview.rows.add()
' for multiple rows
'number is the number rows you want to add to datagridview
datagridview.rows.add(number)
Re: [2008] datagridview single row
ok but
i mean i want the datagridview to display only one row (the first record in My DB).
and when i press forward the same row in The Datagridview Will Be Changed the Information To the Second row In The DB.
can i do that ?
thx
Re: [2008] datagridview single row
Re: [2008] datagridview single row
How are you loading the DGV, are you loading all the records from the table or are you calling the db for each record? How are you populating each cell of the DGV?
Count the rows returned from the db and then catch the row number to assign each record. This is how I do it in a form with controls, but the principle is the same.
Code:
At form level declare these two variables
' declare a database cursor to point to the record
Dim dbCursor As Integer = 0
' declare a variable to hold the max number of records
Dim MaxRecords As Integer
' Fill() is the method I use to populate a dataset, call yours whatever you want, remember do not just copy and past this into your form and expect it to work
Private Sub btnFirstRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirstRecord.Click
' error checking
Try
dbCursor = 0
Fill()
Catch ex As Exception
MsgBox("Error occurred in btnFirstRecord - Click event:" & ex.Message)
End Try
End Sub
Private Sub btnPreviousRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreviousRecord.Click
' error checking
Try
If dbCursor <> 0 Then
dbCursor = dbCursor - 1
Fill()
End If
Catch ex As Exception
MsgBox("Error occurred in btnPreviousRecord - Click event:" & ex.Message)
End Try
End Sub
Private Sub btnNextRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNextRecord.Click
' error checking
Try
If dbCursor < MaxRecords - 1 Then
dbCursor = dbCursor + 1
Fill()
End If
Catch ex As Exception
MsgBox("Error occurred in btnNextRecord - Click event:" & ex.Message)
End Try
End Sub
Private Sub btnLastRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLastRecord.Click
' error checking
Try
dbCursor = MaxRecords - 1
Fill()
Catch ex As Exception
MsgBox("Error occurred in btnLastRecord - Click event:" & ex.Message)
End Try
dbCursor = MaxRecords - 1
Fill()
End Sub
Good luck
Re: [2008] datagridview single row
Quote:
Originally Posted by snakeman
ok but
i mean i want the datagridview to display only one row (the first record in My DB).
and when i press forward the same row in The Datagridview Will Be Changed the Information To the Second row In The DB.
can i do that ?
thx
yes,you can do that.
clear all the columns of the datagridview and display the second row.
'to clear all the columns in datagrid
DataGridview.columns.clear()
and then add the row to it and display your content.
Re: [2008] datagridview single row
Normally the DataGridView should show all the records of your database not a single record. What is the purpose to show only one at the time? If that is what you want than you may consider using textboxes and binding them to the database fields so it will contain one record at the time. If you want to have DataGridView instead of textboxs than you need to add and remove the row manually witch will have no sense.
Re: [RESOLVED] [2008] datagridview single row