I've been studying report viewer recently and I have one problem I cannot resolve...
I'm trying to bind data from a collection of objects (headers) where each object has a collection of child objects (rows). How can this be done? Below are pieces of code that I currently have (somedata is a collection of header objects).
Windows form with ReportViewer control has following:
reportViewer1.ProcessingMode = ProcessingMode.Local;
reportViewer1.LocalReport.LoadReportDefinition(GetReport());
reportViewer1.LocalReport.DataSources.Clear();
var dataSourcesNames = GetDataSourceNames();
var headerSource = new ReportDataSource(dataSourcesNames[0], somedata);
reportViewer1.LocalReport.DataSources.Add(headerSource);
reportViewer1.RefreshReport();
Header object:
public class ReportHeader{
readonly string id;
readonly List<ReportRow> rows;
public ReportData(Header h) {
this.id = h.Id;
rows = new List<ReportRow>();
foreach(RowObject o in h.Rows){
rows.Add(new ReportRow(o));
}
}
public string Id { get { return id; } }
public List<ReportRow> Rows { get { return rows;} }
}
Row object:
public class ReportRow{
readonly decimal sum;
readonly string type;
readonly string code;
public ReportDataRow(RowObject r) {
sum = r.Sum;
type = r.Type;
code = r.Code;
}
public decimal Sum { get { return sum; } }
public string Type { get { return type; } }
public string Code { get { return code; } }
}
I created a report that has all the properties of ReportHeader and a list which should contain all the ReportRows but it doesn't seem work. The only solution was to make two separate collections, ReportHeader collection and ReportRow collection, and then bind them separately like below:
var headerSource = new ReportDataSource(dataSourcesNames[0], somedata);
reportViewer1.LocalReport.DataSources.Add(headerSource);
var rowSource = new ReportDataSource(dataSourcesNames[1], somedata.Rows);
reportViewer1.LocalReport.DataSources.Add(rowSource);
With this solution I would need to make some kind of relation between the collections..? So please help. (note that I simplified the objects a lot for my question)
If i understand your question correctly. Could you just pass in a bool object isHeader and then in your visiability on your .rdlc text box do a function to =Fields!isHeader.value. If you have all your fields layered and hidden correctly you can have a header AND data in the same column.
Related
I am coding an application for my study, but i'm stuck at this point. I made a class called 'Neighborhood'. This class has a string and an int. In my main code I give the class a value and put it in a list. I now want to loop through my list and get the int out of it (put it in a listbox, or do a calculation). How do I get the int out of the list?
class Wijk
{
private string wijken;
private int tijd;
public string Wijken
{
get { return wijken; }
set { wijken = value; }
}
public int Tijd
{
get { return tijd; }
set { tijd = value; }
}
}
Created the list and the instance of the class.
List<object> Uden = new List<object>();
Wijk Wijkeninput = new Wijk();
Now I value the string and int with a combobox and textbox.
private void wijkAanmaken()
{
Wijkeninput.Wijken = Convert.ToString(cbWijken);
Wijkeninput.Tijd = Convert.ToInt16(tbSnelheid.Text);
Uden.Add(Wijkeninput);
}
For this, instead of having an object list, you can have list containing class objects like
List<Wjik> Uden = new List<Wjik>();
then you can access int as follows:
foreach (Wjik obj in listProgram)
{
int tij = Convert.ToInt32(obj.tijd);
}
First the List can be declared like this:
List<Wijk> Uden = new List<Wijk>();
To iterate over it:
foreach(var item in Uden)
{
var myInt = item.Tijd;
var myString = item.Wijken;
//here do whatever you want with the values
}
I am successfully binding to a list of objects and setting this as the DataGridViews datasource. Definiting columns, at run time, which include the appropriate DataPropertyNames.
However I now need to add a list to my object class. The size of this list is dynamic, but always the same size for all instances of my object.
So my question is how can I create my DataGridViewTextBoxColumn to create a column for each items within this list?
Below is my object code, which has been simplified for this question. Within the languages Dictionary will be something like:
"English", "Hello"
"German", "Hallo"
"Spanish", "Hola"
Ideally the Key would appear as the column name.
Looking like this (each row is a StringItem):
public class StringItem
{
#region Attributes ########################################################################
string name; // String name, used to generate the enum for referencing
string comment; // Helpful description of the string item.
Dictionary<string, string> languages = new Dictionary<string, string>(); // For language strings.
#endregion
#region Public Functions ##################################################################
public string Name
{
get { return name; }
}
public string Comment
{
get { return comment; }
set { comment = value; }
}
public Dictionary<string, string> Languages
{
get { return languages; }
set { languages = value; }
}
#endregion
}
Update:
I believe the suggested link in the comments isn't trying to achieve quite the same thing, however it is useful.
I can see that by adding the follow code to my StringItem I can directly access the language dictionary doing myObj["English"]
public string this[string key]
{
get
{
return languages[key];
}
set
{
languages[key] = value;
}
}
However the DataPropertyName, for each column, doesn't quite work liek this. I assume it uses reflections? Can anyone confirm this and tell me if I can implement my own reflection, or whatever DataPropertyName is using, to get my dictionary item.
This is how I set up the columns:
DataGridViewColumn column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "Name";
column.Name = "Name";
dgvStrings.Columns.Add(column);
foreach (string lang in ProjectSettings.Languages)
{
column = new DataGridViewTextBoxColumn();
column.DataPropertyName = lang; // <<<< THIS ISN'T WORKING.
column.Name = lang;
dgvStrings.Columns.Add(column);
}
You could iterate through your list and add a ColumnHeader for each item.
Example:
foreach ( var item in list )
{
someDataGridView.Columns.Add(item + "ColumnHeader", item);
}
Explanation:
You can add columns programmatically. The first argument being the Column name, item + "ColumnHeader" in this case and the 2nd is Column text eg. displayed text, in this case item so if it was German that would be the header.
You can create a DataTable containing columns for your class properties and languages, then after editing the data table in DataGridView, revert it back to List<StringItem>.
To do so, I suppose you have StringItem. I just refactored your code to make it more clean:
public class StringItem
{
public static string[] LanguageNames
{
get { return new[] { "English", "German", "Spanish" }; }
}
public StringItem(string name)
{
Name = name;
Languages = new Dictionary<string, string>();
LanguageNames.ToList().ForEach(x => Languages.Add(x, null));
}
public string Name { get; private set; }
public string Comment { get; set; }
public Dictionary<string, string> Languages { get; set; }
}
Then create methods to convert List<StringItem> to a DataTable and vice versa:
public static class StringItemExtensions
{
public static DataTable ToDataTable(this List<StringItem> list)
{
var dt = new DataTable();
dt.Columns.Add("Name").ReadOnly = true;
dt.PrimaryKey = new DataColumn[] { dt.Columns["Name"] };
dt.Columns.Add("Comment");
StringItem.LanguageNames.ToList().ForEach(x => dt.Columns.Add(x));
list.ForEach(item =>
{
var values = new List<object>();
values.Add(item.Name);
values.Add(item.Comment);
values.AddRange(item.Languages.Values.Cast<string>());
dt.Rows.Add(values.ToArray());
});
return dt;
}
public static List<StringItem> ToStringItemList(this DataTable table)
{
return table.AsEnumerable().Select(row =>
{
var item = new StringItem(row.Field<string>("Name"));
foreach (var lang in StringItem.LanguageNames)
item.Languages[lang] = row.Field<string>(lang);
return item;
}).ToList();
}
}
Now you can edit a List<StringItem> in DataGridView:
var list = new List<StringItem>();
list.Add(new StringItem("Key1"));
list.Add(new StringItem("Key2"));
list.Add(new StringItem("Key3"));
this.dataGridView1.DataSource = list.ToDataTable();
After finish editing, it's enough to export the data table to a List<StringItem>:
var list = ((DataTable)this.dataGridView1.DataSource).ToStringItemList();
Edit to save you from reading through this whole post
tldr: an object's fields should not be static unless you want all instances of that object to have the same value for that field
I'm trying to create and populate an ArrayList of Blog objects. I do know the generic way do this:
create ArrayList of Blogs
loop (some condition)
create new Blog
add this Blog to AL
However, when I attempt to do so within the while(datareader.read()) loop, all of the elements in the ArrayList are exactly the same Blog. Specifically, I end up with an ArrayList filled with multiple pointers to the very last Blog object from the database table. Here is my code:
public static ArrayList AllBlogs()
{
SqlDataReader dr = anonPage.ExecuteReader("SELECT * FROM Kristina_Blogs");
ArrayList allBlogs = new ArrayList();
if (dr.HasRows)
{
while (dr.Read())
{
Blog b = new Blog();
//grab a row from Kristina_Blogs and assign those attributes to b
b.setTitle(dr["title"].ToString());
b.setMessage(dr["message"].ToString());
b.setId(dr["id"]);
allBlogs.Add(b);
}
}
dr.Close();
return allBlogs;
}
As I said before, the result of this is an ArrayList filled with pointers to the very last blog from the Kristina_Blogs table. I imagine the ArrayList allBlogs looks like [b, b, b, ... b] and therefore they ALL get updated when I say b.setTitle() etc. But how can this be the case if I am creating a NEW Blog object at the beginning of each iteration?
Here is some extra info that you don't have to read but it might clear up some confusion about the structure of the problem:
Blog object has id, title, and message fields and their respective getter/setters
Kristina_Blogs is a table representing these blogs with columns for id, title, message
The suggestions say to include a tag for my DB engine but I can't find a tag for it: Microsoft SQL Server Management Studio
This code works perfectly when I use an ArrayList of Strings instead of Blogs
Edit: Including the code from Blog class
public class Blog
{
public App myApp;
public static string Title;
public static string Message;
public static int Id;
//constructors
public Blog() { }
public Blog(App App) { this.myApp = App; }
//all getters and setters look like this
public string getTitle() { return Title; }
public void setTitle(string t) { Title = t; }
}
The main problem you have, as I mentioned in comments is your member variables are static, so when you set the value, they change in all instances. you should change your code this way:
public class Blog
{
public int Id { get; set; }
public string Title { get; set; }
public string Message { get; set; }
}
And fill your list this way, don't forget to add using System.Linq;:
var result = new List<Blog>();
var connection = #"your connection string";
var command = "SELECT * FROM Kristina_Blogs";
var adapter = new System.Data.SqlClient.SqlDataAdapter(command, connection);
var dataTable = new DataTable();
//Get data
adapter.Fill(dataTable);
dataTable.Rows.Cast<DataRow>().ToList()
.ForEach(row =>
{
var b = new Blog();
b.Id = row.Field<int>("Id");
b.Title = row.Field<string>("Title");
b.Message = row.Field<string>("Message");
result.Add(b);
});
return result;
Note:
When you create a member static, it is shared between all instances of that calss.
In C# you can use property to get or set values, you don't need to setX or setY, when you get the value of a property, the get code of that property will execute and when you assign a value to a property the set part of it will execute. you can define properties this way:
Property:
private int id;
public int Id
{
get
{
return id;
}
set
{
id = value;
}
}
or more simple:
public int Id { get; set; }
All of the fields in your Blog class are static, meaning they're shared between all object instances. You want them to be instance field (meaning not static) so that each object has its own copy of each of those values.
Remove the static attributes from your class:
public class Blog
{
public App myApp;
public String Title;
public String Message;
public int Id;
//constructors
public Blog() { }
public Blog(App App) { this.myApp = App; }
//all getters and setters look like this
public String getTitle() { return Title; }
public String getMessage() { return Message; }
public void setTitle(String t) { Title = t; }
public void setMessage(String m) { Message = m; }
}
When you use static variables, all instances of an object will contain the same values in those variables. By removing the static keyword, you are allowing different instances of the object to hold different values.
Now, every time you create a blog object, that object's Title and Message etc, will contain its own information.
I would make a quick method to prevent null value from throwing error
public static string GetSafeString(SqlDataReader reader, int index)
{
if (!reader.IsDBNull(index))
return reader.GetString(index);
else
return string.Empty;
}
Replace this code:
while (dr.Read())
{
Blog b = new Blog();
//grab a row from Kristina_Blogs and assign those attributes to b
b.setTitle(dr["title"].ToString());
b.setMessage(dr["message"].ToString());
b.setId(dr["id"]);
allBlogs.Add(b);
}
With This Code:
while (dr.Read())
{
Blog b = new Blog();
//grab a row from Kristina_Blogs and assign those attributes to b
b.setId(dr.GetInt32(0));
b.setTitle(GetSafeString(dr, 1);
b.setMessage(GetSafeString(dr, 2);
allBlogs.Add(b);
}
Where the number is the index of field in the record and assuming "id" is an integer. Also consider moving creation of "Blog" object outside of loop and just change values.
I have a List that contains a series of transaction objects. What I'm trying to do is to display these transaction objects in a Datagridview control on loading a form, basically the Datagridview should represent something of a transaction register to display the data for each of the transaction objects in the list.
I must admit to a lack of experience when it comes to using Datagridviews and I'm having some difficulty with understanding what I need to do here.
My question is, how do I go about getting the details of each of the objects in the list to display in the Datagridview?
Here is my code.
First the transaction class:
public class Transaction
{
// Class properties
private decimal amount;
private string type;
private decimal balance;
private string date;
private string transNum;
private string description;
// Constructor to create transaction object with values set.
public Transaction(decimal amount, string type, decimal currBal, string date, string num, string descrip)
{
this.amount = amount;
this.type = type;
this.balance = currBal;
this.date = date;
this.transNum = num;
this.description = descrip;
}
// Get and Set accessors to allow manipulation of values.
public decimal Amount
{
get
{
return amount;
}
set
{
amount = value;
}
}
public string Type
{
get
{
return type;
}
set
{
type = value;
}
}
public decimal Balance
{
get
{
return balance;
}
set
{
balance = value;
}
}
public string Date
{
get
{
return date;
}
set
{
date = value;
}
}
public string TransNum
{
get
{
return transNum;
}
set
{
transNum = value;
}
}
public string Description
{
get
{
return description;
}
set
{
description = value;
}
}
public decimal addCredit(decimal balance, decimal credit)
{
decimal newBalance;
newBalance = balance + credit;
return newBalance;
}
public decimal subtractDebit(decimal balance, decimal debit)
{
decimal newBalance;
newBalance = balance - debit;
return newBalance;
}
}
}
Now the code for the "Register" form:
public partial class Register : Form
{
List<Transaction> tranList = new List<Transaction>();
public Register(List<Transaction> List)
{
InitializeComponent();
this.tranList = List;
}
private void Register_Load(object sender, System.EventArgs e)
{
//regView represents the Datagridview that I'm trying to work with
regView.AutoSize = true;
regView.DataSource = tranList;
regView.Rows.Add(tranList[0]);
}
}
And here's the output I get.
There's really two high level approaches to this.
1) Add the manually created rows directly to the DataGridView. In this case, you have to manually update/remove them as things change. This approach is "ok" if you don't intend to alter/change the content of the display after you initialize it. It becomes untenable if you do.
To add it directly, you need to create a DataGridViewRow, and populate it with the individual values, and then add the DataGridViewRow to the DataGridView.Rows.
2) Data bind the DGV. There's many articles about databinding to a DataGridView. In some cases, it's easier to just add your data to a DataTable, and then extract a DataView from that, and bind the DataGridView to the DataView. Other people find it easier to directly bind to a collection.
CodeProject has a decent article to get you started down that path, but a quick Google search will yield many other articles.
http://www.codeproject.com/Articles/24656/A-Detailed-Data-Binding-Tutorial
use as DGV:
DataGridView groupListDataGridView;
column:
DataGridViewTextBoxColumn groupListNameColumn;
column setup should be like this:
groupListNameColumn.DataPropertyName = "name";
use this property, else all columns will be added.
groupListDataGridView.AutoGenerateColumns = false;
populate like this:
private void populateGroupList() {
groupListDataGridView.DataSource = null;
formattedGroupList = new SortableBindingList<DataGridGroupObject>();
foreach (GroupObject go in StartUp.GroupList) {
DataGridGroupObject dggo = new DataGridGroupObject();
dggo.id = go.Id;
dggo.name = go.Name;
formattedGroupList.Add(dggo);
}
groupListDataGridView.DataSource = formattedGroupList;
groupListDataGridView.Invalidate();
}
and model:
public class DataGridGroupObject
{
public int id { get; set; } //this will be match id column
public string name { get; set; } // this will be match name column
}
Simply add using System.Linq; at the top. Then you can do this:
//This will create a custom datasource for the DataGridView.
var transactionsDataSource = tranList.Select(x => new
{
Amount = x.amount,
Type = x.type,
Balance = x.balance,
Date = x.date,
TransNum = x.transNum
Description = x.description
}).ToList();
//This will assign the datasource. All the columns you listed will show up, and every row
//of data in the list will populate into the DataGridView.
regView.DataSource = transactionsDataSource;
I have a Linq to Entities query and I want to select some specific columns and store the new object into a pre-defined object. However, I'm getting the error
<object> does not contain a constructor that takes 0 arguments.
Not sure what is wrong here...
Also not sure if this is the best way or if using anonymous type is better instead of creating a payroll object.
Linq Query
public Payroll GetTestCasePayroll(decimal testScenarioID) //not sure if object is correct return
{
Payroll instance = (from o in DbContext.UI_OnDemandCheckHeader
where o.TestScenarioID == testScenarioID
select new Payroll(o.PayEntityCode, o.PayrollYear, o.PayrollNumber)).First();
//{ PayEntityCode = , PayrollYear = o.PayrollYear, PayrollNumber = o.PayrollNumber }).First();
return instance;
}
Payroll object
class Payroll
{
private string _payEntityCode;
private decimal _payrollYear;
private string _payrollNumber;
public Payroll(string payEntityCode, decimal payrollYear, string payrollNumber)
{
PayEntityCode = payEntityCode;
PayrollYear = payrollYear;
PayrollNumber = payrollNumber;
}
public decimal PayrollYear
{
get { return _payrollYear; }
set { _payrollYear = value; }
}
public string PayEntityCode
{
get { return _payEntityCode; }
set { _payEntityCode = value; }
}
public string PayrollNumber
{
get { return _payrollNumber; }
set { _payrollNumber = value; }
}
Your Payroll class needs a constructor that takes no parameters e.g.
Public Payroll() { }
Linq works by creating an empty instance of the output class and then using the setters on each of the properties. It does not use anything but an empty constructor.