WinForms reportViewer (.NET 3.5) binding data from a DataTable - c#

WinForms reportViewer (.NET 3.5) binding data from a DataTable
I'm trying to bind a DataTable which is dynamically created, to a reportViewer control . There are many tutorials on this on the internet but they don't seem to work for this project... (there are related questions on stack overflow but the solutions don't work in my case)
This is the event handler for the Show Report Button:
private void btn_ShowRwport_Click(object sender, EventArgs e)
{
//Set the date range for the report
rptctn.SetDateRangeOne(listBox1.SelectedItem.ToString(), dateTimePicker_Start.Value, dateTimePicker_End.Value);
//Get the report datatable
DataTable dt = rptctn.GetReportTest(listBox1.SelectedItem.ToString());
//Set the datagridview
dataGridView1.DataSource = dt;
//Set the reportViewer
this.reportViewer1.LocalReport.DataSources.Clear();
Microsoft.Reporting.WinForms.ReportDataSource rprtDTSource = new Microsoft.Reporting.WinForms.ReportDataSource(dt.TableName, dt);
this.reportViewer1.LocalReport.DataSources.Add(rprtDTSource);
this.reportViewer1.RefreshReport();
}
I know the DataTable is being retried properly as the dataGridView works...
https://docs.google.com/file/d/0B6b_N7sDgjmvZHpEYS1BWWhqZ3c/edit?usp=sharing
The project has the following constraints:
Must use .NET 3.5 due to client environment. (so report viewer 2008)
Creation of DataTable and the ReportViewer are in different packages (application/presentation layer)
I'm trying to avoid creating .rdlc files as reports (DataTables) have a variable number of rows/columns.
Anyone know how i can bind a dynamicly created DataTable to a report viewer in my case?

Really what you're asking to do is dynamically create an RDLC file and add a table. I found a 3rd party example with some code from gotreportviewer.com, just look down on the right hand side of the page for Generate RDLC dynamically - Table.

Related

How to display the given table data from sqlserver into crystal report using c# winforms?

I am developing a winforms application in 3-tier architecture in the form of
connection oriented using system.data.sqlclient namespace.
So in my database i have a ward table, the fields like ward_id,ward_name,
member_name etc.
So i want to display this on crystal report using c#.
Right click on my project -->Add New Item --> CrystalReports
After selecting it display wizard so i have selected and the next wizard will
display i.e standard report creation wizard like below.
In this,no items found is shown.
But in Project Data folder--> .NET Objects it shown all my tables like below.
But the tables are shown in the above pic when i expand actually those are
not the fields for that particular table example in my ward table.
The fileds shown like below
My problem is that, how to get my fields like ward_id,ward_name etc. in my
crystal report.
I am new to crystal report can you solve my issue?
Suppose if i add dataset in my project but in that i need to enter all the
columns with relations in dataset object. But my table contains nearly 35
fields with relations this is very complex. So, instead of dataset can i get
another wary to display data as report in crystal report viewer.
VIS_wardinformation1 is the class in my project;
tbl_wardinformation is my table in sql server.
Actually in my wardInformation form i have a print button when user clicks on
print button i will display total records on form2
In wardInformation Form:
form2 f=new form2();
f.show();
Then, in my form2 load event:
CrystalReport2 crystalReport = new CrystalReport2();
DataSet dsCustomers = GetData();
crystalReport.SetDataSource(dsCustomers);
this.crystalReportViewer1.ReportSource = crystalReport;
this.crystalReportViewer1.RefreshReport();
My GetData(): it gives all the records of my ward table:
dt = WLogic.GetInformation();
ds.Tables.Add(dt);
return ds
So in my ds contains data i used connection oriented architecture.
But in runtime when i click on print button it shows "File NotFound Exception Was
Un handled" "No source available" page is displayed instead of displaying
data what is the issue here?
Note: I didn't create dataset means example.xsd file. Instead of this i wrote
storedprocedure for getting all the records from back end and i called sp from
front and i have stored the given result into dataset. that's what i did.
Is there any wrong in my approach? please tell?what's the issue?
Thanks

c# datagridview not displaying data on second pass

Background :-
Winforms app that imports data & performs some statistical analysis - stores the results in various data tables - displays the results in a datagridview & chart control.
User can select the various statistical tests via a combobox & this updates the datagridview & chart with the appropriate data.
All works fine on the first pass (i.e.:- first time round). The user then has the option of starting afresh with the same, or new, data to include additional tests.
When the user does this with the same data, adding additional tests, the datagridview does not display the bound datatable information for any test reviewed previously.
When the user selects a new instance I am doing the following with the data tables / datagridview :-
dataGridView1.DataSource = null;
dataGridView1.Refresh();
dataGridView1.Update();
dataGridView1.DataSource = dataTable;
dataTable.Clear();
dataTable.Reset();
Interesting point is that the datagridview is only blank for the tests that have been reviewed. The other data appears as expected.
If a new dataset is imported then all test rests are visible.
If the user exits the application & re-launches it all test results are visible.
It seems that the data is not visible in the datagridview for a data table that has been reviewed in a previous run.
Any ideas on how-to resolve this would be greatly appreciated.
Thanks.
IMHO, when you clear and reset DataTable, DataSource is also cleared. Try this:
DataTable newDataTable = dataTable;
dataGridView1.DataSource = null;
dataGridView1.Refresh();
dataGridView1.Update();
dataGridView1.DataSource = newDataTable;
dataTable.Clear();
dataTable.Reset();
I had the same problem with datagridview. In my case, the datagridview was on a panel. And datagridview didn't renew after updating data on it. Updating was done succesfully on database. But after updating it coulnt be got filled. I spent many time solving this problem. End of the day I have taken datagridview out of panel. And the problem was gone!

c# - How to pass the data from grid view to report Viewer?

Staff gave some research, but still could not understand how to do, I have little knowledge in C #.
I have a grid view where I add several items that were in it, now I need to move these items to a report View report.
I do not know how to do, it would be better to pass all data grid for vestments or pick up the item ids of each output as they are distinct values ​​(eg, are items that correspond to various outputs) and make a query and bring the data to the report ... what would be the most feasible??
Assuming you're in Local-Processing Mode since no further information was supplied.
ReportDataSource is how you supply data to a ReportViewer Control in local-mode.
http://msdn.microsoft.com/en-us/library/ms251736(v=vs.90).aspx
Now after going to GridView Page on MSDN,
I found that GridView.DataSourceObject implements IDataSource. (Which is a parameter option for creating a ReportDataSource).
Meaning you should be able to create a ReportDataSource to supply to your ReportViewer Control, just create the ReportDataSource (Rds) from your GridView like so,
ReportDataSource Rds = new ReportDataSource("DataSetName", GridView.DataSourceObject);
you can use below code for viewing data in report viewer with gridview data.
int id =Convert.ToInt32(txtID.Text);
ReportViewer1.ProcessingMode = ProcessingMode.Local;
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report1.rdlc");
DataTable dt = GridView1.DataSourceObject;
if (dt.Rows.Count > 0)
{
ReportDataSource rds = new ReportDataSource("DatasetName", dt);
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(rds);
}
Reference code

C# create report programmatically

I want to create a report, using either Crystal reports or RDLC, doesn't really matter which. I can get all the data sources together as a series of dynamically generated textboxes etc, but how do I add that to a report?
Eg I want customer name and all of their ordered items in a report. Now I can get all of the information in an array... how would I then place that into a Crystal Report?
Any good introductions that cover non-wizards for Crystal Reports would be amazing.
Every datasource of your report has a name (menu report->datasources, It can be not exact because my vs is not in English).
Supose that one of your datasources name is prj_folder_classSample, and classSample is a class of your project. Then you need to add a List to the report.
Let's do it.
List<classSanple> lst = new List<classSample>
lst.Add(...) //Add various instances of classSample
BindingSource thisIsABindingSource = new BindingSource();
thisIsABindingSource.DataSource = lst;
reportDataSource rds = new ReportDataSource("prj_folder_classSample", thisIsABindingSource);
ReportViewer1.ProcessingMode = ProcessingMode.Local;
ReportViewer1.LocalReport.EnableExternalImages = true;
ReportViewer1.LocalReport.ReportEmbeddedResource = "YourProject.Folder.reportName.rdlc";
ReportViewer1.LocalReport.DataSources.Add(rds)
I do it in this way. Hope It helps you.
Look at this link http://msdn.microsoft.com/en-us/library/cc281022.aspx#RDCE if you want to dynamically change your report. This extension is called just before the report is rendered. Microsoft has created a RDL Object Model. With this one you can customize your whole report. But maybe you don't need this extension. Just try first your stuff in the Report Designer.

Display SQL Server table in DataGridView

I think it's kind of noob question but I'm new to SQL Server in .NET and I've already lost several hours on this...
I started new project, inserted DataGridView on empty form and as Data Source I chose Add->Database and I created new SQL Server Database File. I called it db.mdf. Now I get DataSet named dbDataset and BindingSource named dbDataSetBindingSource. I also added LINQ to SQL Classes to my project and dragged my table (where I added some rows before) to my .dbml. Next I doubleclicked my dbDataset and in designer dragged and dropped the same table.
I hope you have an image now ;). The thing is that when I run program, nothing shows in DataGridView. I know that I can do
dbDataClassesDataContext db = new dbDataClassesDataContext();
var records= from rec in db.MyTable select rec;
dataGridView1.DataSource = records;
And it works perfect. But I believe that dbDataSet and dbDataSetBindingSource should work too... How to use them just to show data in DataGridView?
OMG, found it... For any others with this problem in future. Click on BindingSource in designer and in properties choose table in DataMember.

Categories

Resources