Adsence750x90

Saturday, May 10, 2008

C#.NET And ASP.NET GridView Control With BoundField - with sample code and tutorials

C#.NET And ASP.NET GridView Control With BoundField

this is a simple example of GridView Control in ASP.NET and C#.NET. You can Code your GridView very Esay and simply.
BoundField.DataField="Column1"; This means that column1 will display in the GridView.
finally you can Bind your Gridview with your datatable




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 DBLayer;
public partial class Default4 : System.Web.UI.Page
{
DataTable bookingtable;
DBHelper objDBHelper;
protected void Page_Load(object sender, EventArgs e)
{
objDBHelper = new DBHelper();
string cmd = string.Format("SELECT dealId,BirthdayId,ddDealItem,ddgivenWithItem,DealItemPrice, SeatNumbet,AllotedDate, Venue FROM Table1)", Request.QueryString["id"]);
bookingtable = objDBHelper.GetReaderTable(cmd, 8);
if (bookingtable.Rows.Count > 0)
{

for (int i = 0; i < bookingtable.Rows.Count; i++)
{
DateTime d = Convert.ToDateTime(bookingtable.Rows[i][6]);
bookingtable.Rows[i][6] = d.ToShortDateString();
}
if (!IsPostBack)
{
ButtonField bfcol = new ButtonField();
bfcol.HeaderText = "SELECT";
bfcol.ButtonType = ButtonType.Link;
bfcol.CommandName = "select";
bfcol.Text = "Select";

BoundField DID = new BoundField();
DID.DataField = "Column1";
BoundField BID = new BoundField();
BID.DataField = "Column2";

BoundField Item = new BoundField();
Item.DataField = "Column3";
Item.HeaderText = "Item";
Item.ItemStyle.HorizontalAlign = HorizontalAlign.Left;

BoundField Extra = new BoundField();
Extra.DataField = "Column4";
Extra.HeaderText = "Extra Item";
Extra.ItemStyle.HorizontalAlign = HorizontalAlign.Left;

BoundField Price = new BoundField();
Price.DataField = "Column5";
Price.HeaderText = "Price";
Price.ItemStyle.HorizontalAlign = HorizontalAlign.Center;

BoundField Seat = new BoundField();
Seat.DataField = "Column6";
Seat.HeaderText = "Maximum Seats";
Seat.ItemStyle.HorizontalAlign = HorizontalAlign.Center;

BoundField date = new BoundField();
date.DataField = "Column7";
date.HeaderText = "Date of birthday";
date.ItemStyle.HorizontalAlign = HorizontalAlign.Center;

TextBoxControlBuilder txt = new TextBoxControlBuilder();
txt.ID = "txtPrice";


BoundField venue = new BoundField();
venue.DataField = "Column8";
venue.HeaderText = "Venue";
venue.ItemStyle.HorizontalAlign = HorizontalAlign.Center;


GridView1.Columns.Add(DID);
GridView1.Columns.Add(BID);
GridView1.Columns.Add(Item);
GridView1.Columns.Add(Extra);
GridView1.Columns.Add(Price);
GridView1.Columns.Add(Seat);
GridView1.Columns.Add(date);
GridView1.Columns.Add(venue);
GridView1.Columns.Add(bfcol);
}
GridView1.DataSource = bookingtable.DefaultView;
GridView1.DataBind();
GridView1.Columns[0].Visible = false;
GridView1.Columns[1].Visible = false;
}
}
}

No comments: