| |
GeekInterview.com > Interview Questions > Microsoft > DataGrid
| Print | |
Question: Data grid Serial Number
Answer: How to add Serial Number in the data grid dynamically, Windows application, using C# |
| November 11, 2007 07:41:30 |
#1 |
| msg2mano |
Member Since: November 2007 Total Comments: 2 |
RE: Data grid Serial Number |
//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; } } } |
| |
Back To Question | |