C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Dynamically Bind C# ASP.NET GridView


Posted Date: 25 May 2008    Resource Type: Code Snippets    Category: Databinding

Posted By: Raju.M       Member Level: Gold
Rating:     Points: 10



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;
}
}
}





Responses

Author: Jolly Trivedi    27 Jun 2008Member Level: Silver   Points : 1
DataTable bookingtable;
DBHelper objDBHelper;
bookingtable = objDBHelper.GetReaderTable(cmd, 8);

Here can u please give details of DBHelper...


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Bind GridView Dynamically C# ASP.NET  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to do Paging with Repeater control
Previous Resource: Binding Data From Textbox to Dropdown
Return to Discussion Resource Index
Post New Resource
Category: Databinding


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

conference calls

Contact Us    Privacy Policy    Terms Of Use