-
Jan 15th, 2015, 04:31 PM
#1
Thread Starter
Hyperactive Member
Getting error when click on DataGridView Header Column...
The following error pops up when I click on DataGridView Header Column.
{"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"}
What does it mean and how to avoid this error?
Code:
Private Sub DataGridView1_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick
'Assign values to textboxes from selected/higlighted row
'e.index is the index value of current selected row
'cell(1) for instance gets the value of the cell(row,column) of the current row selected
txtEMPID.Text = DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString
txtEMPNAME.Text = DataGridView1.Rows(e.RowIndex).Cells(1).Value.ToString
txtEMPPROFESSION.Text = DataGridView1.Rows(e.RowIndex).Cells(2).Value.ToString
End Sub
-
Jan 15th, 2015, 04:43 PM
#2
Re: Getting error when click on DataGridView Header Column...
Which line does it give you the error on? What is happening is that somewhere when you set the Text of the various TextBox controls is that you're trying to access a Row or Cell that does not exist.
We can immediately rule out that it's the Row causing the issue because you're using the DataGridViewCellMouseEventArgs to get the current row. This tells us that it's the Cell that is causing the problem. Whichever line is causing you the problems is telling you that the column at clicked row does not exist.
-
Jan 15th, 2015, 04:52 PM
#3
Thread Starter
Hyperactive Member
Re: Getting error when click on DataGridView Header Column...
This the line which throws an error:
Code:
txtEMPID.Text = DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString
-
Jan 15th, 2015, 04:58 PM
#4
Hyperactive Member
Re: Getting error when click on DataGridView Header Column...
You need to validate that the users selection is greater than 0 you carnt use that function on row headers as they have different accessible properties add an if statement in to check e.rowindex value is greater than 0 before calling the above and it won't throw an error
-
Jan 15th, 2015, 04:58 PM
#5
Re: Getting error when click on DataGridView Header Column...
Then the column 0 does not exist. What you need to do is setup a breakpoint on that line and check the following:
- What the value of e.RowIndex is and compare that to the DataGridView1.Rows.Count value
- What the value of e.Columns.Count is
-
Jan 15th, 2015, 05:09 PM
#6
Thread Starter
Hyperactive Member
Re: Getting error when click on DataGridView Header Column...
e.RowIndex is -1. But how to check DataGridView1.Rows.Count value & e.Columns.Count?
-
Jan 15th, 2015, 05:14 PM
#7
Hyperactive Member
Re: Getting error when click on DataGridView Header Column...
It would be -1 as that's the index for column headers which is why you need to validate that the e.rowindex is greater than or equal to 0 before executing that line of code
-
Jan 15th, 2015, 05:24 PM
#8
Re: Getting error when click on DataGridView Header Column...
Then you're receiving the error because there is no such thing as a row at index number -1. So this brings up my next question, which row are you wanting to set the Text to?
-
Jan 16th, 2015, 12:26 AM
#9
Thread Starter
Hyperactive Member
Re: Getting error when click on DataGridView Header Column...
Actually, I want to sort the column by clicking on it.
-
Jan 16th, 2015, 09:51 AM
#10
Re: Getting error when click on DataGridView Header Column...
Originally Posted by VS2013
Actually, I want to sort the column by clicking on it.
That is the default behavior of a DataGridView which means that there is no coding needed on your end. Why were you setting TextBox text if you wanted to sort a DataGridView?
-
Jan 17th, 2015, 02:49 AM
#11
Thread Starter
Hyperactive Member
Re: Getting error when click on DataGridView Header Column...
The code in my post#1 is to show data of a cell in related textboxes. And when I click on any of the cell in the Datagridview, it shows correctly in the textboxes. But when I click on the DGV Header column to sort it, that error pops-up.
Thanks.
-
Jan 19th, 2015, 08:47 AM
#12
Hyperactive Member
Re: Getting error when click on DataGridView Header Column...
As Previously stated, on the event validate that the selected row id is more than or equal to 0
Code:
If e.RowIndex >= 0 Then
' Do TextBox Population Stuff
End If
-
Jan 19th, 2015, 12:35 PM
#13
Re: Getting error when click on DataGridView Header Column...
I haven't used the DGV in a while, but know you can get into trouble with cascading events...in any case, checking the row index is probably the way to go, but the reason is that clicking the header cell may very well raise the CellMouseClick event.
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|