how to highlight the biggest value in a Gridview - asp.net

I have a gridview that is binded.
And i want to change the color of the font for the longest leadtime even if there are duplicates. I have no idea on how to write my if statement.
This is a rough idea of what i want to do, though I know this code is wrong.
if Max(LeadTime) Then
GridView.ForeColor = Color.Red
Can anyone help me?

You first need to get the max value from your datasource. You can do this with linq:
maxLeadTime = ds.Max(dsi => dsi.LeadTime)
In your item data bound event handler, compare the bound item with the max value:
if (item.LeadTime == maxLeadTime)
{
/* do stuff */
}

(VB.NET version) Assuming you are binding you Grid to a datatable, this is how you will do it.
Private maxVal As Decimal
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Me.Page.IsPostBack Then
dim dt as datatable = GetTable()
maxVal = ds.AsEnumerable.Max(Function(dr) dr("lead_time"))
gv.DataSource = dt
gv.DataBind()
End If
End Sub
Private Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim dr As DataRowView = e.Row.DataItem
If dr("lead_time") = maxVal Then
e.Row.BackColor = Drawing.Color.Red
End If
End If
End Sub
it would be the same if you are binding it to a list(of T)
In PAge Load:
maxVal = urList.Max(Function(x) x.LeadTime)
In Row dataBound:
Dim uc As urClass = e.Row.DataItem
If uc.LeadTime = maxVal Then
e.Row.BackColor = Drawing.Color.Red
End If

Related

Delete selected gridview row

trying to delete selected gridview row. I read few sites and used code however I am getting error. I think because most of the codes I've seen they using DataGridView, This code is within delete button event
For i = 0 To myGridView.Rows.Count - 1 Step i + 1
Dim delrow As GridViewRow = myGridView.Rows(i)
If delrow.Selected = True Then
myGridView.Rows.RemoveAt(i)
End If
Next
the two errors I am getting are:
1) RemoveAt is not a member of GridViewRowCollection
2) Selected is not a member of GridViewRow
Your help will be highly appreciated!
I usually delete rows on GridViews putting an ImageButton (with a trash bin) on every row with a Template Field, then on RowDataBound event I associate "DeleteRow" event and rowIndex to the CommandName and CommandArgument
Protected Sub myGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim ib As ImageButton = e.Row.FindControl("ibDeleteRow")
ib.CommandName = "DeleteRow"
ib.CommandArgument = e.Row.RowIndex
End if
end sub
Private Sub myGridView_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName = "DeleteRow" Then
Dim rowToDeleteIndex as integer = e.CommandArgument
myGridView.DeleteRow(rowToDeleteIndex)
end if
end sub
Private Sub myGridView_RowDeleting(sender As Object, e As GridViewDeleteEventArgs) Handles GridView1.RowDeleting
End Sub

ASP.NET VB Color single Row in Gridview

I want to color single Rows when there empty in a Gridview, but it changes the color from every Row empty and not.
Private Sub _Default_Load(sender As Object, e As EventArgs) Handles Me.Load
... SQL Connection Stuff
Using dt As New DataTable()
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
Dim Menge As Integer = GridView1.Rows.Count - 1
For i = 0 To Menge
For Each row As GridViewRow In GridView1.Rows
If GridView1.Rows(i).Cells(4).Text = "" Then
GridView1.Rows(i).Cells(4).BackColor = System.Drawing.Color.Red
End If
Next
Next
End Sub
Can someone Help me?
You should use the following code in your Default_Load event, but this will only color rows when page loads. Ideally, you should hook up the RowDataBound event with your grid view.
Code if using Default_Load event
Private Sub _Default_Load(sender As Object, e As EventArgs) Handles Me.Load
... SQL Connection Stuff
Using dt As New DataTable()
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
'color rows based on some condition
For Each row As GridViewRow In GridView1.Rows
If row.Cells(4).Text = "" Then
row.Cells(4).BackColor = System.Drawing.Color.Red
End If
Next
End Sub
Code if using RowDataBound event
Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
If e.Row.Cells(4).Text = "" Then
e.Row.Cells(4).BackColor = System.Drawing.Color.Red
End If
End If
End Sub

how to get value of dynamically added textbox in gridview. getting an error that Reference object has value of nothing

