Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Basics of using DataGrid

This tutorial introduces you to the DataGrid control. It tells you about the BoundColumn and shows an example of how to use it.

On Monday, September 13th 2004 at 05:24 AM
By Andrew Pociu (View Profile)
****-   (Rated 3.7 with 60 votes)
Contextual Ads
More ASP.NET Resources
Advertisement
The DataGrid control, similar to the DataList control but with more features, bounds data and displays it in a grid.



We shall begin with just displaying the records from the database MyDB (which we used in several tutorials, here at Geekpedia and which was created in the tutorial named 'Connecting to a SQL database from ASP .NET I').

Anyway, for setting up fast a connection to the SQL database from the ASP .NET Web Application use the following code inside WebForm1.aspx.cs:



Inside the public class named WebForm1:






// Here's what we need to establish the connection

protected System.Data.SqlClient.SqlConnection sqlConnection1;

protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;

protected System.Data.SqlClient.SqlDataReader sqlDataReader1;



Inside InitializeComponent() - after you expand the region:






this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();

this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();

this.sqlSelectCommand1.CommandText = "SELECT * FROM dbo.MyLinks";

this.sqlSelectCommand1.Connection = this.sqlConnection1;

this.sqlConnection1.ConnectionString = "workstation id=WORKSTATION;packet size=4096;user id=aspnet;data source=WORKSTATION;persist security info=True;initial catalog=MyDB;password=secret";

// This line was already there

this.Load += new System.EventHandler(this.Page_Load);



On the web form (design mode of WebForm1.aspx) drag a DataGrid control:








Now go to the C# code file (WebForm1.aspx.cs) and inside Page_Load() use the following code:





private void Page_Load(object sender, System.EventArgs e)

{

// Open the connection to the database

sqlConnection1.Open();

// Set the DataSource to the result of the Select command

DataGrid1.DataSource = sqlSelectCommand1.ExecuteReader();

// Bind the DataGrid

DataGrid1.DataBind();

// Close the connection to the database

sqlConnection1.Close();

}



Here we simply bind the control to the database, and if you compile now you'll see the result in the browser:








The bind was successful. The title of the columns in the DataGrid is the actual name of the columns inside the database (Title and URL).

Yet we didn't used any of the features of the DataGrid, and that's what we're going to do next.

DataGrid columns


The BoundColumn


BoundColumn is the column that displays the records inside a DataGrid. This is the default column type. You don't have to specify any BoundColumns to display the columns inside the database as they are generated automatically. But if you do specify the columns, you need to set an attribute for the <asp:DataGrid> tag that sets the auto generation of columns to false.



Let's do this - go into the HTML code of WebForm1.aspx and inside the form tag of Form1 use the following code:





<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" runat="server" AutoGenerateColumns="False">

<Columns>

<asp:BoundColumn DataField="Title" HeaderText="Title" />

<asp:BoundColumn DataField="URL" HeaderText="Link" />

</Columns>

</asp:DataGrid>



First you can notice the attribute called AutoGenerateColumns which is set to False, that's because we want to create our own columns.

Then, inside the <Columns> tag we create two BoundColumns and we specify to each of them, the DataField (which is actually the column name from the database) and HeaderText - the text which appears above the column.

Here's the result in the browser:



Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by Marcel Albersdörfer on Wednesday, September 14th 2005 at 08:46 AM

Very Good Tutorial.
It helped me much.

Thx @ Andrei Pociu

by Josh on Thursday, November 17th 2005 at 12:42 PM

Thanks, your tutorial was very helpful.

by tralle on Friday, January 26th 2007 at 02:13 PM

thanks alot!

by maha on Thursday, March 8th 2007 at 01:22 PM

by Ram on Wednesday, April 4th 2007 at 10:26 AM

Thanks, your tutorial is good .

by suresh on Saturday, November 3rd 2007 at 02:29 AM

excellent

by Akash on Tuesday, February 12th 2008 at 03:53 AM

Can you tell me how to insert chechkbox in DataGrid

by milan on Wednesday, April 30th 2008 at 09:46 AM

it was fantastic......explained everything in a simple way...thanks a lot......shall i ask the author one thing....its not related to this topic....an asp application design have two parts...one html view and one design view...should i learn html for creating good programs in asp.net??????if i should learn what are topics that i should learn...i expect your kind reply...

by fdgdfg on Thursday, June 12th 2008 at 02:36 AM

<b>eeffe</b>

by Abhishek on Tuesday, August 12th 2008 at 01:06 AM

very nice but now a days we are not using this pointer in Asp.net

by Asad on Thursday, April 2nd 2009 at 06:18 AM

thanks i am learning asp.net, and found very good help related to grid.

by sameena on Thursday, November 5th 2009 at 02:32 AM

Thanks for explaining the concepts so clearly...

by sameena on Thursday, November 5th 2009 at 02:32 AM

Thanks for explaining the concepts so clearly...

by vidhya on Monday, December 14th 2009 at 11:33 AM

Thanks

by vamsi on Thursday, January 7th 2010 at 01:35 AM

Thanks for sharing.. Easily understood for beginners...

by vamsi on Thursday, January 7th 2010 at 01:35 AM

Thanks for sharing.. Easily understood for beginners...

by vamsi on Thursday, January 7th 2010 at 01:35 AM

Thanks for sharing.. Easily understood for beginners...

by vamsi on Thursday, January 7th 2010 at 01:35 AM

Thanks for sharing.. Easily understood for beginners...

by vamsi on Thursday, January 7th 2010 at 01:35 AM

Thanks for sharing.. Easily understood for beginners...

by vamsi on Thursday, January 7th 2010 at 01:35 AM

Thanks for sharing.. Easily understood for beginners...

by DougF on Thursday, June 24th 2010 at 06:55 AM

Cheers.

I've been using entity models to handle the SQL connections but the rest helped muchly.


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs ASP.NET Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Sponsors
Discover Geekpedia

Other Resources