C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ADO.NET »

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


Posted Date: 25 Aug 2009    Resource Type: Code Snippets    Category: ADO.NET
Author: Syed Shakeer Hussain Member Level: Diamond    
Rating: 1 out of 5Points: 10




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.
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();
}
}


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();

}


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.

}
}


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
{

}

}

}



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
First-Next-Previous-Last records using Windows Application?  .  First-Next-Previous-Last records in a Textboxes using Windows Application  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Export GridviewData to Excelsheet using ASP.Net
Previous Resource: Data Access Objcet In Dotnet
Return to Discussion Resource Index
Post New Resource
Category: ADO.NET


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use