Contact Us

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: (3516)

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" 
TargetSchema="" %> 

<title>Datagrid Web Control in C#</title> 
<!-- create datagrid Web Control -->
<asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False"
      <asp:BoundColumn Visible="False" DataField="airplaneID">
      <asp:BoundColumn Visible="False" DataField="companyID">
      <asp:BoundColumn Visible="False" DataField="flightID">
        <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>
          <TD class="spacer" colspan="3"> </TD>
        <TR width="100%">
          <TD width="30%" class="report_text">
          <%# DataBinder.Eval(Container, "DataItem.airplane_name")%> 
          <TD width="20%" class="report_text">
          <%# DataBinder.Eval(Container, "DataItem.flight_departure", "{0:d}")%>
          <TD width="20%" class="report_text">
          <%# DataBinder.Eval(Container, "DataItem.flight_arrival", "{0:d}")%>
          <asp:label id="Msg1" runat="server" Visible="False" Font-Names="Verdana"
          Font-Size="X-Small" ForeColor="Red">*On Schedule</asp:label>



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 

    private void populateDataGrid()
      DataSet dataset = null;


         string connStr = "server=localhost;"
                  + "database=Northwind;uid=sa;pwd=manager";

         string sqlText = "Select * from table_airport_flights"

         MySqlConnection myConn = new MySqlConnection(connStr);

         MySqlTransaction myTrans = myConn.BeginTransaction();

         MySqlCommand cmd = new MySqlCommand(sqltext, myConn);
         cmd.Transaction = myTrans;

         MySqlDataAdapter adapter = new MySqlDataAdapter();
         adapter.SelectCommand = cmd;
         dataset = new DataSet();

      catch(MySqlException ex)
      catch(Exception ex)

      DataSet flightSchedules = new DataSet();
      dataset.TableName = "Flights";

      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)
          int flightId = Convert.ToInt32(((DataRowView) e.Item.DataItem).Row["flightID"]);
          Label lbl2 = (Label)e.Item.FindControl("Msg1");
            lbl2.Text = "<I>[On Schedule]</I>";

        catch(Exception ex)
    override protected void OnInit(EventArgs e)

    private void InitializeComponent()
      this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.
      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:
*Your Name or Username:
Home Town:
*Description and Code:
*Enter Code shown
to the right:

[ Refresh Image ]