.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!



Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

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

Posted By:Syed Shakeer Hussain       Posted Date: August 25, 2009    Points: 25    Category: C#    URL: http://www.dotnetspark.com  
 

In this article you will know how to display records in a textboxes as First,Next,Previous and Last records.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


 Subscribe to Articles

     

Further Readings:

Responses
Author: Amit Mehra         Company URL: http://www.dotnetspark.com
Posted Date: August 27, 2009

Good job Syed..nice and self explanatory code...
Author: Syed Adnan Amir         Company URL: http://www.dotnetspark.com
Posted Date: January 31, 2011

AOA Shah Sahib,

I have read your this artical. this is realy very good artical...i was realy in the search of this kid of artical.

I am New to C# and ASP.net with C#, Can you please give me some good links of websites or tutrial of you which can help me to understand the development of a good database web as wel as windows aplication.

I will greatful to you.

regards,

Syed Adnan Amir.

Author: nandanasai         Company URL: http://www.dotnetspark.com
Posted Date: September 18, 2013

Hai shyed,

I have seen your displaying first ,next,previous and last records in textboxes. It was very helpful. But one defect is click on next button shows only next record then it won't shows other records. Likewise click on previous button goes out of the loop condition.can you give me the suggestion to view next and previous records with source codes.

Regards
Nandanasai

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend