Monday, 2 April 2012

how to paging,sorting,filtering in gride view in user control.

first add a class on Ur project  for defining property:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Web;

/// <summary>
/// Summary description for BO
/// </summary>
public class BO
{
    public string table
    {
        get;
        set;

    }
    public string connection
    {
        get;
        set;
    }

    public ArrayList Column
    {
        get;
        set;
    }
}
add a user control on ur project and design the page.
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>




<asp:UpdatePanel ID="up1" runat="server">
<ContentTemplate>
<table align="center">
<tr><td>Search</td><td>
    <asp:TextBox Width="400" ID="tb" runat="server"
        ontextchanged="tb_TextChanged" AutoPostBack="True"></asp:TextBox></td></tr>
        <tr><td colspan="2"><asp:Label ID="lb" runat="server" Visible="false"></asp:Label></td></tr>
<tr><td colspan="2"><asp:GridView  Width="100%" ID="gv" runat="server"
        AllowSorting="True" onsorting="gv_Sorting"
        AllowPaging="True" onpageindexchanging="gv_PageIndexChanging" PageSize="3"
        CellPadding="4" ForeColor="#333333" GridLines="None">
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    <EditRowStyle BackColor="#999999" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#E9E7E2" />
    <SortedAscendingHeaderStyle BackColor="#506C8C" />
    <SortedDescendingCellStyle BackColor="#FFFDF8" />
    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    <PagerStyle HorizontalAlign="Right" />
    </asp:GridView></td></tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
C# code of the user control :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

