Google Search

Google
 

Wednesday, January 2, 2008

Binding Reports to windows Treeview control

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using windowsSSRS.SSRS;

namespace windowsSSRS
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
CatalogItem[] lst;
// Report Server defines the URL to root of the
// Reporting Services home page.
public static string ReportServer
{
get { return "http://reporting/reportserver"; }
}

// ReportPath, when appended to the ReportServer property,
// will define the root of the report search.
// For example, to view all available reports on the report
// server, use "/" as the ReportPath. Setting the value to
// "/MyDemoReports", would only show reports and subdirectories
// under http://ReportServer/MyDemoReports.
public static string ReportPath
{
get { return "/SampleReports"; }
}

// There are a couple places where we need to do string
// manipulation to add and remove path seperators -
// sometimes the seperator needs to be passed as an array
// of char, othertimes as a string. These two properties
// simply define both so the code is a little cleaner
// when we do the string munging.
public static char[] PathSeparatorArray
{
get { return pathSeparatorArray; }
}

public static string PathSeparatorString
{
get { return pathSeparatorString; }
}

protected static char pathSeparator = '/';
protected static char[] pathSeparatorArray = { pathSeparator };
protected static string pathSeparatorString = new string(pathSeparator, 1);
private void Form1_Load(object sender, EventArgs e)
{

ReportingService2005 rs = new ReportingService2005();
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
//CatalogItem[] lst;
lst = rs.ListChildren("/", true);

Array name;
for (int i = 0; i < lst.Length; i++)
{
if (lst[i].Type == ItemTypeEnum.Report)
{
listBox1.Items.Add(lst[i].Path.ToString());
}
name=lst[i].Path.Split('/');
}

BuildTree(lst);
//treeView1.Nodes.Insert(0, "/");
treeView1.ExpandAll();
}

private void BuildTree(CatalogItem[] catalogItems)
{

for (int i = 0; i < lst.Length; i++)
{
if (lst[i].Type == ItemTypeEnum.Report)
{
string path = lst[i].Path.Remove(0, 1);
string[] tokens = path.Split('/');
BuildNodesFromTokens(tokens, 0, treeView1.Nodes);
}
}


}

private void BuildNodesFromTokens(string[] tokens, int index,
TreeNodeCollection nodes)
{
TreeNode node = null;

// first, see if a node for the current token
// exists on this level
for (int i = 0; i < nodes.Count; i++)
{
if (nodes[i].Text == tokens[index])
{
// a node was found at this level, no need to continue searching
node = nodes[i];
break;
}
}

// no node was found, will need to create a new tree node
if (node == null)
{
node = new TreeNode();
node.Text = tokens[index];
nodes.Add(node);

// check if this is the final token, which means this token represents a report name
if (tokens.Length - 1 == index)
{

}
}

// if we have not reached the end of the token list,
// increase the level by (move down the tree) and
// call ourselves again
index++;
if (tokens.Length > index)
{
BuildNodesFromTokens(tokens, index, node.Nodes);
}

}



private void treeView1_AfterSelect(object sender, TreeNodeMouseClickEventArgs e)
{
String grp = string.Empty;
String rname = string.Empty;
if (e.Node.Parent != null)
{
grp = e.Node.Parent.Text.ToString();
rname = e.Node.Text;
string ull = "http://localhost/ReportServer/Pages/ReportViewer.aspx?%2f" + grp + "%2f" + rname + "&rs:Command=Render";
webBrowser2.Navigate(ull);
}
else
{
string ull = string.Empty;
webBrowser2.Navigate(ull);
}
}

}
}

No comments: