Click here to Skip to main content
16,004,479 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have got a datagridview in my windows forms app, which is filled up with data, I have (nextButton) that is clicked to go through each and every row in the Grid. When ever I load my form its seems that whenever I click the next button the selection starts from the second row and not the first row.. why is this? is there anything I can do?

My code for datagridview
C#
SqlConnection conn = new SqlConnection(connection);
              SqlCommand db = new SqlCommand("select * from TblEmp where Emp_Title = 'Mr'", conn);
              SqlCommandBuilder builder = new SqlCommandBuilder(dataAdapter);
              dataAdapter = new SqlDataAdapter(db);
              dataset = new DataSet();
              dataAdapter.Fill(dataset, "TblEmp");
              dataGridView1.DataSource = dataset.Tables["TblEmp"];


Mycode for nextButton

C#
void NextRecord()
       {
           if ( RecordCounter < dataset.Tables[0].Rows.Count -1)
           {
               RecordCounter++;
               TxtDisplayQuestion.Text = dataset.Tables[0].Rows[ RecordCounter]["Emp_Title"].ToString();


           }

Thank you
Posted
Comments
Sergey Alexandrovich Kryukov 24-Mar-14 13:23pm    
Why? Where did you select first line? Who told you that you nextButton should select first line?
—SA

use your "RecordCounter++;" after the "TxtDisplayQuestion.Text = ..." code line.
 
Share this answer
 
Don't know about your schema, so give it a try..
C#
void NextRecord()
{
    if ( RecordCounter < dataset.Tables[0].Rows.Count -1)
    {
        TxtDisplayQuestion.Text = dataset.Tables[0].Rows[ RecordCounter]["Emp_Title"].ToString();
    }
    RecordCounter++;
}


-KR
 
Share this answer
 
C#
//as first
void NextRecord()
       {
           if ( RecordCounter < dataset.Tables[0].Rows.Count -1)
           {
               RecordCounter++;//before RecordCounter= 0, after RecordCounter=1
               TxtDisplayQuestion.Text = dataset.Tables[0].Rows[ RecordCounter]["Emp_Title"].ToString(); //<= here: RecordCounter = 1 
 

           }


You can set RecordCounter = -1 as First or move code RecordCounter++; after set TxtDisplayQuestion.Text=...
C#
void NextRecord()
       {
           if ( RecordCounter < dataset.Tables[0].Rows.Count -1)
           {
               
               TxtDisplayQuestion.Text = dataset.Tables[0].Rows[ RecordCounter]["Emp_Title"].ToString();
 RecordCounter++;

           }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900