public partial class WebUserControl : System.Web.UI.UserControl
{
    #region
    SqlConnection cn;
    SqlDataAdapter da;
    DataTable dt = new DataTable();
    DataSet ds = new DataSet();
    string s; int i,j,k;
    ArrayList al = new ArrayList();
    string[] ss,ss1;
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["x"] = 1;
           
        }
       
    }
    protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gv.PageIndex = e.NewPageIndex;
        DataTable dt = (DataTable)ViewState["dt"];
        gv.DataSource = dt;
        gv.DataBind();
    }
  
    protected void gv_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (Convert.ToInt32(ViewState["x"]) == 1)
        {
          
            ViewState["x"] = 2;
            DataTable dt = (DataTable)ViewState["dt"];
            s = "";
            s= e.SortExpression.ToString()+" asc";
            dt.DefaultView.Sort = s;
            gv.DataSource = dt;
            gv.DataBind();
        }
        else
        {
        
            ViewState["x"] = 1;
            DataTable dt = (DataTable)ViewState["dt"];
            s = "";
            s = e.SortExpression.ToString() + " desc";
            dt.DefaultView.Sort = s;
            gv.DataSource = dt;
            gv.DataBind();


      
        }
    }
    public void view(BO o)
    {
        lb.Visible = false;
        cn = new SqlConnection(ConfigurationManager.ConnectionStrings["" +o.connection  + ""].ToString());
        cn.Open();
        s = string.Empty;
        if(o.Column[0].ToString() == "*")
        {
            da = new SqlDataAdapter("select * from " + "" + o.table + " ; SELECT COLUMN_NAME , DATA_TYPE  FROM INFORMATION_SCHEMA.COLUMNS  WHERE  TABLE_NAME ='" + o.table + "'", cn);
        }
        else
        {

            if (o.Column[0].ToString().IndexOf('=')>=0)
        {
            for (i = 0; i < o.Column.Count; i++)
            {
                ss1 = o.Column[0].ToString().Split('=');
                ss = o.Column[i].ToString().Split('=');
                s = s + ss[0] + " " + ss[1] + ",";
            }
            da = new SqlDataAdapter("select " + "" + s.Substring(0, s.Length - 1) + "" + " from " + "" + o.table + " order by " + ss1[0] + " desc" + "; SELECT COLUMN_NAME , DATA_TYPE  FROM INFORMATION_SCHEMA.COLUMNS  WHERE  TABLE_NAME ='" + o.table + "'", cn);
        }
        else
        {
            for (i = 0; i < o.Column.Count; i++)
            {
              
                s = s + o.Column[i].ToString() + ",";
            }
            da = new SqlDataAdapter("select " + "" + s.Substring(0, s.Length - 1) + "" + " from " + "" + o.table + " order by " + o.Column[0].ToString() + " desc" + "; SELECT COLUMN_NAME , DATA_TYPE  FROM INFORMATION_SCHEMA.COLUMNS  WHERE  TABLE_NAME ='" + o.table + "'", cn);
        }
        }
      
        da.Fill(ds);
        gv.DataSource = ds.Tables[0];
        gv.DataBind();
        ViewState["dt"] = ds.Tables[0];
        ViewState["dt1"] = ds.Tables[1];
        ViewState["cl"] = o.Column;
    }
    protected void tb_TextChanged(object sender, EventArgs e)
    {

      try
        {

            DataTable dt = (DataTable)ViewState["dt"];
            DataTable dt1 = (DataTable)ViewState["dt1"];
            al = (ArrayList)ViewState["cl"];
            if (al[0].ToString() == "*")
            {
                for (i = 0; i < dt1.Rows.Count; i++)
                {
                    s = "";
                    for (j = 0; j < dt.Rows.Count; j++)
                    {
                        if (tb.Text.Trim().ToUpper() == dt.Rows[j][dt1.Rows[i][0].ToString()].ToString().Trim().ToUpper())
                        {
                            s = dt1.Rows[i][0].ToString();
                            dt.DefaultView.RowFilter = "" + s.ToString() + "='" + tb.Text.Trim() + "'";
                            gv.DataSource = dt;
                            gv.DataBind();
                            break;
                        }
                    }
                    if (s != "")
                        break;
                }
            }
            else
            {
                if (al[0].ToString().IndexOf('=') >= 0)
                {
                    for (i = 0; i < dt1.Rows.Count; i++)
                    {
                        s = "";
                        for (k = 0; k < al.Count; k++)
                        {
                            ss = al[k].ToString().Split('=');
                            if (ss[0].ToString().Trim().ToUpper() == dt1.Rows[i][0].ToString().Trim().ToUpper())
                            {
                                for (j = 0; j < dt.Rows.Count; j++)
                                {
                                    if (tb.Text.Trim().ToUpper() == dt.Rows[j][ss[1].ToString().Trim()].ToString().Trim().ToUpper())
                                    {
                                        s = ss[1].ToString().Trim();
                                        dt.DefaultView.RowFilter = "" + s.ToString() + "='" + tb.Text.Trim() + "'";
                                        gv.DataSource = dt;
                                        gv.DataBind();
                                        break;
                                    }
                                }

                            }
                            if (s != "")
                                break;
                        }
                        if (s != "")
                            break;
                    }
                }
                else
                {
                    for (i = 0; i < dt1.Rows.Count; i++)
                    {
                        s = "";
                        for (j = 0; j < dt.Rows.Count; j++)
                        {
                            if (tb.Text.Trim().ToUpper() == dt.Rows[j][dt1.Rows[i][0].ToString()].ToString().Trim().ToUpper())
                            {
                                s = dt1.Rows[i][0].ToString();
                                dt.DefaultView.RowFilter = "" + s.ToString() + "='" + tb.Text.Trim() + "'";
                                gv.DataSource = dt;
                                gv.DataBind();
                                break;
                            }
                        }
                        if (s != "")
                            break;
                    }
                }
            }


        }
        catch (Exception ex)
        {
            lb.Text = ex.Message.ToString();
        }
        
 
  
}


design the page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<%@ Register Src="~/WebUserControl.ascx" TagPrefix="uc" TagName="UserControl" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
  <asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager>
    <div>
  <asp:UpdatePanel ID="up" runat="server">
  <ContentTemplate>
  <uc:UserControl ID="uc" runat="server" />
  </ContentTemplate>
  </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>
C# code of the page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    #region
    BO o = new BO();
    ArrayList al = new ArrayList();
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            o.connection = "TestConnectionString";
            o.table = "employee";
            //al.Add("sno=Sl_No");
            //al.Add("fname=First_Name");
            //al.Add("lname=Last_Name");
            //al.Add("mobnum=Mobile_Number");
            //al.Add("city=City");
            //al.Add("state=State");
            //al.Add("zip=Zip");
           // al.Add("*");
            al.Add("sno");
            al.Add("fname");
            al.Add("lname");
            al.Add("mobnum");
            al.Add("city");
            al.Add("state");
            al.Add("zip");
            o.Column = al;
            uc.view(o);
        }
    }
}
the page looks like:





No comments:

Post a Comment