Dynamically Show Hide Column in RDLC report using C# and VB.Net in ASP.Net

kankon
 
on Aug 16, 2020 11:40 PM
Sample_113604.zip
9774 Views

How to hide/show column from report viewer

I am success from this Dynamically Show Hide multiple GridView column using CheckBox in ASP.Net

When i try show/hide 1 or 2 column's from GridView and use button from report viewer it show me all column's 

        protected void ShowRDLC(object sender, EventArgs e)
        {
            Session["MsgBox"] = TextBox11.Text;
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[7] {
            new DataColumn("id"), new DataColumn("name"), new DataColumn("civilid"), new DataColumn("jop"), new DataColumn("fileid"), new DataColumn("markazel3mal"), new DataColumn("eldarajah") });
            foreach (GridViewRow row in GridView1.Rows)
            {
                if ((row.FindControl("CheckBox1") as CheckBox).Checked)
                {
                    string id = row.Cells[1].Text;

                    string name = (row.FindControl("nametxt") as Label).Text.Trim();
                    string civilid = (row.FindControl("civilidtxt") as Label).Text.Trim();
                    string jop = (row.FindControl("joptxt") as Label).Text.Trim();
                    string fileid = (row.FindControl("fileidtxt") as Label).Text.Trim();
                    string markazel3mal = (row.FindControl("markazel3meltxt") as Label).Text.Trim();
                    string eldarajah = (row.FindControl("eldarajahtxt") as Label).Text.Trim();
                    dt.Rows.Add(id, name, civilid, jop, fileid, markazel3mal, eldarajah);
                }
            }

            Session["GridViewRow"] = dt;
            string url = "/Admin/WebForm5_Users.aspx";
            string s = "window.open('" + url + "', 'popup_window', 'width=1100,height=800,left=100,top=100,resizable=yes');";
            ClientScript.RegisterStartupScript(this.GetType(), "script", s, true);
        }

the page from session 

        protected void Page_Load(object sender, EventArgs e)
        {
            TextBox11.Text = Session["MsgBox"].ToString();

            if (!this.IsPostBack)
            {
                if (Session["GridViewRow"] != null)
                {
                    DataTable dt = Session["GridViewRow"] as DataTable;
                    if (dt.Rows.Count > 0)
                    {
                        Webuser dsCustomers = new Webuser();
                        foreach (DataRow dr in dt.Rows)
                        {
                            dsCustomers.Tables[0].Rows.Add(dr["id"].ToString(), dr["name"].ToString(), dr["civilid"].ToString(), dr["jop"].ToString(), dr["fileid"].ToString(), dr["markazel3mal"].ToString(), dr["eldarajah"].ToString());

                        }

                        ReportViewer1.ProcessingMode = ProcessingMode.Local;
                        ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/reportviewer/Reportfullname2020.rdlc");
                        ReportDataSource datasource = new ReportDataSource("Webuser", dsCustomers.Tables[0]);
                       
                        ReportParameter[] param = new ReportParameter[1];
                        param[0] = new ReportParameter("textmsg" , TextBox11.Text);
                        ReportViewer1.LocalReport.SetParameters(param);

                        ReportViewer1.LocalReport.DataSources.Clear();
                        ReportViewer1.LocalReport.DataSources.Add(datasource);
                    }
                }
            }
        }

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Aug 17, 2020 07:09 AM

Hi kankon,

Set the parameter for show and hide columns from code behind.

Pass the parameter and set the column visibility property to using the expression in the RDLC designer.

Steps

using the below link i created the example. 

Display GridView checked rows in RDLC Report in new Popup window using C# and VB.Net in ASP.Net

HTML

<asp:CheckBox ID="chkName" Text="Name" runat="server" OnCheckedChanged="OnCheckedChanged"
    AutoPostBack="true" Checked="true" />
<asp:CheckBox ID="chkCountry" Text="Country" runat="server" OnCheckedChanged="OnCheckedChanged"
    AutoPostBack="true" Checked="true" />
<hr />
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:CheckBox ID="chkSelect" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Id" HeaderText="Id" />
        <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
                <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Country">
            <ItemTemplate>
                <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtCountry" runat="server" Text='<%# Eval("Country") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<br />
<asp:Button ID="btnShow" Text="Show In New Window" runat="server" OnClick="ShowRDLC" />

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[3] {
        new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
        dt.Rows.Add(1, "John Hammond", "United States");
        dt.Rows.Add(2, "Mudassar Khan", "India");
        dt.Rows.Add(3, "Suzanne Mathews", "France");
        dt.Rows.Add(4, "Robert Schidner", "Russia");
        gvCustomers.DataSource = dt;
        gvCustomers.DataBind();
    }
}

