Go to DotNetFunda.com
 Online : 1937 |  Welcome, Guest!   Login
 
Home > Articles > Windows Forms > How to display records as First-Next-Previous-Last in a Textboxes using Windows Application?

  • Download the OOPS, ASP.NET and ADO.NET Training Videos for FREE, click here.

Submit Article | Articles Home | Search Articles |

How to display records as First-Next-Previous-Last in a Textboxes using Windows Application?

 Posted on: 8/25/2009 6:01:05 PM by Syedshakeer | Views: 1209 | Category: Windows Forms | Level: Intermediate | Print Article
In this article you will know how to display records in a textboxes as First,Next,Previous and Last records

.NET Training Videos!
Buy online comprehensive training video pack just for $35.00 only, see what's inside it.

For doing this we have to add textboxes and 4 buttons on a Windows form.see below design of windows form containing buttons and textboxes.


Here i am using DataGridvew to show you records present in atable.Bind a Data to the DataGridview.Here i am using MsAccess DataProvider as Odbc client.You can use your own DataProvider in the same way.

Explanation:When the users clicks on 'First Button' ,the first record of a table have to display in a corresponding column textboxe.

        Coding for 'First Button':- Double click on 'First Button' and write the below code.

private void btnfirst_Click(object sender, EventArgs e)

{

if (ds.Tables[0].Rows.Count > 0)

{

i = 0;

textBox1.Text = ds.Tables[0].Rows[i]["ID"].ToString();

textBox2.Text = ds.Tables[0].Rows[i]["empname"].ToString();

textBox3.Text = ds.Tables[0].Rows[i ]["salary"].ToString();

}

}


First Button Image

        Coding for 'Last Button':- Double click on'Last Button' and write the below code.

private void btnlast_Click(object sender, EventArgs e)

{

i = ds.Tables[0].Rows.Count - 1;

textBox1.Text = ds.Tables[0].Rows[i ]["ID"].ToString();

textBox2.Text = ds.Tables[0].Rows[i]["empname"].ToString();

textBox3.Text = ds.Tables[0].Rows[i]["salary"].ToString();

 

}

Last Button Iamge

        Coding for 'Next Button':- Double click on'Next Button' and write the below code.

private void btnnext_Click(object sender, EventArgs e)

{

 

if (i < ds.Tables[0].Rows.Count-1)

{

i++;

textBox1.Text = ds.Tables[0].Rows[i]["ID"].ToString();

textBox2.Text = ds.Tables[0].Rows[i]["empname"].ToString();

textBox3.Text = ds.Tables[0].Rows[i]["salary"].ToString();

}

else

{

//no records to see more.

}

}

Next Button Image

       Coding for 'Previous Button':- Double click on'Previous Button' and write the below code.

private void btnprevious_Click(object sender, EventArgs e)

{

if (i == ds.Tables[0].Rows.Count - 1 || i !=0)

{

i--;

textBox1.Text = ds.Tables[0].Rows[i]["ID"].ToString();

textBox2.Text = ds.Tables[0].Rows[i]["empname"].ToString();

textBox3.Text = ds.Tables[0].Rows[i]["salary"].ToString();

}

else

{

//No records to see more

}

}

Above ds.Tables[0].Rows.Count means it counts number of records presnt in a table.

       The Complete coding in Form1.cs as follows:-

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Data.Odbc;

using System.IO;

namespace WindowsApplication1

{

public partial class Form1 : Form

{

   public Form1()

{

   InitializeComponent();

}

OdbcDataAdapter da;

DataSet ds;

int i = 0;

int j;

OdbcConnection conn;

int last;

private void Form1_Load(object sender, EventArgs e)

{

conn = new OdbcConnection("dsn=t1");

conn.Open();

da = new OdbcDataAdapter("select * from emp", conn);

OdbcCommandBuilder builder = new OdbcCommandBuilder(da);

ds = new DataSet();

da.Fill(ds, "emp");

dataGridView1.DataSource = ds.Tables["emp"];

}

 

private void btnfirst_Click(object sender, EventArgs e)

{

if (ds.Tables[0].Rows.Count > 0)

{

i = 0;

textBox1.Text = ds.Tables[0].Rows[i]["ID"].ToString();

textBox2.Text = ds.Tables[0].Rows[i]["empname"].ToString();

textBox3.Text = ds.Tables[0].Rows[i ]["salary"].ToString();

}

}

private void btnlast_Click(object sender, EventArgs e)

{

i = ds.Tables[0].Rows.Count - 1;

textBox1.Text = ds.Tables[0].Rows[i ]["ID"].ToString();

textBox2.Text = ds.Tables[0].Rows[i]["empname"].ToString();

textBox3.Text = ds.Tables[0].Rows[i]["salary"].ToString();

 

}

private void btnnext_Click(object sender, EventArgs e)

{

 

if (i < ds.Tables[0].Rows.Count-1)

{

i++;

textBox1.Text = ds.Tables[0].Rows[i]["ID"].ToString();

textBox2.Text = ds.Tables[0].Rows[i]["empname"].ToString();

textBox3.Text = ds.Tables[0].Rows[i]["salary"].ToString();

}

   else

{

}

}

private void btnprevious_Click(object sender, EventArgs e)

{

   if (i == ds.Tables[0].Rows.Count - 1 || i !=0)

{

   i--;

   textBox1.Text = ds.Tables[0].Rows[i]["ID"].ToString();

   textBox2.Text = ds.Tables[0].Rows[i]["empname"].ToString();

   textBox3.Text = ds.Tables[0].Rows[i]["salary"].ToString();

   }

      else

{

}

}

 }

Thanks for reading my article!

  Syed Shakeer Hussain

 


If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Interesting?   Share and Bookmark this kick it on DotNetKicks.com


Experience:1 year(s)
Home page:http://www.dotnetfunda.com
Member since:Thursday, February 05, 2009
Level:Bronze
Status: [Member]
Biography:Hi to All ..
I am Working as Software Developer on .NET and MVM of www.dotnetspider.com
 Latest post(s) from Syedshakeer

   ◘ How to get the value of a Hidding column in a Gridview using C# posted on 9/8/2009 6:05:36 PM
   ◘ How to calculate total at the BackEnd using Trigger? posted on 8/25/2009 6:14:14 PM
   ◘ How to display records as First-Next-Previous-Last in a Textboxes using Windows Application? posted on 8/25/2009 6:01:05 PM
   ◘ How to Start Mobile Application on Windows? posted on 8/18/2009 1:00:01 AM
   ◘ Paging for First, Next, Previous and Last in gridview posted on 8/16/2009 12:47:43 PM


Submit Article

About Us | The Team | Advertise | Contact Us | Feedback | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found copied contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
All rights reserved to DotNetFunda.Com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
(Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher)