Scripts: C# :: Web controls :: Library Article #3
Developer's Section
Learn how to pupulate and filter records in a Datagrid Web Control in C# By: Erobo Team Member
|
Hire a Developer for Related Work / Installation | $55 hr
|
|
Rating: | Rate It:
|
Average Votes: (3728)
|
Favorites:
|
|
|
|
In this article we describe how to pupulate and manipulate a Datagrid in C#.
In this example you will learn how to populate and filter records in a DataGrid Web Control in C#. We will use the structure of the datagrid templates to place the header , body and footer of an HTML table. In the code behind we will make a call to the database and assign it to a dataset. Also, We will use custom filtering using a Dataview to get only the needed rows. Finally, we will call a method on databound to achieve some final tweaking of the result.
In file datagridTest.aspx
 | Code Snippet 1 |
 |
|
<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="datagridTest.aspx.cs"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<html>
<head>
<title>Datagrid Web Control in C#</title>
</head>
<body>
<!-- create datagrid Web Control -->
<asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False"
Width="100%">
<TemplateColumn>
<Columns>
<asp:BoundColumn Visible="False" DataField="airplaneID">
</asp:BoundColumn>
<asp:BoundColumn Visible="False" DataField="companyID">
</asp:BoundColumn>
<asp:BoundColumn Visible="False" DataField="flightID">
</asp:BoundColumn>
<asp:TemplateColumn>
<HeaderTemplate>
<TABLE cellSpacing="0" cellPadding="0" width="100%" border="0">
<TR width="100%">
<TD width="20%" class="report_column_header">
<B>AirPlane Name</B></TD>
<TD width="30%" class="report_column_header">
<B>Departure Date</B></TD>
<TD width="30%" class="report_column_header">
<B>Arrival Date</B></TD>
</TR>
</HeaderTemplate>
<ItemTemplate>
<TR>
<TD class="spacer" colspan="3"> </TD>
</TR>
<TR width="100%">
<TD width="30%" class="report_text">
<B>
<%# DataBinder.Eval(Container, "DataItem.airplane_name")%>
</B></TD>
<TD width="20%" class="report_text">
<B>
<%# DataBinder.Eval(Container, "DataItem.flight_departure", "{0:d}")%>
</B></TD>
<TD width="20%" class="report_text">
<B>
<%# DataBinder.Eval(Container, "DataItem.flight_arrival", "{0:d}")%>
</B>
<asp:label id="Msg1" runat="server" Visible="False" Font-Names="Verdana"
Font-Size="X-Small" ForeColor="Red">*On Schedule</asp:label>
</TD>
</TR>
</ItemTemplate>
<FooterTemplate>
</TABLE>
</FooterTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
</body>
</html>
|
In file datagridTest.aspx.cs
 | Code Snippet 2 |
 |
|
namespace airport_project
{
using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public class airplane_schedule
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Label Msg1;
private void Page_Load(object sender, System.EventArgs e)
{
//Set any methods on page load here
populateDataGrid();
}
private void populateDataGrid()
{
DataSet dataset = null;
try
{
string connStr = "server=localhost;"
+ "database=Northwind;uid=sa;pwd=manager";
string sqlText = "Select * from table_airport_flights"
MySqlConnection myConn = new MySqlConnection(connStr);
myConn.Open();
MySqlTransaction myTrans = myConn.BeginTransaction();
MySqlCommand cmd = new MySqlCommand(sqltext, myConn);
cmd.Transaction = myTrans;
MySqlDataAdapter adapter = new MySqlDataAdapter();
adapter.SelectCommand = cmd;
dataset = new DataSet();
adapter.Fill(dataset);
adapter.Dispose();
cmd.Dispose();
}
catch(MySqlException ex)
{
Response.Write(ex);
throw;
}
catch(Exception ex)
{
Response.Write(ex);
throw;
}
DataSet flightSchedules = new DataSet();
dataset.TableName = "Flights";
flightSchedules.Tables.Add(dataset.Copy());
Dataview dv = flightSchedules.Tables["Flights"].DefaultView;
dv.RowFilter = " flight_number >= '" + 1789 + "'"
DataGrid1.DataSource = dv;
}
// call an on ItemDataBound to set / or customize datagrid elements
private void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
try
{
int flightId = Convert.ToInt32(((DataRowView) e.Item.DataItem).Row["flightID"]);
Label lbl2 = (Label)e.Item.FindControl("Msg1");
if(flightId!=0)
{
lbl2.Text = "<I>[On Schedule]</I>";
}
}
catch(Exception ex)
{
throw;
}
}
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.
DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);
}
}
}
|
|
|
See other Scripts in Web controls
|
Submit Your Scripts:
|
System Message:
- Your C# script has been sent. Please allow 48 hours for processing.
|
If you would like to have your Javascripts published in this section please fill out the form below: |