Blank DataGrid

S

Skandy

Hello:

This must be quite simple! But I couldnt figure out whats wrong:

I have this following code, and I still see a blank datagrid:

Imports System.Data
Imports System.Data.SqlClient

Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim conn As New SqlConnection("****")
Dim da As New SqlDataAdapter("Select * from tblNote where CDID
= 55", conn)
Dim ds As New DataSet
Dim dt As New DataTable

da.Fill(ds)
If (ds.Tables(0).Rows.Count > 100) Then
dt = Get100Rows(ds.Tables(0))
End If
dt.AcceptChanges()
Dim dv As New DataView(dt)
Try
DataGridView1.DataSource = dv
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

End Sub

Private Function Get100Rows(ByRef dt As DataTable) As DataTable
Dim dt1 As New DataTable
Dim i As Integer
For i = 0 To 99
dt1.ImportRow(dt.Rows(i))
Next
dt1.AcceptChanges()
Get100Rows = dt1
End Function
End Class


I can see that the DataTable dt has 1000 rows. The datagrid does get
populated if I were to have Tables(0) of the DataSet ds as the
DataSource.

TIA.

Skandy
 
J

Jim Hughes

By default, the DataGridView does not autogenerate columns.

Specify
Me.DataGridView1.AutoGenerateColumns = True

somewhere in the code before setting the DataSource
 
S

Skandy

Thanks Jim.

I got the problem resolved by adding:

dt1 = dt.Clone();

Regards,
Sk&y;
 

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