简体   繁体   中英

How to Check Selected Row In Datagridview without Checking the Cell in C#

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 在此处输入图片说明

it Shows This Error

在此处输入图片说明

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM