I have an application that is doing all of our reporting with locally stored RDLs. I have two forms, one is a simple viewer that contains a TabPageControl, and the other is an FLP of controls that the user can use to select report parameters. We are planning on having over 30+ options, but at the moment we only have around 5 (certain ones display for certain reports, the rest are hidden).
The issue I'm running into is that I need to have some way to get the options they chose out of a List or Collection. The method I'm using right now is
public void AddReportToViewer(string reportName, List<ReportParameter> parameterList = null)
{
TabPage newPage = new TabPage();
ReportViewer reportViewer = new ReportViewer();
newPage.Text = reportName;
newPage.Controls.Add(reportViewer);
tbcReports.TabPages.Add(newPage); //adding the report viewer to page, should do this at the end of the method
reportViewer.Reset();
reportViewer.ProcessingMode = ProcessingMode.Local;
reportViewer.LocalReport.ReportPath = "MyPath/reportName";
ReportDataSource rds = new ReportDataSource();
//parse parameters to individual objects to use with method calls
int parameterEmployeeID = 0;
int parameterStoreID = 0;
List<int> parameterStoreIDs = null;
DateTime parameterFromDate = DateTime.Now;
DateTime parameterToDate = DateTime.Now;
//Use parameters to get objects, just one here, but we have multiple
List<EmployeeTimeclockReportObjects> timeclock = reportBL.getEmployeeTimeclock(parameterEmployeeID, parameterStoreID, parameterFromDate, parameterToDate);
reportBindingSource.DataSource = timeclock;
rds = new ReportDataSource("TimeclockEntries", reportBindingSource);
reportViewer.LocalReport.DataSources.Add(rds);
reportViewer.RefreshReport();
reportViewer.Dock = DockStyle.Fill;
}
This all works, but here's where I need help
int parameterEmployeeID = 0;
int parameterStoreID = 0;
List<int> parameterStoreIDs = null;
DateTime parameterFromDate = DateTime.Now;
DateTime parameterToDate = DateTime.Now;
I need to strip the values from the list of report parameters so I can use them in the data population methods. For example, I'm doing something like...
int parameterEmployeeID = Convert.ToInt32(parameterList[parameterList.FindIndex(x => x.Name == "employeeID")].Values[0]);
But then I run into issues on the List<int> parameterStoreIDs, because I can't convert a StringCollection to a List of ints.
Does anyone have any recommendations on either a) converting the StringCollection to a list of ints, or b) anything else I could try doing to get the information back from the second form besides a List<ReportParameters> ???
For question (a), consider something like this:
I am not clear on what variable in the code would be the StringCollection, so for the sake of the example I'm just calling it strings.
List<int> ints = strings.Cast<string>(s => Convert.ToInt32(s)).ToList();
Related
I have a super-class (abstract) and then 2 inherited classes.
SuperClass: Sessions
Class 1: Cycling
Class 2: Running
I also have a list that will hold all of my objects private List<Session> allSessions = new List<Session>();
I also have declared some arrays that hold hard-coded data to populate my objects.
Also, Running and Cycling has an overridden ToString() method that displays different data depending on the class.
public override string ToString() => $"Cycle Average RPM is {AverageRpm} and Average Resistance is {AverageResistance}";
I am using a for loop to create and add new objects into my list like this
for (int i = 0; i < id.Length; i++)
{
Cycling Cycle = new Cycling(id[i], titles[i], date[i], duration[i], difficulty[i], instructor[i],
description[i], averageRpm[i], averageResistance[i]);
// Add new objects to list
allSessions.Add(Cycle);
}
I have a dataGridView that is getting everything from my list and displays it like this:
My problem now is, that I want to display only specific data depending on what you choose in the ComboBox, but something is not working,
The overridden ToString() is not added to the list for some reason and whenever I choose a different option from the ComboBox, nothing is being displayed.
EDIT 1:
// Filter Sessions by type using Linq
var sessions = new List<Session>();
var cyclingSessions = sessions.OfType<Cycling>();
var runningSessions = sessions.OfType<Running>();
listBox1.DataSource = null;
listBox1.Items.Clear();
if (cboMenu.SelectedIndex == 0)
{
// Populate GridView with data
dataDisplay.DataSource = allSessions;
}
else if (cboMenu.SelectedIndex == 1)
{
// Populate GridView with data
dataDisplay.DataSource = cyclingSessions;
}
else
{
// Populate GridView with data
dataDisplay.DataSource = runningSessions;
}
}
You need to filter your sessions list and set that as your data source you can easily filter the list using OfType from System.Linq It would look something like this:
var sessions = new List<Sessions>();
var cyclingSessions = sessions.OfType<Cycling>();
var runningSessions = sessions.OfType<Running>();
dataDisplay.DataSource = cyclingSessions;
I have an asp:Listbox that I need to switch out the items on depending on user selection. Here is what I have tried:
string[] my2012Departments = new string[5];
my2012Departments[0] = "Administration";
my2012Departments[1] = "Imaging Services";
my2012Departments[2] = "IT";
my2012Departments[3] = "Lab";
my2012Departments[4] = "Support Services";
lstDSYDepartment.Items.AddRange(my2012Departments.ToArray());
//The AddRange will also not work without .ToArray()
This however causes the following errors:
1. Cannot Convert from 'string[]' to 'System.Web.UI.WebControls.ListItem[]'
2.The best overloaded method match for ....AddRange.... has some invalid arguments
According to the documentation this should work as long as I put the code in the form load.
You can create a List<string> and assign that as the datasource for your listbox like below
List<string> my2012Departments = new List<string>();
my2012Departments.Add("Administration");
my2012Departments.Add("Imaging Services");
my2012Departments.Add("IT");
my2012Departments.Add("Lab");
my2012Departments.Add("Support Services");
lstDSYDepartment.DataSource = my2012Departments;
lstDSYDepartment.DataBind();
(OR) Instead of assigning a string array; create a array of ListItem[] like below
ListItem[] my2012Departments = new ListItem[5];
my2012Departments[0] = "Administration";
my2012Departments[1] = "Imaging Services";
my2012Departments[2] = "IT";
my2012Departments[3] = "Lab";
my2012Departments[4] = "Support Services";
this.lstDSYDepartment.Items.AddRange(my2012Departments);
The documentation you refer to is for Windows Forms, not for ASP.
Try
lstDSYDepartment.Items.Add("Administration");
lstDSYDepartment.Items.Add("Imaging Services");
...
I am trying to create a list via array like this:
private Application[] GetApps()
{
DataSet ds = new Dataset();
string query = "query";
ds = GetData(query);
var appList = new Application[ds.Tables[0].Rows.Count];
for(var i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
DataRow item = ds.Tables[0].Rows[i];
appList[i].Name = item["Name"].ToString();
appList[i].Confidentiality = int.Parse(item["Conf"].ToString());
appList[i].Id = item["ID"].ToString();
}
return appList;
}
I keep getting an object null error and I know I have to be missing something completely obvious that I'm just not seeing. Do I need to declare the new array in some other way?
When you create appList, you are only creating the array itself. .NET does not automatically populate the array with Application objects for you to manipulate. You need to create a new Application object, and set the properties on that object, then you can assign the object to the array.
There are multiple Application classes withing the .NET framework, none of which seem to match your code, so the below example will simply assume that Application is a custom type of your own design.
for(var i = 0; i < ds.Tables[0].Rows.Count; i++)
{
DataRow item = ds.Tables[0].Rows[i];
Appliction app = new Application();
app.Name = item["Name"].ToString();
app.Confidentiality = int.Parse(item["Conf"].ToString());
app.Id = item["ID"].ToString();
appList[i] = app
}
As an aside, note that you can replace i <= x - 1 with i < x and the behavior is exactly the same.
Finally, you should introduce checks for all of your accessors if there is a chance that they could return null. For example, if item["Name"] returns null, then calling item["Name"].ToString() is equivelant to calling null.ToString(), which will also result in a NullReferenceException.
I have an XML file I am loading and breaking the document down into Ienumerable then putting each element into a label on a winform. sofar I have the following code, which works
public void PopulateGameBoard()
{
XDocument gameFiles = XDocument.Parse(Properties.Resources.Jeopardy);
IEnumerable<string> categories =
from category in gameFiles.Descendants("category")
select (string)category.Attribute("name");
string first = categories.ElementAt(0);
cat1HeaderLabel.Text = first;
string second = categories.ElementAt(1);
cat2HeaderLabel.Text = second;
string third = categories.ElementAt(2);
cat3Label.Text = third;
string fourth = categories.ElementAt(3);
cat4Label.Text = fourth;
string fifth = categories.ElementAt(4);
cat5Label.Text = fifth;
}
The final product is Jeopardy Game Board where the categories and questions will be pulled from an XML file
This is the first of 5 rows that I will need to do this with (5 lists going into 5 rows). I am wondering if there is a better way to code this where I dont end up with 25 statements assigning a variabel to an ElementAt() and then 25 assignments of that variable.
Here I tried to create labels dynamically and assign values to them,This is a hand written code,so no guarentee it will compile ,make necessary changes
public void PopulateGameBoard()
{
XDocument gameFiles = XDocument.Parse(Properties.Resources.Jeopardy);
IEnumerable<string> categories =
from category in gameFiles.Descendants("category")
select (string)category.Attribute("name");
Label[] cat1HeaderLabel= new Label[100];
int i = 0;
categories.Each(p =>
{
cat1HeaderLabel[i] = new Label();
cat1HeaderLabel[i].Text = p;
this.Form.Controls.Add(cat1HeaderLabel[i]);
i++;
});
}
I'm trying to populate an NSTableView with an Image column and some text columns.
At the moment I have populated a purely text columns NSTableView, using the method found in the NSTableView sample in the monomac samples, but I think it might be a big clunky and I'm not sure if I can use this to populate different column types cause it uses a List of NSStrings.
It's something like this:
NSTableColumn showIdColumn = myTableView.FindTableColumn(SHOWID);
showIdColumn.Bind("value",myContentArray,"arrangedObjects.showId",null);
/*
NSTableColumn titleColumn = myTableView.FindTableColumn(TITLE);
titleColumn.Bind("value", myContentArray, "arrangedObjects.title", null);
*/
NSTableColumn dateColumn = myTableView.FindTableColumn(SHOWDATE);
dateColumn.Bind("value", myContentArray, "arrangedObjects.showDate", null);
NSTableColumn comicsColumn = myTableView.FindTableColumn(SHOWCOMICS);
comicsColumn.Bind("value",myContentArray, "arrangedObjects.showComics", null);
foreach (DataRow row in dtShowList.Rows)
{
DateTime d = (DateTime)row["showDate"];
string strDate = d.ToString("ddd, MMM d # htt");
List<NSString> objects = new List<NSString>
{
new NSString(row["showId"].ToString ()),
new NSString(strDate),
new NSString(row["showComics"].ToString())
//new NSString(row[2].ToString()),
//new NSString(row[3].ToString ())
};
}
var dict = NSMutableDictionary.FromObjectsAndKeys(objects.ToArray(), Keys.ToArray());
myContentArray.AddObject(dict);
Any ideas?