第一个下一个上一个按钮的代码和页面窗口C#应用程序中的显示数据 [英] Code for first next last previous button and display data in page windows C# application

查看:68
本文介绍了第一个下一个上一个按钮的代码和页面窗口C#应用程序中的显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我创建了windows c#应用程序。其中我把按钮放在第一个,下一个,最后一个,上一个。

现在我想显示从数据库中获取记录,并在页面中显示。

当我点击第一个按钮然后在加载时间数据显示首先。当我点击最后一个然后数据显示最后一个记录,当我点击上一个然后数据显示之前,当我点击下一个然后在c#windows应用程序接下来显示数据。

请帮助我...



我尝试了什么:



i尝试创建代码,但没有成功。如何使用for循环来使用它并在页面的加载时间内显示数据。


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.

推荐答案

你不使用循环。

究竟如何做到这一点取决于许多因素,例如你如何获取数据,涉及多少数据,以及你的数据库包含的内容。

但是一种方法是:

确保您的数据库包含Id列,ID为IDENTITY。

在数据库中设置一个类级别int,名为 lastRow 并将其设置为-1。

当您从数据库中获取要显示的单行时,请使用以下内容:

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:
SELECT TOP 1 * FROM MyTable WHERE Id > lastRow ORDER BY ID ASC



如果您获得任何行,请使用ID值加载lastRow。

对于Next,它是相同的查询。

对于之前的:


If you get any rows, load lastRow with the ID value.
For "Next" it's the same query.
For previous its:

SELECT TOP 1 * FROM MyTable WHERE Id < lastRow ORDER BY ID DESC



首先:


First:

SELECT TOP 1 * FROM MyTable ORDER BY ID ASC



Lst:


Lst:

SELECT TOP 1 * FROM MyTable ORDER BY ID DESC


使用此示例建立你的表格



use this example build your Form

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;

       }
   }


这篇关于第一个下一个上一个按钮的代码和页面窗口C#应用程序中的显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