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.
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