Click here to Skip to main content
15,885,435 members
Articles / Web Development / ASP.NET

Stored Procedure Driven Data Grid

Rate me:
Please Sign up or sign in to vote.
4.08/5 (11 votes)
22 Mar 2010CPOL3 min read 27.2K   441   27  
Dynamically updates datagrid columns and formats without changing the code files
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class Basic : System.Web.UI.Page 
{
    public GridFormatter gf;    //will be initialized in "Button_Query_Click", and will be called from "gvMain_RowDataBound"

    protected void Page_Load(object sender, EventArgs e)
    {
        this.gvMain.RowDataBound += new GridViewRowEventHandler(gvMain_RowDataBound);

        this.Label_Info.Text = "";

        if (!IsPostBack)
        {
            this.TextBox_StartsOn.Text = "2010-3-1";
            this.TextBox_EndsOn.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);

            Button_Query_Click(null, null);
        }
    }

    protected void Button_Query_Click(object sender, EventArgs e)
    {
        string _sql = string.Format("EXEC spListDeliveryForServerSide '{0}', '{1:yyyyMMdd}', '{2:yyyyMMdd} 23:59:59'",
                this.TextBox_SearchCriteria.Text,
                DateTime.Parse(this.TextBox_StartsOn.Text),
                DateTime.Parse(this.TextBox_EndsOn.Text));

        using (SqlConnection sqlConn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DB_CONNECTION_STRING"].ConnectionString))
        {
            SqlDataAdapter sqlDa = new SqlDataAdapter(_sql, sqlConn);
            DataTable dt = new DataTable();
            sqlDa.Fill(dt);

            if (dt.Rows.Count > 0)
            {
                gf = new GridFormatter(dt);

                this.gvMain.DataSource = dt.DefaultView;
                this.gvMain.DataBind();
                //
                // CSS support
                this.gvMain.HeaderRow.CssClass = "dyn_grid_header";
                this.gvMain.RowStyle.CssClass = "dyn_grid_row";
                this.gvMain.AlternatingRowStyle.CssClass = "dyn_grid_row_alternate";
                //
                // Set the footer summary row
                gf.SetSummaryColumns(ref this.gvMain);

                this.Label_Info.Text = string.Format("{0:n0} row(s)", dt.Rows.Count);
            }
            else
            {
                this.gvMain.DataSource = null;
                this.gvMain.DataBind();

                this.Label_Info.Text = "No Row found";
            }
        }
    }

    protected void gvMain_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        gf.HandleRowDataBoundEvent(this.Page, ref this.gvMain, e.Row);
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
Turkey Turkey
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions