How do I fill a Datagridview?

C

Co

Hi All,

I'm trying to fill a datagridview from a database but I can't get it
to work.
This is my code:

Private Sub CheckForExistingRoute()

Dim sql As String = "SELECT * FROM Route WHERE bestand=" & CInt
(iD)
Dim strTable As String = "Route"
Dim da As New OleDb.OleDbDataAdapter(sql, conn)
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim i As Integer = 1
Dim ds As New DataSet
conn.Open()

cb.QuotePrefix = "["
cb.QuoteSuffix = "]"

Try
da.SelectCommand = New OleDb.OleDbCommand(sql, conn)
da.Fill(ds, strTable)

If ds.Tables.Count > 0 _
Then

Dim dgvRow As New DataGridViewRow
Dim dgvCell As DataGridViewCell

Dim dr As DataRow
For Each dr In ds.Tables(0).Rows

dgvCell = New DataGridViewTextBoxCell()
dgvCell.Value = i
dgvRow.Cells.Add(dgvCell)
i = i + 1

dgvCell = New DataGridViewImageCell
dgvRow.Cells.Add(dgvCell)

dgvCell = New DataGridViewComboBoxCell
dgvCell.Value = dr.Item("naam")
dgvRow.Cells.Add(dgvCell)

dgvCell = New DataGridViewComboBoxCell
dgvCell.Value = dr.Item("functie")
dgvRow.Cells.Add(dgvCell)

dgvCell = New DataGridViewTextBoxCell()
dgvCell.Value = dr.Item("datum")
dgvRow.Cells.Add(dgvCell)

dgvCell = New DataGridViewCheckBoxCell()
dgvCell.Value = True
dgvRow.Cells.Add(dgvCell)
Next

DataGridView1.Rows.Add(dgvRow)
End If
Catch oException As OleDbException
MessageBox.Show(oException.Message)

Catch oException As Exception
MessageBox.Show(oException.Message)

End Try
conn.Close()

End Sub

Regards
Marco
 
P

PvdG42

Co said:
Hi All,

I'm trying to fill a datagridview from a database but I can't get it
to work.
This is my code:

Private Sub CheckForExistingRoute()

Dim sql As String = "SELECT * FROM Route WHERE bestand=" & CInt
(iD)
Dim strTable As String = "Route"
Dim da As New OleDb.OleDbDataAdapter(sql, conn)
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim i As Integer = 1
Dim ds As New DataSet
conn.Open()

cb.QuotePrefix = "["
cb.QuoteSuffix = "]"

Try
da.SelectCommand = New OleDb.OleDbCommand(sql, conn)
da.Fill(ds, strTable)

If ds.Tables.Count > 0 _
Then

Dim dgvRow As New DataGridViewRow
Dim dgvCell As DataGridViewCell

Dim dr As DataRow
For Each dr In ds.Tables(0).Rows

dgvCell = New DataGridViewTextBoxCell()
dgvCell.Value = i
dgvRow.Cells.Add(dgvCell)
i = i + 1

dgvCell = New DataGridViewImageCell
dgvRow.Cells.Add(dgvCell)

dgvCell = New DataGridViewComboBoxCell
dgvCell.Value = dr.Item("naam")
dgvRow.Cells.Add(dgvCell)

dgvCell = New DataGridViewComboBoxCell
dgvCell.Value = dr.Item("functie")
dgvRow.Cells.Add(dgvCell)

dgvCell = New DataGridViewTextBoxCell()
dgvCell.Value = dr.Item("datum")
dgvRow.Cells.Add(dgvCell)

dgvCell = New DataGridViewCheckBoxCell()
dgvCell.Value = True
dgvRow.Cells.Add(dgvCell)
Next

DataGridView1.Rows.Add(dgvRow)
End If
Catch oException As OleDbException
MessageBox.Show(oException.Message)

Catch oException As Exception
MessageBox.Show(oException.Message)

End Try
conn.Close()

End Sub

Regards
Marco

Please provide the exact error message you receive, or provide a short, but
complete program that illustrates the problem.
 
P

PvdG42

Co said:
Hi All,

I'm trying to fill a datagridview from a database but I can't get it
to work.
This is my code:

Private Sub CheckForExistingRoute()

Dim sql As String = "SELECT * FROM Route WHERE bestand=" & CInt
(iD)
Dim strTable As String = "Route"
Dim da As New OleDb.OleDbDataAdapter(sql, conn)
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim i As Integer = 1
Dim ds As New DataSet
conn.Open()

cb.QuotePrefix = "["
cb.QuoteSuffix = "]"

Try
da.SelectCommand = New OleDb.OleDbCommand(sql, conn)
da.Fill(ds, strTable)

If ds.Tables.Count > 0 _
Then

Dim dgvRow As New DataGridViewRow
Dim dgvCell As DataGridViewCell

Dim dr As DataRow
For Each dr In ds.Tables(0).Rows

dgvCell = New DataGridViewTextBoxCell()
dgvCell.Value = i
dgvRow.Cells.Add(dgvCell)
i = i + 1

dgvCell = New DataGridViewImageCell
dgvRow.Cells.Add(dgvCell)

dgvCell = New DataGridViewComboBoxCell
dgvCell.Value = dr.Item("naam")
dgvRow.Cells.Add(dgvCell)

dgvCell = New DataGridViewComboBoxCell
dgvCell.Value = dr.Item("functie")
dgvRow.Cells.Add(dgvCell)

dgvCell = New DataGridViewTextBoxCell()
dgvCell.Value = dr.Item("datum")
dgvRow.Cells.Add(dgvCell)

dgvCell = New DataGridViewCheckBoxCell()
dgvCell.Value = True
dgvRow.Cells.Add(dgvCell)
Next

DataGridView1.Rows.Add(dgvRow)
End If
Catch oException As OleDbException
MessageBox.Show(oException.Message)

Catch oException As Exception
MessageBox.Show(oException.Message)

End Try
conn.Close()

End Sub

Regards
Marco

Please provide the exact error message you receive, or provide a short, but
complete program that illustrates the problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top