RDLC Report not paging with dynamically bound datasource - c#

I have a rdlc report I want to bind a list of my own custom class to as a data source.
I've used some code recommended on this site to build the DataTable
This works really well
//convert my list of Invoices to a DataTable
var dt = invoices.CopyToDataTable(dataTable, LoadOption.PreserveChanges);
//Setup a new DataSource
var rds = new ReportDataSource {Name = "ReportDataSet", Value = dt};
//Add that datasource to my ReportViewer
rvSampleInvoice.LocalReport.DataSources.Add(rds);
//Map my report path, etc
rvSampleInvoice.LocalReport.ReportPath = Server.MapPath("etc");
This all works fine, when I load my page, the report shows, but it only has one 2 pages. the first is the first entry in my invoices collection, and when I click next page, it's a blank page.
I've added a gridview to the page, and bound the DataTable (dt) to it and it displays as a nice table with a row of headers and 20 rows of data.
I'm wondering if I've got something wrong on how to bind a RDLC to my List of Invoices

This SO answer has has solved the issue. There's gotta be a better way of doing this, so if anyone knows a better way to have multiple pages for one result set, let me know
Update:
Ok, I'll explain what I've learnt now more about RDLCs for future archeologists. You can't just drag and drop data into a page, bind a list of items and expect it to duplicate the sample page you've made however many times there are items in your list.
You need to drag a table into the report and tick the "page break" option in the SO solution I linked to. Then stretch the table across the page, add a bunch of rows and columns. The fill in the cells you want in your page.
It takes a bunch of cell merging and stretching to get it to look the way I want, so if there's a better way, someone please make a post.

Related

Converting value from datagridview to a text box

My code is below, I cannot for the life of me get the value from a Data table to display in a text box on my main form. I recently came to a realization that DataGridView is not the same as DataTable. Which I think got me much closer. I was originally having issues getting to the specific index as datatable doesn't work that way. Now that I'm using datagridview I feel like I'm pretty much there but missing something silly.
My Code
private void dt_SelectionChanged(object sender, EventArgs e)
{
DataGridView dgv = new DataGridView();
dgv.DataSource = dt;
if (dgv.SelectedCells.Count > 0)
{
active_Description.Text =
Convert.ToString(dgv.CurrentRow.Cells["Description"].Value);
active_Cost.Text = Convert.ToString(dgv.CurrentRow.Cells["Cost"].Value);
active_Diameter.Text = Convert.ToString(dgv.CurrentRow.Cells["Diameter"].Value);
active_Last_Used.Text = Convert.ToString(dgv.CurrentRow.Cells["CareTaker"].Value);
active_Location.Text = Convert.ToString(dgv.CurrentRow.Cells["Location"].Value);
active_OAL.Text = Convert.ToString(dgv.CurrentRow.Cells["OAL"].Value);
active_Vendor.Text = Convert.ToString(dgv.CurrentRow.Cells["Vendor"].Value);
}
}
True; a DataGridView is a UI device that shows data in a tabular fashion, something similar to how Excel appears. A DataTable is a device that stores data in a tabular fashion. If you had data to store you put it in a datatable, and if you wanted to show it you can tell a datagridview tonuse the table as a source of data but they are very distinct things
DataGridView shows many rows of data at once, whereas text boxes can only show one value. Generally when we mix the two we end up using the DGV as a navigation device (it shows eg 20 rows and we can click one of them to make it "the current row") and any text boxes also using the same data source typically show the current row data
The code you're trying to put together manually there can be much more simply done using data binding where both the DataGridView and the TextBoxes are bound to the same BindingSource; the BS takes care of maintaining notion of "the current row"
To make your life easy when doing this, follow these steps:
add a new file to your project, of type DataSet
open the dataset, right click the surface and add a datatable, call it Product (you look like you're making some kind of product related system)
right click the table and add a new column, call it Description
add another column for Cost(type decimal?) Diameter(int? Decimal?) etc
switch to your form designer
open the data sources window (view menu, other windows) - if you're on .net core it doesn't work at the moment, so here's hoping you're on framework. There are other ways to do this process on core, it's just more of a nuisance
expand all the nodes in Data Sources
you can see parent node, Product, with a little grid looking icon. It has children whose icons look more like textboxes etc
drag product node onto the form; a DataGridView appears, along with a dataset, binding source and navigator (in the bottom tray)
drag a few of the child nodes to the form too, textboxes appear but you'll note that they reuse the existing products binding source
if you inspect the DGV and textboxes you'll find that they have a data source set to the binding source, and the binding source has a data source of the dataset (and data member of products table)
That's really all you need to do; you could put some code into your constructor that fills your dataset with data or you can do it at runtime. do it at runtime for now; run the app, enter 3 or so rows worth of data and then click up and down between them; you'll note that the textboxes change depending on what is the active row in the DGV
If you want to see the code that visual studio wrote to wire all this up it's in the Designer.cs file
If you're getting this data out of a database take a look at my answer to Michal here - actually it has relevance to what is posted above; there Michal refs data out of a db, so a lot of the answer is geared towards pulling that data out and into a dataset, showing it in DGV and textboxes and then sending it back, but the screenshots and other relevant parts about showing and manipulating data (that is in a data set) will help you out too

Dynamic filtering a datagridview in wpf

I am trying to filter a datagridview dynamically, which is filled by input of an xml, using a textbox.
If I start the debugging mode,
- the form will be loaded
- the datagridview will be filled by the input of an xml file (Form1_FormLoad)
- I can add some input to the datagrid view
- I can update a selected row
- I can choose the path where the datagridview should be saved as xml
- I could save the edited/unedited datagridview to an xml file
The saving happens on a equal way like the xml-reading/opening.
But what ever I try, I can't filter this data unbinded datagridview.
I've found 2 methods. Both have the disadvantage (for me as noob) that I have to bind the data first. So, If I would use them I would have to rewrite the whole other code of saving, loading, adding and updating. Because I couldnt add a new row to a data binded source.(error message)
So I would be happy, if someone can suggest some ideas/solutions how I can realize the filtering without
- public dataset ds get; set;
- adding the input of a datagrid in the code
The Code describes the method how I add the xml-input to the datagrid view.
I search solutions since 1 week. Had one, but there the datagrid view would become empty If I write a single letter in the textbox.. And If I erase the single letter, the datagridview would still be empty.
PS. I would like to say, that I dont have any clue of coding. I've searched several coding-snippets and tried them^^ :/
If someone asks, I can post the 2 methods how it works. I tried them too :)
pathbox.Text = Properties.Settings.Default.pathbox;
string somePath = pathbox.Text;
string filename = "test.xml";
string path = Path.Combine(somePath, filename);
DataSet ds = new DataSet();
ds.ReadXml(path);
foreach (DataRow item in ds.Tables["Player"].Rows)
{
int n = dgw1.Rows.Add();
dgw1.Rows[n].Cells[0].Value = item["Name"].ToString();
dgw1.Rows[n].Cells[1].Value = item["Age"].ToString();
}
"Because I couldnt add a new row to a data binded source.(error message)"
Why not?
If you databind it to items in an ObservableCollection, adding snd removing items from the collection would automatically update the view side of it (the datagrid).
I would strongly recommend to use binding, which is a very strong argument for using WPF in the first place.
Look at mvvm pattern. You can probably reuse most of your code. But just moving it to a Viewmodel. Then you get good code separation as well.

