简体   繁体   中英

Datagridview not selecting last row

I am selecting records from a datagridview... I have got 5 rows in this datgridview and I am trying to select each and everyone of them by a click of a buton(NextrecordButton). This is working well but I am running into a problem the only 4 rows are selected and the last row isn't selected. is there anything am doing wrong in my code below:

private void btnNext_Click(object sender, EventArgs e)
{
   if (EmpCounter < dataset.Tables[0].Rows.Count - 1)
   {
      TxtDisplay.Text = dataset.Tables[0].Rows[EmpCounter]["Emp_Name"].ToString();
   }
}

This is working well but I am running into a problem the only 4 rows are selected and the last row isn't selected.

Because your check is checking till second last row

EmpCounter < dataset.Tables[0].Rows.Count - 1

It should be:

EmpCounter < dataset.Tables[0].Rows.Count

So your code should be:

private void btnNext_Click(object sender, EventArgs e)
{
    if (EmpCounter < dataset.Tables[0].Rows.Count)
    {
        TxtDisplay.Text = dataset.Tables[0].Rows[EmpCounter]["Emp_Name"].ToString();
    }
}

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