asp.net vb gridview coulours based on previous row - asp.net

I have a GridView displaying results from sql server, all values are supposed to follow on the previous value e.g. 3373 must be before 3372. I need to colour the row on the gridview as soon as the value does not follow the previous value. Sometimes the values are missing so I need to identify if a value is missing or not.

You could use the OnRowDataBound event to store the last value and compare. Something like this:
Private _lastRowValue As Integer = -1
Protected Sub OnGridViewRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
If _lastRowValue <> -1 Then
' read current row value, compare and then format, e.g. like this
e.Row.Cells(1).Text = "<i>" + e.Row.Cells(1).Text + "</i>"
End If
_lastRowValue = ... ' read value from cell
End If
End Sub

Related

VB.net For Each Loop Not Accounting for First Datarow

I am currently looping through a data gridview in Visual Studio, The Loop works well and the code does pretty much what I want,
My only problem is that it seems to skip, or not account for the first row in my gridview, I have looked online to resolve this but cannot find anything,
I was wondering if i could get some assistance with this...
Here is my code for the loop:
'Checks the flags for montor/expiry date to set red or green cells
'------------------------------------------------------------------
For Each Row As GridViewRow In GridView3.Rows
'-----------------------------------------------------------------------------------------
Dim MonitorVal As String = GridView3.DataKeys(e.Row.RowIndex).Values("Monitor").ToString
If MonitorVal.Contains("Yes") Then
Dim IsExtention As String = GridView3.DataKeys(e.Row.RowIndex).Values("FileName").ToString
If IsExtention = "" Or IsExtention = Nothing Or IsExtention = "Null" Then
'set red
'--------
e.Row.BackColor = Drawing.Color.LightPink
Else
'else green
'-----------
e.Row.BackColor = Drawing.Color.LightGreen
End If
Else
'else green
'-----------
e.Row.BackColor = Drawing.Color.LightGreen
End If
Next
I guess that "first row in gridview" actually means the header-row of the grid. That is not returned from GridView.Rows, only rows with DataControlRowType.DataRow are returned. You can get it only in events like RowDataBound or RowCreated. I would suggest to use RowDataBound in this case:
Protected Sub GridView3_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView3.RowDataBound
Select Case e.Row.RowType
Case DataControlRowType.Header
' ... '
Case DataControlRowType.DataRow
' ... '
Case DataControlRowType.Footer
' ... '
End Select
End Sub
Documentation:
Only rows with their RowType property set to
DataControlRowType.DataRow are stored in the Rows collection. The
GridViewRow objects that represent the header, footer, and pager rows
are not included in the collection.

Datagrid row not being read code

I have the following code...
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As EventArgs) Handles GridView1.RowUpdating
Dim SocioNumInfo As Integer = CInt(GridView1.Rows(GridView1.SelectedIndex).Cells(4).Text)
MsgBox(SocioNumInfo.ToString)
...
End Sub
Now, this code should read the cell, but it gives me the following error:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
The MsgBox is just there for me to check if the data is being read, at the end of the day it should turn that into a parameter so I can add to the DB... but still.. nope nothing. Is that the correct code to read directly from a cell in a choosen row? In the "Protected Sub" area I already tried SelectedIndexChanging and RowEditing, etc... and still nothing. Still throws the error at me.
If I try
Dim SocioNumInfo As String = CStr(GridView1.Rows(GridView1.SelectedRow).Cells(4).Text) it gives me a "cannot be converted to Integer" error.
If no row is selected then GridView1.SelectedIndex is -1. You might have to add a check
If GridView1.SelectedIndex <> -1 Then
' Use GridView1.SelectedIndex here
End If
However, it is easier to access the selected row like this:
Dim row = GridView1.SelectedRow
If Not row Is Nothing AndAlso IsNumeric(row.Cells(2).Text) Then
Dim SocioNumInfo As Integer = CInt(row.Cells(2).Text)
....
End If
Note that the cell index is zero based. row.Cells(4) is in the 5th column.

convert unbound Gridview column values to lowercase at runtime

I have an unbound Gridview that is populated by a Linq to Entities query and would like to convert string values in a particular column to lowercase.
In the Gridview's RowDataBound event, i have tried StrConv(e.Row.Cells(3).Text, VbStrConv.ProperCase) but this doesn't work.
I have also tried StrConv(emp.Name, VbStrConv.ProperCase) in the LiNQ to Entities query but still the Name values returned are to converted to Lower-case.
Protected Sub GridView3_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView3.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
For i As Integer = 0 To e.Row.Cells.Count - 1
Dim cellDate As Date
If Date.TryParse(e.Row.Cells(i).Text, cellDate) Then
e.Row.Cells(i).Text = String.Format("{0:d}", cellDate)
End If
Next
End If
StrConv(e.Row.Cells(4).Text, VbStrConv.ProperCase)
End Sub
As far as I can see, strConv returns a string, which should be used, I think like:
e.Row.Cells(4).Text = StrConv(e.Row.Cells(4).Text, VbStrConv.ProperCase)
have you tried to do this:
string strLower = e.Row.Cells[0].Text.ToLower();
and then use the strLower as the lower case string.

loop through gridview and get data key value

I am trying to loop through the rows of my gridview and retrieve the data key value of each row and then execute some code to run sql queries. How can I get the data key value of each row in variable? right now I am receiving an error message saying:
value of type system.web.ui.webcontrols.datakey cannot be converted to integer.
Here is my code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
For Each row As GridViewRow In GridView1.Rows
Dim therowindex As Integer = row.RowIndex
Dim theid As Integer = GridView1.DataKeys([therowindex])
'execute some more code running queries using the data key value.
Next
End Sub
You can iterate through the .Rows collection, and then find the row's DataKey value.
foreach(GridViewRow row in GridView1.Rows)
{
string theDataKey = GridView1.DataKeys[row.RowIndex].Value.ToString();
// Do something with your index/key value
}
You have to use Value property.
Dim theid As Integer = Integer.Parse(GridView1.DataKeys(therowindex).Value.ToString())

Change gridview row forecolor based on column field value

I have a value in one of my gridview column which will determine if the entire row's value should be in red color. I did the below but somehow every single row is red colored. Please kindly advice. Thanks.
Protected Sub uigvList_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles uigvList.RowCreated
If e.Row.RowType = DataControlRowType.DataRow Then
If Not (DataBinder.Eval(e.Row.DataItem, "VoidOn")) Is Nothing Then ' This is the condition, if this field is not blank then I want to the entire row to have red colored values.
e.Row.BackColor = Drawing.Color.Firebrick
End If
End If
End Sub
Maybe DBNull.Value?
If (DataBinder.Eval(e.Row.DataItem, "VoidOn")) IsNot DBNull.Value Then ...
If e.Row.DataItem("VoidOn") IsNot DBNull.Value Then ...
PS: You can check VoidOn value in the debug mode, just set breakpoint to the code row.

Resources