Click here to Skip to main content
15,312,535 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 C#.net windows application

descreption:

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 begining.so please send solution to my emailid [email removed]
Posted
Updated 3-Apr-12 5:31am
v2

1 solution

Hello

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


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

C#
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;
}
   
v3
Comments
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;
i++;
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();

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

}
panduRajju 4-Apr-12 9:43am
   
and for previous button


objCourtDetails.Action = "select";
ds = objCourtDetails.CourtDetailsSelect_Bal(objCourtDetails);
////dgvCourtView.DataSource = ds.Tables[0];

//int i = Convert.ToInt32(txtCourtID.Text);
//if (i > -1 || i != 0)
if (i == ds.Tables[0].Rows.Count - 1 || i != 0)
{
i--;
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();
}

else
{
//no records to see more.
}
}
Shahin Khorshidnia 4-Apr-12 13:38pm
   
Ok, 1. If you have not a List of your entity create one (myObjectCollection) 2. Pass the SelectedItem and the collection of data (in my example: myObjectCollection) to the other form. You must pass them, in CellDoubleClick event. 3. Use the methods that I wrote in updated solution. I tested it and works. you know that "MyType" is an example and you must change it to your defined type.
Shahin Khorshidnia 5-Apr-12 17:06pm
   
And?

Did it help?

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