Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is my code:
VB.NET
Private Sub dgv_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles dgv.CellValueChanged

        dgv(e.ColumnIndex, e.RowIndex).Value = dgv(e.ColumnIndex, e.RowIndex).Value.ToString().ToUpper()

    End Sub

In code dgv means the datagridview.
There are 2 problems.
1 - I don't get any error in IntelliSense and this sub should work when value of a cell changed; but I get error at first of running.
2 - I've checked e.ColumnIndex and e.RowIndex with messagebox. Value of e.ColumnIndex starts from 0 and goes on with no problem; but value of e.RowIndex always equals -1.

The title of my question is the text of error.
Why? Where is my mistake?
Please help me

What I have tried:

I've tried to do that, with 2 nested for loop, but nothing happens.
I tested that code with CellLeave. In this way, i get first problem when i select any cell, title of error for second problem was changed as following:
Object reference not set to an instance of an object.
Posted
Updated 25-May-16 4:18am
v2
Comments
Kenneth Haugland 25-May-16 9:25am    
Is this WPF or WinForm?
Member 12127618 25-May-16 9:30am    
WinForm

-1 is the index of the header row and CellValueChanged events with RowIndex = -1 are raised when a Column's HeaderText property changes. You probably have no interest in these events and can ignore them by testing for RowIndex = -1 in the handler.

For example:
VB.NET
Private Sub dgv_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles dgv.CellValueChanged
  If (e.RowIndex <> -1) Then 
    dgv(e.ColumnIndex, e.RowIndex).Value = dgv(e.ColumnIndex, e.RowIndex).Value.ToString().ToUpper()
  End If
End Sub


It may be that the events occur during column configuration and so deferring attachment of the CellValueChanged event handler until after the columns have been fully configured may be a solution.

Alan.
 
Share this answer
 
v2
Comments
Member 12127618 25-May-16 11:25am    
no, I don't want to handle a single event for RowIndex = -1.
I didn't understand what is your suggestion for me.
But if it is possible for you, instead of that, explain please, how to do following:
I want user put letter with all upper case. if user didn't, application do it for that cell.
Alan N 25-May-16 14:20pm    
"I don't want to handle an event for RowIndex = -1"
You have no choice in the matter as the CellValueChanged event is raised for a change in ANY cell, including the header cells. The code in the handler must be written to exclude the header cells. I've added an example.
Member 12127618 26-May-16 0:29am    
Thank you.
But I have another question. It's just a question for my knowledge.
With your example I can run with no problem, It changes user's write to uppercase, but I do not understand why it selects a whole row and at the first of running all of rows are selected.
Can you explain me please why?
The solution is kind of easy. The RowIndex is -1 because there is no selected row. So, just check for that and if it's -1 return, don't do anything.

Next, you have to make sure that the index you're getting is actually a valid index for the range. So, check for it before you try to use an index that isn't in the range of valid values.
 
Share this answer
 
Comments
Member 12127618 25-May-16 11:19am    
I have 21 rows. The code should wait until my selection; but i get problem at the first of running.
Dave Kreskowiak 25-May-16 11:35am    
Yeah, and? I already told you how to defend against it.
Member 12127618 25-May-16 13:09pm    
You said I have to make sure that the index is valid. I said I tried that and it returns -1.
So what else?
I didn't understand how to solve the problem.
Please Explain more
Thank you
Dave Kreskowiak 25-May-16 13:42pm    
If it's not valid, you just return out of the method! You don't do anything with it.

Your code can only work with real index values. Anything outside of that range isn't valid and the code shouldn't do anything at all in that case.
Member 12127618 26-May-16 0:08am    
Do you mean my problem does not have any solution?
Please look at this video:
https://youtu.be/bejVqkvsZPc?t=1525
I write his code in vb as you see in my code. For him it works.
All other part of code before this part was OK and exactly the same.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900