Question bout Datagrids

LostProgrammer

Centurion
Joined
Jan 17, 2003
Messages
123
I am trying to setup a datagrid but I keep getting this error:

An unhandled exception of type 'System.NullReferenceException' occurred in system.windows.forms.dll

Additional information: Object reference not set to an instance of an object.

Here is my code:
The error here : dGridOneDay.TableStyles.Add(dGridTableStyle)

Dim adaptOneDay As New SqlClient.SqlDataAdapter("Select logtime, username from timelog", conSql)
Dim dsOneDay As DataSet
Dim dGridTableStyle As DataGridTableStyle
Dim dGridColumnTime As DataGridTextBoxColumn
Dim dGridColumnUser As DataGridTextBoxColumn

dGridOneDay.DataMember = "TimeLog"
dGridOneDay.DataSource = dsOneDay
dGridOneDay.HeaderForeColor = System.Drawing.SystemColors.ControlText
dGridOneDay.Location = New System.Drawing.Point(8, 152)
dGridOneDay.Name = "dGrid"
dGridOneDay.Size = New System.Drawing.Size(272, 208)
dGridOneDay.TabIndex = 2
dGridOneDay.TableStyles.Add(dGridTableStyle)
'dGridOneDay.TableStyles.AddRange(New System.Windows.Forms.DataGridTableStyle() {dGridTableStyle})
dGridOneDay.Visible = False

dGridTableStyle.DataGrid = dGridOneDay
dGridTableStyle.GridColumnStyles.Add(dGridColumnTime)
dGridTableStyle.GridColumnStyles.Add(dGridColumnUser)

' dGridTableStyle.GridColumnStyles.AddRange(New System.Windows.Forms.DataGridColumnStyle() {dGridColumnTime, dGridColumnUser})
dGridTableStyle.HeaderForeColor = System.Drawing.SystemColors.ControlText
dGridTableStyle.MappingName = "TimeLog"


dGridColumnTime.Format = "MM/dd/yyyy hh:mm tt"
dGridColumnTime.FormatInfo = Nothing
dGridColumnTime.HeaderText = "Log Time"
dGridColumnTime.MappingName = "logTime"
dGridColumnTime.Width = 150

dGridColumnUser.Format = ""
dGridColumnUser.FormatInfo = Nothing
dGridColumnUser.HeaderText = "Username"
dGridColumnUser.MappingName = "username"
dGridColumnUser.Width = 75

adaptOneDay.Fill(dsOneDay, "Timelog")


For the life of me I do not know why....

Thanks

Lp
 
Unless you're setting the data classes to something else later
in the code, when you declare the variable you need to include
the New keyword to initialize a new instance of the class.
Otherwise, they will be equal to Nothing (null), and you can't do
anything with them. That's the cause of the error... you're passing
Nothing to the Add method.

I'm not familiar with the constructors of these classes, so you may
need to pass some parameters to the constructors.

Visual Basic:
Dim dsOneDay As New DataSet()
Dim dGridTableStyle As New DataGridTableStyle()
Dim dGridColumnTime As New DataGridTextBoxColumn()
Dim dGridColumnUser As New DataGridTextBoxColumn()

[edit]Added a little more info[/edit]
 
Back
Top