Play-Asia.com - Buy Video Games for Consoles and PC - From Japan, Korea and other Regions!

C# - DataGrid Control

The DataGrid control is a table driven. It is ideal for displaying information that contains many elements or fields or many rows and columns

The DataGrid control also provides many advanced features that make it the right choice for many programming tasks. For example, ou can use the DataGrid control for selecting items, sorting, paging, and editing in place.

 

Using DataGrid Control

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>


<script language="C#" runat="server">


void Page_Load(object src, EventArgs e){
      //Create Instance of Connection and Command object
      SqlConnection cn = new SqlConnection("server=localhost;uid=sa;pwd=;
                                                                                          database=Northwind");
      SqlDataAdapter adapter = new SqlDataAdapter("Select * from products", cn);


      DataSet ds = new DataSet();
      adapter.Fill(ds, "products");
    
     mygrid.DataSource = ds.Tables["products"];
     mygrid.DataBind();

}
</script>


<html>
<head>
  <title>Basic DataGrid Example</title>
</head>
<body>
<asp:datagrid id="mygrid" runat="server" />

</body>
</html>

The definition of the DataGrid control is quite simple, and it actually contains only one line. When the DataBind() method is invoked, the DataGrid control automatically builds the table by creating a column for every data field and a row for every record. It even builds a header, using field names in the data source. The output will be as the following.

Output

 
 
Visitor Review Write a review
No visitor review for this page yet
Write a review
Rating:
Text:
Name:
Email:
(optional)
 
Code:
 

Search:
Shopping Store Online
Clothes (25)
Gift shop (69)
Hand-made (27)
Luxuries (47)
Others (42)
Programming Tutorial
ASP (14)
CSharp
Binding Data to List Control
Data Access with ADO.NET
DataGrid Control
The ArrayList class
The BitArray Class
The Calendar Class
The Hashtable Class
The HttpCookie Class
The HttpRequest Class
The HttpResponse Class
The Queue Class
The Repeater List Control
The SortedList Class
The SqlCommand Class
The SqlConnection Class
The SqlDataReader Class
The SqlParameter Class
The SqlParameterCollection Class
The SqlTransaction Class
HTML (10)
JavaScript (6)
Photoshop (9)
PHP (11)
Entertainment
Anime (6)
E-Cards (12)
Games (20)
Music (7)