Adsence750x90

Friday, May 9, 2008

ASP.NET And C# GridView Control

Simple code for C# ASP.NET GridView Control.



using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class Default3 : System.Web.UI.Page
{
DataTable GridViewTable;
protected void Page_Load(object sender, EventArgs e)
{

SqlConnection con=new SqlConnection(@"Data Source=RAJ\SQLEXPRESS;Initial Catalog=Raju;Integrated Security=True");
SqlCommand cmd=new SqlCommand("SELECT username,firstname,lastname,age FROM Examples",con);
GridViewTable =new DataTable();
con.Open();
SqlDataAdapter dataAdptr=new SqlDataAdapter(cmd);
dataAdptr.Fill(GridViewTable);
con.Close();
if (GridViewTable.Rows.Count > 0)
{
GridViewTable.Columns[0].ColumnName = "User Name";
GridViewTable.Columns[1].ColumnName = "First Name";
GridViewTable.Columns[2].ColumnName = "Last Name";
GridViewTable.Columns[3].ColumnName = "Age";

GridView1.DataSource = GridViewTable.DefaultView;
GridView1.DataBind();
}
}
}

No comments: