Click here to Skip to main content
16,004,833 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In application of on line accounts systems, a grid view contains around 70 columns.
But I want to show say only 7 columns at a time. Is it possible? If yes, then how?
Posted

Wouldn't it be better to give a spreadsheet for download?

You could place the grid in a div with the overflows set so it would be scrollable. Or, provide column paging buttons that would cause the grid to be drawn with the next set of columns. A couple of ways for this that I can think of. One, query the datasource and only return the selected columns then bind it to the grid. Two, filter the datasource after querying for the necessary columns. Three, manually create the grid columns each time.
 
Share this answer
 
Comments
Qureshali 6-Feb-12 8:34am    
i have tried to convert the data of grid view to pdf but the problem is, the pdf file generated shrinks the column size and makes it small. The grid view is in the panel which is scrollable and it doesn't take all the columns. the pdf file generated only with those column which are visible on the screen. Rest which are behind the scroll bars are not shown on pdf file. please help me to Solve this problem and my work will be done.
[no name] 6-Feb-12 8:44am    
Do you want to create a PDF or display the grid on a page? You need to clarify your problem before anyone can help.
Qureshali 6-Feb-12 8:47am    
Sorry sir, I want to do both the work but if creating pdf solves my problem then it is better for me. This is my problem:
i have tried to convert the data of grid view to pdf but the problem is, the pdf file generated shrinks the column size and makes it small. The grid view is in the panel which is scrollable and it doesn't take all the columns. the pdf file generated only with those column which are visible on the screen. Rest which are behind the scroll bars are not shown on pdf file. please help me to Solve this problem and my work will be done.
[no name] 6-Feb-12 8:54am    
How are you creating the PDF? The more information you supply the better the answer will be. It will also be faster since we won't need to constantly ask for more information. I understand this is a critical problem for you but we are not working on your project; slow down and explain it fully and completely.
Here is code through which you will find workaround

Aspx code

ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CP_325671_is_it_possible_to_give_paging_in_grid_view_on_colu._Default" %>

<!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">
    <div>

        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
        <asp:Button ID="Button1" runat="server" Text="Page1" onclick="Button1_Click" />
        <asp:Button ID="Button2" runat="server" Text="Page2" onclick="Button2_Click" />
        <asp:Button ID="Button3" runat="server" Text="Page3" onclick="Button3_Click" />

    </div>
    </form>
</body>
</html>


C# Code
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace CP_325671_is_it_possible_to_give_paging_in_grid_view_on_colu
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


            
     

            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string myConnection = "Data Source=MDT765;Initial Catalog=TST;User Id=sa;Password=sa@123;";
            string sqlquery;
            sqlquery = "EXEC FindTable " + 1;
            SqlDataAdapter sqlcom0 = new SqlDataAdapter(sqlquery, myConnection);

            DataSet ds0 = new DataSet();
            sqlcom0.Fill(ds0);
            GridView1.DataSource = ds0.Tables[0].DefaultView;
            GridView1.DataBind();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string myConnection = "Data Source=MDT765;Initial Catalog=TST;User Id=sa;Password=sa@123;";
            string sqlquery;
            sqlquery = "EXEC FindTable " + 2;
            SqlDataAdapter sqlcom0 = new SqlDataAdapter(sqlquery, myConnection);

            DataSet ds0 = new DataSet();
            sqlcom0.Fill(ds0);
            GridView1.DataSource = ds0.Tables[0].DefaultView;
            GridView1.DataBind();
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            string myConnection = "Data Source=MDT765;Initial Catalog=TST;User Id=sa;Password=sa@123;";
            string sqlquery;
            sqlquery = "EXEC FindTable " + 3;
            SqlDataAdapter sqlcom0 = new SqlDataAdapter(sqlquery, myConnection);

            DataSet ds0 = new DataSet();
            sqlcom0.Fill(ds0);
            GridView1.DataSource = ds0.Tables[0].DefaultView;
            GridView1.DataBind();
        }
    }
}



Stored Procedure


SQL
CREATE PROCEDURE [dbo].[FindTable]
     @Page BIGINT
AS
BEGIN
  IF @Page = 1
            SELECT [Column1],[Column2],[Column3] FROM [Table1]
          IF @Page = 2
            SELECT [Column4],[Column5],[Column6] FROM [Table1]
          IF @Page = 3
             SELECT [Column7],[Column8] FROM [Table1]
END

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON


GO



Hope this helps if yes the accept and vote the answer otherwise revert back with your queries
--Rahul D.
 
Share this answer
 
Yes it is, but you need to implement that as custom solution.
First what gets on my mind is to define gridview with all available columns which are hidden in start.
Then you need to make "column pager" (this can be a few hyperlinks controls in your grid footer or below grid), 1-7, 8-14, 15-21 and so on until 70.
Then when you bind grid you will bind it according to selected "pager" value. this means that you will need to hide all other columns that are out of selected range...
use this peace of code to hide columns...
C#
gvReport.Columns[1].Visible = false;
gvReport.Columns[2].Visible = false;
gvReport.Columns[3].Visible = false;
gvReport.Columns[4].Visible = false;
gvReport.Columns[5].Visible = false;
gvReport.Columns[6].Visible = false;
gvReport.Columns[7].Visible = false;

and so on... I guess you can imagine what I want to say...

Another approach is to show all columns available with some List (CheckedListBox or some other control) and alow user to choose what columns he wants to use. Something like this[^].

But after all if you need bullet proof solution find some third party grid control that can show or hide columns dynamically. Here are some:
1. http://demos.devexpress.com/aspxgridviewdemos/Columns/CustomizationWindow.aspx[^]
2. http://demos.telerik.com/aspnet-ajax/grid/examples/generalfeatures/headercontextmenu/defaultcs.aspx[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900