Why does my GridView control in c# aspnet display an old table below the new table?

The objective i'm trying to achieve is to display filtered data in the same gridcontrol.
So what I do is e.g A-DROPDOWN clicked, display XDATA in GRIDVIEW1. Then FilterA selected, this filter calls the "ADROPDOWN" clicked event again and then checks the appropriate filter then passes the appropriate sql query, then displays "YDATA" in GRIDVIEW1.
Now when I select B-DROPDOWN, ZDATA is displayed however below it is the YDATA. If I select C-DROPDOWN, VDATA is displayed howver below it is still the YDATA.
If I selected A-DROPDOWN then XDATA would be displayed then if I would selected B-DROPDOWN without selecting any of the filters, WDATA would be displayed cleanly in GRIDVIEW1 without any data below or above it. Would appreciate if anyone has any suggestions. Cannot show my code because its 20000 lines.
Would like to add. The alphabets assigned to the dropdowns and data represents the different datasets.
SOLVED !!!
Incase it is of help to someone. my update panel in my front end was only updating the gridview, not the table where the grid view was located . I simple made it so that my entire table where the gridview was located was updating. Many thanks.

Inserting new row in gridview using an external Add button and a list as data source

I have a grid with three columns, two of which contain drop-downs, all of them getting filled from a web service result set. Now I want to allow the functionality of adding a new record in the grid by clicking an Add button present outside the gridview.
Whenever the user clicks the Add button, a new record should be created in the grid with value list filled in the drop-downs with the available options.
What is the best possible way to achieve this considering extensibility in mind.
Thanks a lot!
P.S. The data source set for the grid is a list.
Add a blank item to the list, and rebind with this new list and dummy item. That's typically one way to do it, or store the insert form in the footer of the columns. I've used that approach.

How change column names randomly in gridview?

I am struggling to show different views of gridview with database values. Here is my requirement.
Database Table:
I need to show one drop downlist with three values 1,2,3. If user selects 3 i Need to show a grid like below
Age,AnnualSales and Assortment are names which are coming dynamically from database.
High,Medium and Core all are values
Here my question is I need to show same attribute names.
Like If the name changes from "Assortment" to "Location" I need to show it in different page index .
In page index 2 i need to show like below.
Please help with solution
One solution could be, generating columns in codebehind. e.g.
BoundField bf = new BoundField();
bf.HeaderText = "HeaderText";
bf.SortExpression = "Datafield";
bf.DataField = "Datafield";
GridView1.Columns.Add(bf);
One could write own TemplateClasses if required. There are lot of examples over net.
one would be
http://www.codeproject.com/KB/aspnet/create_template_columns.aspx
Tweak column headers or columns of gridview as required.
hope this helps you.
You can do this very well by setting the property dynamically.
GridView1.Columns[2].HeaderText = "Location";
I have worked pretty well using GridViews and databound controls in most dynamic ways.

Categories