Create GridView using StringBuilder in C# (.Net)


Hello and welcome once again to my blog. I am great thankful to you guys you made me proud to be a great viewership. Here I just coded some mine idea which I used 2 years back. Yes someone asked me about Grid View without actually using our readymade Grid View control but look like Grid View.

I used it before that so I got chance to share with you, it’s very simple and really very less time consumables. Just go through the code..


    <div>
    <asp:Label ID="lbldispArray" runat="server">asp:Label>
    Sort by <asp:DropDownList ID="ddlSortng" runat="server" AutoPostBack="True"
            onselectedindexchanged="ddlSortng_SelectedIndexChanged">
    <asp:ListItem Value="custName">Customerasp:ListItem>
    <asp:ListItem Value="custAddress">Addressasp:ListItem>
    <asp:ListItem Value="custCity">Cityasp:ListItem>
    asp:DropDownList>
   
    <asp:HiddenField ID="hdnOrderBy" Value="ASC" runat="server" />
    <div id="divGrid" runat="server">div>
    div>

.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text;

public partial class Array : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid(ddlSortng.SelectedValue, hdnOrderBy.Value);
        }
    }

    private void BindGrid(string OrderColumn,string OrderBy)
    {
        string strQuery = "SELECT  custID, custName, custAddress, custCity FROM Customer";

        strQuery += " ORDER BY " + OrderColumn + " " + OrderBy + "";
        String strConnString = System.Configuration.ConfigurationManager
                    .ConnectionStrings["connString"].ConnectionString;
        SqlConnection con = new SqlConnection(strConnString);
        con.Open();
        SqlCommand cmd = new SqlCommand(strQuery);
        cmd.Connection = con;
        SqlDataReader dr = cmd.ExecuteReader();
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
        try
        {
            StringBuilder strBHeader = new StringBuilder();
            strBHeader.Append("
");
            while (dr.Read())
            {
                strBHeader.Append("
");
                strBHeader.Append("
");
                strBHeader.Append("
");
            }
            strBHeader.Append("
NameAddressCity
" + dr["custName"].ToString() + "" + dr["custAddress"].ToString() + "" + dr["custCity"].ToString() + "
");
            divGrid.InnerHtml = strBHeader.ToString();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
            dr.Dispose();
            con.Dispose();
        }
    }
    protected void ddlSortng_SelectedIndexChanged(object sender, EventArgs e)
    {
        hdnOrderBy.Value = hdnOrderBy.Value == "ASC" ? "DESC" : "ASC";
        BindGrid(ddlSortng.SelectedValue, hdnOrderBy.Value);
    }
}

Comments