I'm a student learning C#, with previous experiences with VB. I'm trying to use a list view to display three pieces of information in three separate columns. The Item is a decimal (Object = 3.50m), and the subItems are the quantity and a price. I have managed to get the first two columns showing the data with:-
var item = lsvstarter.Items.Add(cmbStarter.Text);
item.SubItems.Add(cmbStrQuantity.Text);
The third column should show the worth of the item multiplied by the quantity, so it would be
1st Column "3.50"
2nd Column "3"
3rd Column (3.50 * 3) "10.5"
But the method I used for the first subitem will not work for the variable that should be displayed by the third column. This being
item.SubItems.Add(Startertotal);
The ListView includes 3 columns. I had this program working on VB since the scenario is the same but using translators didn't work successfully. All of the objects in the Design view are the same.
Also the calculation for the "Startertotal" variable has already been calculated in a loop beforehand. Does anyone know what I'm doing wrong and the fix?
MSDN: SubItems.Add takes either a string or a SubItem as its first parameter.
So you need to change the numeric value to a string in one way or another:
item.SubItems.Add(Startertotal + " ");
item.SubItems.Add(Startertotal + "");
item.SubItems.Add(Startertotal.ToString());
item.SubItems.Add(Startertotal.ToString("###0,00"));
item.SubItems.Add(String.Format("Sum: {0} ", Startertotal));
or whatever formatting you want..
Related
I'm trying to get the subtotals for a table in C# using Microsoft.Interop.Excel, and there is a built in function for that.
[Here][1] is the link to the documentation for the Subtotal function.
I understand what GroupBy and Function parameters do, but what exactly is the TotalList parameter for? Microsoft describes it as:
"An array of 1-based field offsets, indicating the fields to which the subtotals are added."
How are these offsets being read? In pairs for row and column offset? In examples I see whole arrays of many numbers being used for this. How do I make use of this?
[1]: https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel.range.subtotal?view=excel-pia
You pass an array of as many numbers as you have columns for which you want subtotals calculated. If you only have one column to subtotal, you can just pass in a single number in place of an array (at least, in VBA that works)
Eg: (in VBA) -
Range("A1").CurrentRegion.Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(3, 5)
would group a table of data starting in A1, according to the first column, and add subtotals in the third and fifth columns.
I'm writing a program and it uses RadGridView - I've got a summary row which I want to total all of the values in a certain column. I have the row setting up correctly however, it only seems to be totalling up the number of rows and now the value contained within.
GridViewSummaryItem summaryItem = new GridViewSummaryItem("order_qty", "Total = {0}", GridAggregateFunction.Count);
GridViewSummaryRowItem summaryRowItem = new GridViewSummaryRowItem();
summaryRowItem.Add(summaryItem);
radGridView1.Templates[0].SummaryRowsTop.Add(summaryRowItem);
btn_generate_pickList.Visible = true;
My code for this is above.
I've also included a screen shot of the result given below.
Will this be happening because the values are strings and need converting?
As mentioned by TriV, had to use Sum instead of Count.
I've a ListView that fetch data from SQL compact CE database. I've an ID column in the database already , but I want to add an auto number column in the list view itself.
I want to replace the ID column with an auto number column (I don't want the number to be saved in the database itself , I just want to sort the rows in the list view)
http://i.imgur.com/nenhN2w.jpg
The code I'm using :
ListViewItem item = new ListViewItem(sqldr["ID"].ToString());
item.SubItems.Add(sqldr["EnglishWord"].ToString());
item.SubItems.Add(sqldr["EnglishDesc"].ToString());
item.SubItems.Add(sqldr["ArabicDesc"].ToString());
item.SubItems.Add(sqldr["ArabicWord"].ToString());
item.SubItems.Add(sqldr["Subject"].ToString());
listView1.Items.Add(item);
I'm using C# - Windows Application - WinForms
You could do something like this:
ListViewItem item = listView.Items.Add((listView.Items.Count + 1).ToString());
However the problem here is you may get duplicate numbers if you add/remove, if you did this in one go never to be repeated it'd be less of an issue.
To avoid this you'd probably need some kind of class global counter, and then do:
int counter = 0;
ListViewItem item = listView.Items.Add((++counter).ToString());
This would ensure that any new addition to the list will get the next number up, so it's always increasing.
In the tablix if've got 2 fields (Quantity and Price) that is populated from the database (dataset fields) witch works great. Next to those two columns I added another column Total which I use to get the total of the quantity * Price
=Fields!Quantity.Value*Fields!Price.Value
That works fine as well.
I then added a 3 textboxes at the bottom of that column (within a rectangle) which I want to use to do a Subtotal (sum of the Total), The vat and the the Grand Total after tax had been added. The problem is, is that I cannot add a dataset to a textbox, thus I cant use the Field Section when doing the expression which leaves me that I have to use the dataset section in expression to do this. This is how this looks in the dataset section: (and give me the following error:
=Sum(Sum(Fields!Quantity.Value, "DataSetItemsRequested")*Sum(Fields!Price.Value, "DataSetItemsRequested"))
The Value expression for the textrun 'Textbox53.Paragraphs[0].TextRuns[0]' contains an aggregate function (or RunningValue or RowNumber functions) in the argument to another aggregate function (or RunningValue). Aggregate functions cannot be nested inside other aggregate functions.
Is there a way that I can use a simple sum of that Total column or how do I add the dataset to the textbox or which is the correct way of how I can do this?
=Sum(ReportItems!txtTotal.Value) doesn't work either....
Can you just add a Footer row to the Tablix and just add the following expression:
=Sum(Fields!Quantity.Value * Fields!Price.Value)
If you want this value outside the Tablix, you can use a similar expression:
=Sum(Fields!Quantity.Value * Fields!Price.Value, "DataSet1")
Expressions in action:
Result:
Rather than using TextBox, include your value in Tablix: insert another row inside/outside group and place your expression there.
I have a stored proc that returns 4 rows in a table. Each row corresponds to a day a person is staying at a hotel, so when I bind a repeater to it, 4 rows show up. Two of the rows are the same room and the other two are the same room. I want to display credit card information, but not for each individual room, only for every set of rooms that is the same, so it would look something like this:
12/7/2011 Room 1
12/8/2011 Room 1
Credit Card Info
12/7/2011 Room 2
12/8/2011 Room 2
Credit Card Info
I don't want to combine Room information and credit information into a one row each. How can I get this to format the way I want it to?
Currently I am displaying Room Information in an ItemTemplate and inside the ItemTemplate is a tr and td's.
You basically have two options: you can pre-process the data so that what is bound to the repeater represents what you want to present to the user - DJ KRAZE has shown one way to do that.
The other option would be to add an ItemDataBound event handler and manipulate the data and/or the current row at that point.
One drawback of the 2nd approach is that you need access to the prior row in order to make your decision - that may complicate things, especially if you have any paging of the results.
//Create either a HashTable()
//or a Dictionary<int, string[]> I would personally use a Dictionary<>
//create an instance of what ever collection you want to use or array
//List<string>
//Dictionary<int, string[]>() using the room number as the key
// you would add the values to either the List<> or a String[] array or a Dictionary
keey in mind that if you want to create a dynamic string[] array and you do not know how many elements you are going to add just use a List<> or Dictionary<int,string>
and you can always convert that object using .ToArray
so for example
List<string> lstRoomList = new List<string>();
string[] arrayRoom = {};
if you have a DataReader for example and you are doing something like
while (yourdataReader.Read())
{
// here you can decide how you are going to work with the data you
lstRoomList.Add(datareader["roomnubmer"] + " " + datareader[CreditCarInfo];
}
at the end you could assign the values of that list to a dynamic array
arrayRoom = kstRoomList.ToArray(); I would really need to see what you are using and how you are returning the StoredProc results..