0

hi everyone i'm a new coder here , i'm coding for a datagridview and have to check a row of data in the datagridview , but the problem is the datagridview must be enabled , and the selected cell will be automaticly on the first cell and the first row , if i use this code in the cells , not row , it shows an error , can someone help me to make an anti error if someone use this code when the selected item is cells and not row

        if (dataGridView1.SelectedRows.Count < 0==true)
        {
            MessageBox.Show("Pick the data first!");
        }

When i Clicked Update Which Have The Code Above enter image description here

it Shows This Error

enter image description here

But when i Selected the Full Row and Clicked The Update Button , it Works Normally and No error

Kevin
  • 37
  • 6
  • @apomene it's definitly not a good approach, but still it's not a C# syntax error. OP - what error do you get and where? – Kamil T May 29 '14 at 14:00
  • In any case `dataGridView1.SelectedRows.Count < 0 == true` will be `true`? – Soner Gönül May 29 '14 at 14:02
  • the error is the value of data's is not found "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" – Kevin May 29 '14 at 14:02
  • 1
    @user2187805 On which line exactly? I don't think you show us relevant code. – Soner Gönül May 29 '14 at 14:03
  • You should look into [this question](http://stackoverflow.com/questions/3578144/index-of-currently-selected-row-in-datagridview) – j0kly May 29 '14 at 14:04
  • Please provide a. the full postback event code. b. the error message you are receiving. – Justin Russo May 29 '14 at 14:05

2 Answers2

0

I'm not 100% sure what it is you are asking. Are you trying to make the user select full rows in your datagridview instead of individual cells? If that is the case, you'll want to set the SelectionMode property to "FullRowSelect" for your datagridview.

0

So my guess is that SelectionMode of your dataGridView1 is not set to FullRowSelect or RowHeaderSelect - thus you have no selected rows and dataGridView1.SelectedRows collection is always empty - that is why dataGridView1.SelectedRows[0] throws ArgumentOutOfRangeException.

Changing your dataGridView1.SelectionMode to one of the above according to desired logic should solve your problem.

j0kly
  • 33
  • 5