Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi,
i create windows c# application. in which i put button like first,next, last,previous.
now i want to display record fetch from database , and show in page.
when i click first button then on load time data display first. when i click last then data display last record, when i click previous then data dispaly previous, and when i click next then display data next in c# windows application.
pls help me...

What I have tried:

i tried to create code, but not get succcess. how can i use for loop to use that and display data in load time of page.
Posted
Updated 7-Jul-22 15:19pm
Comments
Karthik_Mahalingam 23-Jun-16 5:07am    
are you fetching all the records at once or on each button click you are getting it from db ?
Member 11952997 23-Jun-16 5:18am    
on each button click display record in fields on forms in windows application c#.net.
means examlple- i create regestration form. many record i insert and save in database.
now i want to see first record then click on first button in form and it dispaly first inserted record.
then i want to see last record then click last button and it dispaly last record.
so that...
note- i want to see record in form fields.
Karthik_Mahalingam 23-Jun-16 5:20am    
Ok.
Fine
Are you using datatable or list
Member 11952997 23-Jun-16 5:29am    
data table.
Karthik_Mahalingam 23-Jun-16 5:30am    
ok. what is the table structure, how many columns
does it has ID column ?
what are the controls in the form ?

use this example build your Form

C#
public partial class Form1 : Form
   {

       public Form1()
       {
           InitializeComponent();
       }

       DataTable dt = new DataTable();
       int index = 0;
       int last = 0;

       private void Form1_Load(object sender, EventArgs e)
       {
           dt.Columns.Add("FirstName");
           dt.Columns.Add("LastName");

           dt.Rows.Add("Moto", "Nexus");
           dt.Rows.Add("Apple", "Iphone");
           dt.Rows.Add("Sony", "Vaio");
           dt.Rows.Add("Samsung", "Edge");

           index = 0;
           last = dt.Rows.Count -1;
           PopulateData();
       }

       private void btnFirst_Click(object sender, EventArgs e)
       {
           index = 0;
           PopulateData();
       }
       private void btnLast_Click(object sender, EventArgs e)
       {
           index = last - 1;
           PopulateData();
       }
       private void btnPrev_Click(object sender, EventArgs e)
       {
           index--;
           index = index <0? 0:index;
           PopulateData();
       }
       private void btnNext_Click(object sender, EventArgs e)
       {
           index++;
           index = index > last? last : index;
           PopulateData();
       }

       private void PopulateData()
       {
           DataRow row = dt.Rows[index];
           string firstName = row["FirstName"].ToString();
           string lastName = row["LastName"].ToString();
           txtFirstName.Text = firstName;
           txtLastName.Text = lastName;

       }
   }
 
Share this answer
 
Comments
Member 13697095 30-Mar-18 7:11am    
i have added picture as many as 26 how to display them with prev and next button
Karthik_Mahalingam 30-Mar-18 7:14am    
same concept,
instead of printing the name in the textbox, use the text to load the image to the picture box.
Quote:
The advent of the BindingNavigator control in Windows Forms enables developers to provide end users with a simple data navigation and manipulation user interface on the forms they create.

Navigate Data with BindingNavigator Control - Windows Forms .NET Framework | Microsoft Docs[^]
 
Share this answer
 
Comments
[no name] 22-Mar-24 1:26am    
how to add pervious button and show gride view
You don't use a loop.
Exactly how to do this depends on a number of factors, such as how you are fetching your data, how much data is involved, and what exactly your DB contains.
But one way would be:
Make sure your DB contains an Id column, with is IDENTITY.
Set up a class level int in your DB called lastRow and set it to -1.
When you fetch a single row from the DB to display, use something like:
SQL
SELECT TOP 1 * FROM MyTable WHERE Id > lastRow ORDER BY ID ASC

If you get any rows, load lastRow with the ID value.
For "Next" it's the same query.
For previous its:
SQL
SELECT TOP 1 * FROM MyTable WHERE Id < lastRow ORDER BY ID DESC

First:
SQL
SELECT TOP 1 * FROM MyTable ORDER BY ID ASC

Lst:
SQL
SELECT TOP 1 * FROM MyTable ORDER BY ID DESC
 
Share this answer
 
Comments
Member 11952997 23-Jun-16 5:19am    
thank u sir, i will try, if any query then i will inform u.

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