Data grid Serial Number

How to add Serial Number in the data grid dynamically, Windows application, using C#

Questions by sekaranpg   answers by sekaranpg

Showing Answers 1 - 4 of 4 Answers

msg2mano

  • Nov 28th, 2007
 

//try this coding ---this may help u i thing
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.SqlClient;
using System.Configuration;

namespace CSharp
{
    public partial class Form1 : Form
    {
        SqlConnection con;
        SqlCommand com;
        SqlDataAdapter da;
        DataTable dt = new DataTable();
        //DataSet ds = new DataSet();
        int i = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DgBind();//call datagrid bind function
        }
        public void DgBind()//datagrid bind function
        {
            try
            {
                string constr = (string)ConfigurationSettings.AppSettings["constr"];
                con = new SqlConnection(constr);               
                com = new SqlCommand("select * from student", con);
                da = new SqlDataAdapter(com);
                da.Fill(dt);
                dataGrid1.DataSource = dt;               
                addcol();//call addcolumn(srno) function
             
            }
            catch (Exception ex)
            {
               
            }
        }

        public void addcol()//addcolumn(srno) function
        {
          
            DataColumn col1 = new DataColumn();
            col1.ColumnName = "srno";
            dt.Columns.Add(col1);
           foreach (DataRow row in dt.Rows)
           {
               foreach (DataColumn col in dt.Columns)
               {
                   if (col.ToString() == "srno")
                   {
                       row.BeginEdit();
                       i+=1;
                       row["srno"] =i ;                      
                       row.EndEdit();
                   }
               }
           }
            dataGrid1.DataSource = dt;
        }
    }
}

nt57323

  • Mar 25th, 2009
 

I am providing the solution in ASP. Just convert this as you like.
I have tried
this one at my end and its working fine.
Just take one Template field as
Serialno. in Datagrid.
Below is the code in HTML:

< size=2>asp: size=2>TemplateField HeaderText color=#0000ff size=2>="Sl.no" size=2>HeaderStyle-ForeColor size=2>="White">
<
size=2>ItemTemplate>
<
size=2>asp: size=2>Label ID size=2>="lblslno" size=2>runat="server" color=#ff0000 size=2>Text=' size=2><%# Slcount() %> size=2>'></asp size=2>:Label size=2>>
</
size=2>ItemTemplate>
</
asp color=#0000ff size=2>: size=2>TemplateField>


Slcount() is the function which i have taken in the code page.
Take one variable Count as integer.
In the above function take Count= Count + 1. Initially count will be taken as
Zero.
Below is the code in the code page:


Dim
count
as
integer
= 0
Public Function
Slcount()
as
Integer
    count
=
count
+
1
   
Return
count
End Function


Enjoy Coding.


  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions