Cannot check DataGridView row selected or not when form load C#
<p>I want to check DataGridView row selected or not when Form load to do some stuff but it did not work for me when using this code:</p>

<pre><code> private void btnEdit_Click(object sender, EventArgs e)
        {
            if (grdInvoiceDetails2.Rows.Count &gt; 0 &amp;&amp; grdInvoiceDetails2.SelectedRows.Count &gt; 0)
            {
                try
                {
                    Them = false;
                    var r = grdInvoiceDetails2.CurrentCell.RowIndex;
                    txtQty.Text = grdInvoiceDetails2.Rows[r].Cells[3].Value.ToString();
                    cboInvNo.SelectedValue = grdInvoiceDetails2.Rows[r].Cells[1].Value.ToString();
                    cboItemName.SelectedValue = grdInvoiceDetails2.Rows[r].Cells[2].Value.ToString();
                    cboItemName.Enabled = true;
                    cboInvNo.Enabled = false;
                    txtQty.Enabled = true;

                    btnSave.Enabled = true;
                    btnCancel.Enabled = true;
                    btnAdd.Enabled = false;
                    btnEdit.Enabled = false;
                    btnDelete.Enabled = false;
                    btnBack.Enabled = false;
                    LoadDataToCollection();


                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }

        }
</code></pre>

<p>When debugging, grdInvoiceDetails2.Rows.Count=6, grdInvoiceDetails2.SelectedRows.Count=1
But If I did not click on any row, it will throw an exception .Because grdInvoiceDetails2.CurrentCell= null.</p>