protected void ShowRDLC(object sender, EventArgs e)
{
    List<string> columns = new List<string>();
    if (chkName.Checked)
    {
        columns.Add(chkName.Text);
    }
    if (chkCountry.Checked)
    {
        columns.Add(chkCountry.Text);
    }
    Session["HidenColumn"] = string.Join(",", columns);

    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[3] {
    new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
    foreach (GridViewRow row in gvCustomers.Rows)
    {
        if ((row.FindControl("chkSelect") as CheckBox).Checked)
        {
            string id = row.Cells[1].Text;
            string name = (row.FindControl("lblName") as Label).Text.Trim();
            string country = (row.FindControl("lblCountry") as Label).Text.Trim();
            dt.Rows.Add(id, name, country);
        }
    }

    Session["GridViewRow"] = dt;
    string url = "Default.aspx";
    string s = "window.open('" + url + "', 'popup_window', 'width=350,height=200,left=150,top=120,resizable=yes');";
    ClientScript.RegisterStartupScript(this.GetType(), "script", s, true);
}

protected void OnCheckedChanged(object sender, EventArgs e)
{
    if ((sender as CheckBox).Text == "Name")
    {
        if ((sender as CheckBox).Checked)
        {
            gvCustomers.Columns[2].Visible = true;
        }
        else
        {
            gvCustomers.Columns[2].Visible = false;
        }
    }
    if ((sender as CheckBox).Text == "Country")
    {
        if ((sender as CheckBox).Checked)
        {
            gvCustomers.Columns[3].Visible = true;
        }
        else
        {
            gvCustomers.Columns[3].Visible = false;
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    If Not Me.IsPostBack Then
        Dim dt As DataTable = New DataTable()
        dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
        dt.Rows.Add(1, "John Hammond", "United States")
        dt.Rows.Add(2, "Mudassar Khan", "India")
        dt.Rows.Add(3, "Suzanne Mathews", "France")
        dt.Rows.Add(4, "Robert Schidner", "Russia")
        gvCustomers.DataSource = dt
        gvCustomers.DataBind()
    End If
End Sub

Protected Sub ShowRDLC(ByVal sender As Object, ByVal e As EventArgs)
    Dim columns As List(Of String) = New List(Of String)()
    If chkName.Checked Then
        columns.Add(chkName.Text)
    End If
    If chkCountry.Checked Then
        columns.Add(chkCountry.Text)
    End If
    Session("HidenColumn") = String.Join(",", columns)

    Dim dt As DataTable = New DataTable()
    dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})

    For Each row As GridViewRow In gvCustomers.Rows
        If (TryCast(row.FindControl("chkSelect"), CheckBox)).Checked Then
            Dim id As String = row.Cells(1).Text
            Dim name As String = (TryCast(row.FindControl("lblName"), Label)).Text.Trim()
            Dim country As String = (TryCast(row.FindControl("lblCountry"), Label)).Text.Trim()
            dt.Rows.Add(id, name, country)
        End If
    Next

    Session("GridViewRow") = dt
    Dim url As String = "Default.aspx"
    Dim s As String = "window.open('" & url & "', 'popup_window', 'width=350,height=200,left=150,top=120,resizable=yes');"
    ClientScript.RegisterStartupScript(Me.GetType(), "script", s, True)
End Sub

Protected Sub OnCheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
    If (TryCast(sender, CheckBox)).Text = "Name" Then
        If (TryCast(sender, CheckBox)).Checked Then
            gvCustomers.Columns(2).Visible = True
        Else
            gvCustomers.Columns(2).Visible = False
        End If
    End If
    If (TryCast(sender, CheckBox)).Text = "Country" Then
        If (TryCast(sender, CheckBox)).Checked Then
            gvCustomers.Columns(3).Visible = True
        Else
            gvCustomers.Columns(3).Visible = False
        End If
    End If
End Sub

Default page

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        if (Session["GridViewRow"] != null)
        {
            DataTable dt = Session["GridViewRow"] as DataTable;
            if (dt.Rows.Count > 0)
            {
                Customers dsCustomers = new Customers();
                foreach (DataRow dr in dt.Rows)
                {
                    dsCustomers.Tables[0].Rows.Add(dr["Id"].ToString(), dr["Name"].ToString(), dr["Country"].ToString());
                }

                ReportViewer1.ProcessingMode = ProcessingMode.Local;
                ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc");

                ReportParameterCollection reportparameter = new ReportParameterCollection();
                reportparameter.Add(new ReportParameter("HiddenColumn", Session["HidenColumn"].ToString()));
                ReportViewer1.LocalReport.SetParameters(reportparameter);
                ReportViewer1.LocalReport.Refresh();


                ReportDataSource datasource = new ReportDataSource("Customers", dsCustomers.Tables[0]);
                ReportViewer1.LocalReport.DataSources.Clear();
                ReportViewer1.LocalReport.DataSources.Add(datasource);
            }
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        If Session("GridViewRow") IsNot Nothing Then
            Dim dt As DataTable = TryCast(Session("GridViewRow"), DataTable)
            If dt.Rows.Count > 0 Then
                Dim dsCustomers As Customers = New Customers()
                For Each dr As DataRow In dt.Rows
                    dsCustomers.Tables(0).Rows.Add(dr("Id").ToString(), dr("Name").ToString(), dr("Country").ToString())
                Next

                ReportViewer1.ProcessingMode = ProcessingMode.Local
                ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc")

                Dim reportparameter As ReportParameterCollection = New ReportParameterCollection()
                reportparameter.Add(New ReportParameter("HiddenColumn", Session("HidenColumn").ToString()))
                ReportViewer1.LocalReport.SetParameters(reportparameter)
                ReportViewer1.LocalReport.Refresh()

                Dim datasource As ReportDataSource = New ReportDataSource("Customers", dsCustomers.Tables(0))
                ReportViewer1.LocalReport.DataSources.Clear()
                ReportViewer1.LocalReport.DataSources.Add(datasource)
            End If
        End If
    End If
End Sub

Expression

=IIF((Parameters!HiddenColumn.Value LIKE "*Country*"),false,true)

Screenshot