472,363 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,363 software developers and data experts.

NullReferenceException on DataGridView.Columns Index property

I am trying to set a tooltip for a column cell in a data grid as
documented in the MS Visual Studio 2005 documentation. I set up a test
to match the example exactly including database column, data grid
column and data values. I also have a tooltip control on the form. If
I comment out the 'if' test, it works fine, but uncommented, I get a
NullReferenceException error on the
Me.dataGridView1.Columns("Rating").Index reference. Here is the code
snippet from the help doc:

' Sets the ToolTip text for cells in the Rating column.
Sub dataGridView1_CellFormatting(ByVal sender As Object, _
ByVal e As DataGridViewCellFormattingEventArgs) _
Handles dataGridView1.CellFormatting

If e.ColumnIndex = Me.dataGridView1.Columns("Rating").Index _
AndAlso Not (e.Value Is Nothing) Then

With Me.dataGridView1.Rows(e.RowIndex).Cells(e.ColumnIn dex)

If e.Value.Equals("*") Then
.ToolTipText = "very bad"
ElseIf e.Value.Equals("**") Then
.ToolTipText = "bad"
ElseIf e.Value.Equals("***") Then
.ToolTipText = "good"
ElseIf e.Value.Equals("****") Then
.ToolTipText = "very good"
End If

End With

End If

End Sub 'dataGridView1_CellFormatting
I'm new to vb.net, so be kind :-)

Thanks for the help.

Sep 13 '06 #1
5 4776
Well, lets check the simple cause first. Does the column named "Rating"
exist when this code executes?

Thanks,

Seth Rowe

Kathy wrote:
I am trying to set a tooltip for a column cell in a data grid as
documented in the MS Visual Studio 2005 documentation. I set up a test
to match the example exactly including database column, data grid
column and data values. I also have a tooltip control on the form. If
I comment out the 'if' test, it works fine, but uncommented, I get a
NullReferenceException error on the
Me.dataGridView1.Columns("Rating").Index reference. Here is the code
snippet from the help doc:

' Sets the ToolTip text for cells in the Rating column.
Sub dataGridView1_CellFormatting(ByVal sender As Object, _
ByVal e As DataGridViewCellFormattingEventArgs) _
Handles dataGridView1.CellFormatting

If e.ColumnIndex = Me.dataGridView1.Columns("Rating").Index _
AndAlso Not (e.Value Is Nothing) Then

With Me.dataGridView1.Rows(e.RowIndex).Cells(e.ColumnIn dex)

If e.Value.Equals("*") Then
.ToolTipText = "very bad"
ElseIf e.Value.Equals("**") Then
.ToolTipText = "bad"
ElseIf e.Value.Equals("***") Then
.ToolTipText = "good"
ElseIf e.Value.Equals("****") Then
.ToolTipText = "very good"
End If

End With

End If

End Sub 'dataGridView1_CellFormatting
I'm new to vb.net, so be kind :-)

Thanks for the help.
Sep 14 '06 #2

rowe_newsgroups wrote:
Well, lets check the simple cause first. Does the column named "Rating"
exist when this code executes?

Thanks,

Seth Rowe
Yes it does. It is visible in the column properties within the
datagridview.

Sep 14 '06 #3
Are you adding the Rating Column with code or by using the "Add Column"
dialog in design view? Also what are you putting in for the Name
property and the Header Text property of the column? I used the
following in my form_load event to add the "Rating" column and
everything works fine.

DataGridView1.Columns.Add("Rating", "Rating")
Dim values() As String = {"*", "**", "***", "****"}
For i As Integer = 0 To 4
DataGridView1.Rows.Add(values(i))
Next i

Let me know what you find out.

Thanks,

Seth Rowe
Kathy wrote:
rowe_newsgroups wrote:
Well, lets check the simple cause first. Does the column named "Rating"
exist when this code executes?

Thanks,

Seth Rowe

Yes it does. It is visible in the column properties within the
datagridview.
Sep 14 '06 #4

rowe_newsgroups wrote:
Are you adding the Rating Column with code or by using the "Add Column"
dialog in design view? Also what are you putting in for the Name
property and the Header Text property of the column? I used the
following in my form_load event to add the "Rating" column and
everything works fine.

DataGridView1.Columns.Add("Rating", "Rating")
Dim values() As String = {"*", "**", "***", "****"}
For i As Integer = 0 To 4
DataGridView1.Rows.Add(values(i))
Next i

Let me know what you find out.

Thanks,

Seth Rowe

I did not add the column in code. If I go into Edit Columns now,
Rating is one of the columns in the list. AND its properties shows
Rating as the DataPropertyName as well as the HeaderText.

Thanks,
Kathy

Sep 14 '06 #5
I got it working by changing the following:

If e.ColumnIndex = Me.dataGridView1.Columns("Rating").Index _

to:

Dim col as New DataGridViewColumn

col = Me.dataGridView1.Columns(e.ColumnIndex)

if col.DataPropertyName = "Rating" _
I guess Microsoft needs to correct their documentation ;)

Kathy

Sep 14 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: statlerw | last post by:
I have successfully implemented drag and drop in my application to allow the reordering of columns by dragging and dropping them in the same datagridvire (Net 2.0). If i take it relatively...
7
by: Mitchell S. Honnert | last post by:
Is there an equivalent of the DataGrid's DataGridTableStyle for the DataGridView? If not, is there an easy way to duplicate the DataGridTableStyle's functionality for the DataGridView? Here's...
2
by: Karen Hill | last post by:
I have a datagridview control on a form with a few of the columns being DataGridViewCheckBox. Whenever I click on the checkbox to create a new record I get a NullReference Exception thrown even...
0
by: schoultzy | last post by:
Hello Everyone, I have been trying to figure this one out for two days now. I have created a DataGridView which is populated by an ObjectDataSource. My problem occurs when I attempt to use the...
3
by: connected | last post by:
I'm having difficulty with populating a DataGridView control with data correctly. It works with a single class, for example... class MyClass { private string _propertyOne; private string...
3
by: Johnny E. Jensen | last post by:
Hello Dot sure if this it the right group but here goes. I'am using the DataGridView multiple times in my application, and then i'll read a book on inherience, and that opend a new world for...
8
by: Brian Pelton | last post by:
This is on .Net 2.0 in a WinForms application. I have a DataGridView that is bound to a BindingSource. The DataGridView has 3 columns. The first two are "normal" text columns and the last is a...
7
by: =?Utf-8?B?TG9zdEluTUQ=?= | last post by:
Hi All :) I'm converting VB6 using True DBGrid Pro 8.0 to VB2005 using DataGridView. True DBGrid has a MultipleLines property that controls whether individual records span multiple lines. Is...
6
by: Miro | last post by:
Sorry for the cross post. I am stuck. I have a datagridview for poker rounds. Basically there are 3 columns in this datagridview. "Round" "SmallBlind" "BigBlind" I have an issue when I tab...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.