views:

62

answers:

4

I have a DataGridView control and I'm checking if I'm getting the correct value of column 0

alt text

But when I click my button to echo the value, I always get this error...

alt text

Are there errors in my code? Or what?

A: 

I think the most possible reason is that when you click the button, the form is focus on the 2nd row (row with *), this row has no reference to it until you click & input something in a cell of the row (if so, another row with * will be generated automatically). If you don't want the row to be generated automatically, just set dataGridView1.AllowUserToAddRows to false.

Danny Chen
I just checked it, but it seems that it gets the current row index of the gridview
yonan2236
@yonan Here I perfectly reproduced your error. Make your gridview two rows, one with value and one is marked as *. If you are selecting row 1, the error will not occur. If you are selecting row *, it will throw the exception, just like what happened in your question.
Danny Chen
ah... thanks... but it's already working now. You play DOTA right..? hehe.
yonan2236
+1  A: 

Break up your long/compound statement into several lines/statements:

  • Is dbView1 null?
  • Is dbView1.CurrentRow null?
  • Is dbView1.CurrentRow.Index equal to -1, or greater than the number of rows?
  • Is dbView1[dbView1.CurrentRow.Index] null?
ChrisW
A: 

Try replacing

dgView.CurrentRow.Index with dgView.SelectedRows[0].Index

and set the property Multiselect to False, SelectionMode = FullRowSelect

Did that help? :)

Ranhiru Cooray
ok sir i'll try
yonan2236
Also, when that exception is thrown and the debugging is paused, check each value to see which one is NULL and post it here :)
Ranhiru Cooray
still, doesn't work...
yonan2236
i tried tracing the values of the current selected row and column. It seems correct. But still I can't get the value i supplied in that particular cell.
yonan2236
Can you check which object is NULL and let us know?
Ranhiru Cooray
If the Row and Columns is fine, the only thing left to be NULL i think is `Value`. Is it NULL?
Ranhiru Cooray
this code dataGridView1[dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentRow.Index].Value returns null
yonan2236
yes it's NULL...
yonan2236
Yes yonan, but a NullReferenceException is thrown as one of the objects specified in that line is NULL. So when you hover over the objects when the exception is thrown, which object is Null?
Ranhiru Cooray
Value ..........
yonan2236
Hmm, interesting... When value is NULL, what are the values for the Column and Row? Column should be 0, but what is the value for Row?
Ranhiru Cooray
0, but if you select the second row the value of row is 1. just like as expected.
yonan2236
but i still don't know why I can't fetch the value I entered on that particular cell
yonan2236
What is the DataSource for the DataGridView? I am just asking, as according to the image you have pasted, the 2nd row is empty (Index 1). But you are 100% sure that the 2nd row of the 1st column is NOT empty, is it? Try replacing the indexes with fixed values (0,0) and check whether it is working :)
Ranhiru Cooray
ok sir... I'll do what you said..
yonan2236
I tried this: MessageBox.Show(dataGridView1[0, 0].Value.ToString());
yonan2236
still doesn't work.
yonan2236
the dataGrid has no datasource. but its column is editable so i can add values on it
yonan2236
a lot............
yonan2236
A: 

I just created a new project a and I it worked... strange.

yonan2236