Click here to Skip to main content
15,908,020 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i have problem on getting next,previous records in windows application


i have binded data to datagridview and when i click datagridview middle cell the perticular
row data goes to next form as details .i want navigate data from that middle row data
by counting for next button and decrement for previous button but im getting from please send solution to my emailid [email removed]
Updated 3-Apr-12 5:31am

1 solution


Suppose, there is a List of MyObject, and you've bound MyObjectCollection to the DataGrid
List<mytype> MyObjectCollection

When you select a object then you want the Next and the Previous:

public MyType GetNextItem(object selectedObject, List<MyType> myObjectCollection)
    int c = myObjectCollection.TakeWhile(o => o == selectedObject).Count() - 1;

    int p = c - 1;
    int n = c + 1;

    return (myObjectCollection.Count() >= n + 1) ? myObjectCollection[n] : null;

public MyType GetPreviousItem(object selectedObject, List<MyType> myObjectCollection)
    int c = myObjectCollection.TakeWhile(o => o == selectedObject).Count() - 1;

    int p = c - 1;
    int n = c + 1;

    return (p >= 0) ? myObjectCollection[p] : null;
Share this answer
panduRajju 4-Apr-12 1:15am    
thanq u for your response but its not working
my requirement is i have data in datagridview when i click by celldoubleck event
the perticular row data goes to another form.from that form when i click next or previous button it navigating from begining record onwards.but i want to navigate from selected record onwards.
Shahin Khorshidnia 4-Apr-12 5:23am    
What's the navigating control in the other form? Does it have a SelectedItem or a SelectedIndex property?
panduRajju 4-Apr-12 9:01am    
in another form the data in binding to textboxes. if i click next button i want
display details of next record and previous button for previous record
panduRajju 4-Apr-12 9:37am    
no selecteditem and selectedindex
panduRajju 4-Apr-12 9:41am    
my code in another form is

private void btnNext_Click(object sender, EventArgs e)

objCourtDetails.Action = "select";
ds = objCourtDetails.CourtDetailsSelect_Bal(objCourtDetails);
//nt i = Convert.ToInt32(txtCourtID.Text);
////dgvCourtView.DataSource = ds.Tables[0];

//if( i >-1 )
if (i < ds.Tables[0].Rows.Count - 1)
//nt j = i + 1;
txtCourtID.Text = ds.Tables[0].Rows[i]["ID"].ToString();
txtcomboCourtType.Text = ds.Tables[0].Rows[i]["CourtType"].ToString();
txtCourtCode.Text = ds.Tables[0].Rows[i]["CourtCode"].ToString();
txtCourtName.Text = ds.Tables[0].Rows[i]["CourtName"].ToString();
txtCourtLocation.Text = ds.Tables[0].Rows[i]["CourtLocation"].ToString();
txtCity.Text = ds.Tables[0].Rows[i]["City"].ToString();
txtState.Text = ds.Tables[0].Rows[i]["State"].ToString();
txtCountry.Text = ds.Tables[0].Rows[i]["Country"].ToString();
txtZipCode.Text = ds.Tables[0].Rows[i]["ZipCode"].ToString();
txtURL.Text = ds.Tables[0].Rows[i]["URL"].ToString();
txtWorkPhone.Text = ds.Tables[0].Rows[i]["WorkPhone"].ToString();
txtEmailAddress.Text = ds.Tables[0].Rows[i]["EmailAddress"].ToString();
txtFAX.Text = ds.Tables[0].Rows[i]["FAX"].ToString();
txtOther.Text = ds.Tables[0].Rows[i]["Other"].ToString();
txtNote.Text = ds.Tables[0].Rows[i]["Note"].ToString();
txtComments.Text = ds.Tables[0].Rows[i]["Comments"].ToString();
//txtComments.Text = ds.Tables[0].Rows[i]["DateofEntered"].ToString();
//txtComments.Text = ds.Tables[0].Rows[i]["Comments"].ToString();

//no records to see more.
MessageBox.Show("No Records Found");


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