I have added Textbox like
Protected Sub gvReconciliation_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvReconciliation.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim txtRemarks As New TextBox()
txtRemarks.ID = "txtRemarks"
txtRemarks.Text = TryCast(e.Row.DataItem, DataRowView).Row("Remarks").ToString()
e.Row.Cells(10).Controls.Add(txtRemarks)
End If
End Sub

Gridview daily updating

I currently have a Gridview in ASP.NET VB where you can select a row and the data will then be transported to the next page for entry purposes. My question is there an easy way where I can highlight the row after someone has entered information on this row and for the same day. So if someone enters data for a person today, then tomorrow the highlighting will go away the next calendar day. I appreciate any help.
This is what i currently have.
Public Sub Get_GV()
' Fills Gridview with data selected from dropdown-area
DSADT.SelectParameters("Area").DefaultValue = DDArea.SelectedValue
Dim dv As DataView = DSconnect.Select(DataSourceSelectArguments.Empty)
GridView1.DataSource = dv
GridView1.DataBind()
End Sub
Private Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
' Allows you to "select"/Highlight a row without a select button
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes("onmouseover") = "this.style.cursor='pointer';this.style.backgroundColor = '#87CEFF';"
e.Row.Attributes("onmouseout") = "this.style.textDecoration='none';this.style.backgroundColor = '#FFFFFF';"
e.Row.Attributes("OnClick") = Me.Page.ClientScript.GetPostBackClientHyperlink(Me.GridView1, "Select$" & e.Row.RowIndex)
End If
End Sub
Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
Dim sb As New System.Text.StringBuilder()
'Submits data to next page
Response.Redirect("RoundingEntry.aspx?Room=" & GridView1.SelectedRow.Cells(1).Text & "&Name=" & GridView1.SelectedRow.Cells(2).Text & "&Rounder=" & DDRounder.SelectedValue & "&Area=" & DDArea.SelectedValue)
End Sub
You need to include the last modified date in the data returned from your query to bind the grid view and then you can handle the RowDataBound event to check the last modified date for each row against today's date, like this:
Code-behind:
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
' Only check the last modified date for data rows, ignore header and footer rows
If e.Row.RowType = DataControlRowType.DataRow Then
' Get the last modified date for each row, possibly in a hidden field control and compare it to today's date
End If
End Sub
Markup:
<asp:gridview id="GridView1"
onrowdatabound="GridView1_RowDataBound"
runat="server">
</asp:gridview>
onrowdatabound="CustomersGridView_RowDataBound"
You can use something like this:
Private Sub FormatMyGridView _
(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) _
Handles GridView1.RowDataBound
Dim drItems As DataRow
If e.Row.RowType = DataControlRowType.DataRow Then
drRequest =DirectCast(e.Row.DataItem, System.Data.DataRowView).Row
' get your field from DataItem
' format the row based on field value:
if drItems.YourFieldDateTime.Date = DateTime.Date
' hilight the row
e.Row.BackColor = System.Drawing.Color.Red;
End If
End Sub

setting radiobuttonlist value within a gridview using code behind

I am trying to set the selected value of a radiobuttonlist that is a template column within a gridview. I am having to do this using the code behind as the database field containing the data (Status) contains null values hence I cant used SelectedValue='<%# Bind("Status") %>' on the asp side.
It has been suggested that I use onRowDataBound to do this and use DataItem to retrieve the value from the datasource and used it to set the radiobuttonlist selected value but I'm not sure how to do this in vb code.
I've tried the following:
Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim radioList = TryCast(e.Row.FindControl("rblActions"), RadioButtonList)
' this will be the object that you are binding to the grid
Dim myObject = TryCast(e.Row.DataItem, DataRowView)
Dim sStatus As String = Convert.ToString(myObject("Status"))
If sStatus <> Nothing Then
radioList.SelectedValue = sStatus
End If
End If
End Sub
but it hasnt work. Any help would be appreciated. Thanks
Found my own solution as follows:
Sub gv_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim rbl As RadioButtonList = CType(e.Row.FindControl("rblActions"), RadioButtonList)
Dim selected As String = e.Row.DataItem("Status").ToString
If Not IsNothing(selected) Then
rbl.SelectedValue = selected
End If
End If
End Sub

Resources