473,383 Members | 1,880 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 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 4848
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.