auto generate serial number in datagridview in c#

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.




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



 After Completing the steps, write the code in Datagridview method as shown. 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:







Advertisements
SHARE

Lakshmi Tulasi Jasti

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

13 comments:

  1. private void Form1_Load(object sender, EventArgs e)
    {
    // 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();
    }
    }

    ReplyDelete
  2. Hi Hari,
    This is also the way to generate sno thanks for sharing...

    ReplyDelete
  3. 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... :-)

    ReplyDelete
  4. An unhandled exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll I am getting this error how can i handle this

    ReplyDelete
  5. Sir I am Novice in Dot Net, and i always looking for step by step instruction That I find Here Thank you So much Sir....

    ReplyDelete
  6. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.

    ReplyDelete
  7. Excellent...
    Thanks for sharing...
    But I'm facing a problem... Error "input string was not in a correct format"

    ReplyDelete
  8. Thank you for benefiting from time to focus on this kind of, I feel firmly about it and also really like comprehending far more with this particular subject matter. In case doable, when you get know-how, is it possible to thoughts modernizing your site together with far more details? It’s extremely useful to me
    Data Science training in chennai
    Data Science training in OMR
    Data Science training in chennai
    Data Science Training in Chennai
    Data Science training in Chennai
    Data science training in bangalore

    ReplyDelete
  9. Very efficiently written information. It will be beneficial to anybody who utilizes it, including me. Keep up the good work. For sure i will check out more posts. This site seems to get a good amount of visitors. auto reply text

    ReplyDelete
  10. Nice blog,I understood the topic very clearly,And want to study more like this.
    Data Scientist Course

    ReplyDelete