How to add a div after every 5 rows of data? - asp.net

I'm trying to add div after a group of 5 rows of data inside Listview.
I came up with using Mod 5 = 0 but I realized the last row is missing if total row count can't divided by 5.
For example, I have 11 rows of data.
"Div" will be added after 5 and 10.
I also need to add "Div" after 11 as well. (div will display details of each group)
So, something like this
1|2|3|4|5
DIV1
6|7|8|9|10
DIV2
11|
Div3(missing)
Here is inline code I have so far
<%# IIf((Container.DisplayIndex + 6) Mod 5 = 0, "<div id='temp" + Math.Floor((Container.DisplayIndex + 6) / 5).ToString + "' style='display:none'></div>", "")%>
How do I add the last div when total # can't be divided by 5?
I have to get the total# of display index somehow...

You can compare it with the ListView.Items.Count property.
This should work:
Dim addDiv = (Container.DisplayIndex Mod 5 = 0) OrElse _
(Container.DisplayIndex + 1 = ListView1.Items.Count)
If you are using paging you should use DataItemIndex instead of DisplayIndex.
Edit: (according to your last comment)
If you need to show the total record count you have to cast the ListView's DataSource to it's correct type(f.e. a DataTable).
Dim tbl as DataTable = DirectCast(ListView1.DataSource, DataTable)
Dim totalCount as Int32 = tbl.Rows.Count
or in one line:
DirectCast(ListView1.DataSource, DataTable).Rows.Count
But this works only on databinding and not on every postback because the DataSource will be disposed at the end of the Page-Lifecycle.

the "+6" makes it look like you're guessing around how the modulo function works :)
try this:
<%# IIf(Container.DisplayIndex Mod 5 = 4 Or Container.DisplayIndex = ListView.Items.Count , "<div id='temp" + (1+Math.Floor(Container.DisplayIndex/5)).ToString + "' style='display:none'></div>", "")%>
so first (index+6)%5 == 0 is the same as index%5 == 4,
second floor((index+6)/5) results in 2 when index=4, this is not what you want.
using 1+floor(index/5) will give you the correct results --- you could even drop the "1+" if you want the index to start from 0.
(p.s. i don't know asp.net, sorry if there's compiler errors)

Related

how to loop thru existing labels in vb.net

I have hundred of labels in a web form, just like: Wall_1, Wall_2, Wall_3...
I am using VB.Net. How to loop through all the labels names by just
adding the index number to the end of the "Wall_ " ?
For i As Integer = 1 To 20
"Wall_ " + i.ToString().Text = "142.5"
Next
I am using Visual Studio 2012.
To loop through all the labels, I recommend using the method FindControl.
Add them to your collection/list et voilá!
For i as Integer to 20
labelArray.Add(Form.FindControl("Wall_" & i.ToString()))
Next
In order to change their texts...
For i as Integer to 20
Form.FindControl("Wall_" & i.ToString()).Text = "142.5"
Next
EDIT:
As stated in the comments, the ".Text" attribute can't be applied to a generic control, so first it needs casting to a label:
For i as Integer to 20
Dim label as Label = CType(Form.FindControl("Wall_" & i.ToString()), Label)
label.Text = "142.5"
Next

Peoplesoft rowset

I'm new to peoplesoft. I need a help in understanding the rowset and I have a requirement where i have 3 levels.
On level 1 i have a checkbox and when I open a component the value of the checkbox on level 1 should be passed and display to the level 2 grid for all rows.
For example
level0 - record1
level1 - record2 (Scroll Area)
level2 - record3 (grid)
When i access the page it should have values like this
Record2.field1 = Y => Row1 Record3.field1 = Y
Row2 Record3.field1 = Y
Record2.field1 = N => Row1 Record3.field1 = N
I have written the code at level2 record.field rowinit peoplecode event. but the problem is the same record field is used in level 0 as well. Is there a way where I can avoid using for loop as there could be n number of rows in the grid which might create a performance issue during page opening.
Thanks in advance,
Rowinit will fire for each rows in the scroll. So if you have a loop in the rowinit, loop will execute for each row.
If you want the check box to be set only during the component load, you can add the peoplecode in Component PostBuild.
&rsLevel1 = GetLevel0()(1).GetRowSet(Scroll.Level1);
for &nCnt1 = 1 to &rsLevel1.activerowcount
&rsLevel2 = &rsLevel1(&nCnt1).GetRowset(Scroll.Level2);
for &nCnt2 = 1 to &rsLevel2.activerowcount
&rsLevel2(&nCnt2).Level2.Check_box.value = &rsLevel1(&nCnt1).Level1.Check_box.value
end-for;
end-for;

Multiple Where Parameters on EntityDataSource?

