Advertisement
In this article I want to explain how to Auto generate the Serial No in datagridview.
In my previous article I explained how to display data indatagridview, for that datagridview I want to show Auto generate Serial No for this datagridview.
For auto generating the Serial No is very full to show the data with numbering.
For auto generating the Serial No is very full to show the data with numbering.
Click on Data Gridview Tasks as shown below
Click on Add Column, Enter the Name and Column name. Click on ADD.
I entered name as SNO,and header text as SNO,It will shows like below
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace DatagridviewExample { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { displayDataGridView(); } public void displayDataGridView() { SqlConnection con = new SqlConnection("Data Source = CHINNU;Initial Catalog = dotnetdb;Uid = sa;Password = password123;"); { SqlCommand cmd; cmd = new SqlCommand("select * from Employee", con); cmd.CommandType = CommandType.Text; SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); dataGridView1.DataSource = ds.Tables[0]; dataGridView1.AutoGenerateColumns = false; dataGridView1.AllowUserToAddRows = false; //serial no code starts int i = 1; foreach (DataGridViewRow row in dataGridView1.Rows) { row.Cells["SNO"].Value = i; i++; } } //serial no code Ends } } }Out put:
private void Form1_Load(object sender, EventArgs e)
ReplyDelete{
// Write here SQL Connection string
dataGridView1.Rows[0].Cells[0].Value = "1";
}
private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 5)
{
dataGridView1.Rows[e.RowIndex + 1].Cells[0].Value = (dataGridView1.Rows.Count).ToString();
}
}
Hi Hari,
ReplyDeleteThis is also the way to generate sno thanks for sharing...
what about add kind alphabet like "AK001" and first must check into database if there is no data which mean use "AK001", but if there is data which me the number add like "AK002", etc. can you show me how... i am a little bit trouble in here... appreciate u're help really... :-)
ReplyDeletehope you can help me...
DeleteAn unhandled exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll I am getting this error how can i handle this
ReplyDeleteWork fine with my code. Thanks
ReplyDeleteSir I am Novice in Dot Net, and i always looking for step by step instruction That I find Here Thank you So much Sir....
ReplyDeleteExcellent...
ReplyDeleteThanks for sharing...
But I'm facing a problem... Error "input string was not in a correct format"
Thanks bro... It's solved now...
DeleteI read this post two times, I like it so much, please try to keep posting & Let me introduce other material that may be good for our community.
ReplyDeletethanks this is good blog. 1940 classic autoglass
ReplyDeleteWindshield fix is economical contrasted with windshield substitution. Fix disposes of issues related with substitution, for example, spilling, wind whistle or windshield disappointment due to airbag organization.windshield chip repair
ReplyDelete"Regularly check your power steering fluid level." automatic transmission service gold coast
ReplyDelete