I have a gridview that uses an entity datasource to populate itself. Depending on what the user has access to see, I want the gridview to implement a where clause. At the lowest level of access, the user can only see themselves. In order to do this I implement the line of code:
EmployeeEntityDataSource.Where = "it.Person_ID = " + selectQuery.ToString()
This successfully reduces the data in the gridview to the one appropriate user. If the user has the next step in access, they should be able to see themselves plus all the employees that work for them. I have sucessfully created a list of employees Person_IDs and I'm trying to filter my gridview so that if the Person_ID column in the gridview matches one of the Person_IDs in my list it should show up.
I have tried the following bits of code:
1.
For Each employeeID In employeeList
If count2 <> count Then
whereString += "it.Person_ID = " + employeeID.ToString() + " OR "
count2 += 1
Else
whereString += "it.Person_ID = " + employeeID.ToString()
End If
Next
EmployeeEntityDataSource.Where = whereString
Essentially thought I could create a giant where statement with a bunch of ORs but this did not work
2.
EmployeeEntityDataSource.WhereParameters.Add(employeeList)
EmployeeEntityDataSource.Where = "it.Person_ID = #employeeList"
The error I get here says a List(of T) cannot be converted WebControl.Parameter
How do I properly create a WHERE statement that will compare the it.Person_ID of the gridview to each element in my list called employeeList?
I think an In statement should accomplish what you need. Something like this -
string employeeIDs = string.Join(",", employeeList.ToList());
EmployeeEntityDataSource.Where = String.Format("it.Person_ID IN ({0})", employeeIDs);
Or you may have to iterate through your list to create the employeeIDs string, depending on the types we're dealing with here.
Apparently I lied When I said my first bit of code did not work. The issue was that I was not generating the massive OR statement correctly.
From what I remember the statement I was originally generating was it.Column = ID1 OR ID2 OR ID3 and so on.
If the statement created is it.Column = ID1 OR it.Column = ID2 OR it.Column = ID3 and so on, this creates a statement that works properly.
The code that is working in my current project is:
For Each employee In employeeList
If count2 <> count Then
whereString += "it.Person_ID = " + employee.ToString() + " OR "
count2 += 1
Else
whereString += "it.Person_ID = " + employee.ToString()
End If
Next
EmployeeEntityDataSource.Where = whereString

Change Gridview row backcolor if two cells have a certain value/color

I have a gridview and I want to change the color on page load of multiple rows if one of the header cells has a backcolor of red and if one of the cells in that column has a tick in it (the tick is a html image).
the code I have so far is
Dim y As Integer
Dim m As Integer
y = 3
For m = 0 To availableRuns.Rows.Count - 1
If availableRuns.HeaderRow.Cells(y).BackColor = ColorTranslator.FromHtml("#E95B4D") Then
If row.Cells(y).Text = "<img src='images/green_tick.png' />" Then
availableRuns.Rows(m).BackColor = ColorTranslator.FromHtml("#E95B4D")
End If
End If
Next
But this changes all the rows to red. Looking at the image below I need to change all the rows to red for GT2 except 2 and 8.
Can anyone help? Using MS VS 2013, VB.NET
I have fixed it after thinking about it over the weekend. I changed my IF statement to the following.
For m = 0 To availableRuns.Rows.Count - 1
If availableRuns.HeaderRow.Cells(y).BackColor = ColorTranslator.FromHtml("#E95B4D") And availableRuns.Rows(m).Cells(y).Text = "<img src='images/green_tick.png' />" Then
availableRuns.Rows(m).BackColor = ColorTranslator.FromHtml("#E95B4D")
End If
Next

Datatable's Compute Function For Rows

As we know that, with compute function of datatable we can get sum of columns.
But I want to get sum of a row of datatable.
I will explain with a example:
I have a datatable like image below: With compute function we can get the sum of each column (product). Such as for product1, 2 + 12 + 50 + 13= 77.
I want to get sum of company1 : 2 + 6 + 4 + 3 + 5 = 20
http://img123.imageshack.us/img123/1517/61519307xx5.jpg
How can I do it with asp.net 1.1?
LINQ to the rescue:
DataTable dt = WhateverCreatesDataTable();
DataRow dr = dt.Rows[0];
int sum = dt.Columns.Cast<DataColumn>().Sum(dc=>(int)dr[dc]);
For those still dragging their knuckles in the stone ages (aka pre-.Net 3.5 and LINQ):
DataTable dt = WhateverCreatesDataTable();
DataRow dr = dt.Rows[0];
int sum = 0;
foreach(DataColumn dc in dt.Columns)
sum += (int)dr[dc];
From msdn:
If you must perform an operation on
two or more columns, you should create
a DataColumn, set its Expression
property to an appropriate expression,
and use an aggregate expression on the
resulting column. In that case, given
a DataColumn with the name "total",
and the Expression property set to
this:
"Quantity * UnitPrice"
Since you want to solve this in 1.1 here is what you can do as a simple solution
DataColumn totalColumn = new DataColumn();
totalColumn.DataType = System.Type.GetType("System.Int32");
totalColumn.ColumnName = "Total";
totalColumn.Expression = "Product1 + Product2 + Product3 + Product4 + Product5";
// Populate and get the DataTable dt then add this computed column to this table
dt.Columns.Add(totalColumn);
//Now if you access the column "Total" of the table you will get the desired result.
We can use Datatable's Compute() function for the following purpose:
To find sum.
To find max value.
To find min value.
To calculate average.
Below links have detailed explaination of Datatable's compute() function:
http://codevariation.blogspot.com/2017/02/using-datatable-compute-function-in-cnet.html
Hope this will help you.
Thanks.

Resources