Gridview daily updating - asp.net

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

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

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

Add total in VB.Net Gridview

I can not manage to get the gridview display the total in a footer
I've tried the following:
<asp:GridView ID="GV" runat="server"
DataSourceID="SqlQuery" EmptyDataText="No data">
<Columns>
<asp:BoundField DataField="Weekday" FooterText=" " HeaderText="Weekday" />
<asp:BoundField DataField="Volume" DataFormatString="{0:N0}"
FooterText="." HeaderText="Volume" />
</Columns>
</asp:GridView>
Protected Sub GV_rowdatabound(sender As Object, e As GridViewRowEventArgs) Handles GV.RowDataBound
Dim Volume as integer = 0
For Each r As GridViewRow In GV.Rows
If r.RowType = DataControlRowType.DataRow Then
Volume = Volume + CDec(r.Cells(1).Text)
End If
Next
GV.FooterRow.Cells(1).Text = Math.Round(Volume , 0)
End Sub
This gives me an error messages:
Object reference not set to an instance of an object
I followed advice in the following page and I changed the code:
trying to total gridview in asp
Sub GV_WeekSumary_rowcreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
Dim Volume as integer = 0
For Each r As GridViewRow In GV.Rows
If r.RowType = DataControlRowType.DataRow Then
Volume = Volume + CDec(r.Cells(1).Text)
End If
Next
If e.Row.RowType = DataControlRowType.Footer Then
e.Row.Cells(1).Text = Math.Round(Volume , 0)
End If
End Sub
This does not give an error, but the footer does not show any value.
I've tried also the following:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim Volume as integer = 0
For Each r As GridViewRow In GV.Rows
If r.RowType = DataControlRowType.DataRow Then
Volume = Volume + CDec(r.Cells(1).Text)
End If
Next
GV.FooterRow.Cells(1).Text = Math.Round(Volume, 0)
GV.DataBind()
End Sub
Still no value in the footer, but when I debbug it I can see that footer is assisgned the value I need. Why it is not displayed in the website?
Any idea how I can get this to work?
You must use DataBound event.
Try this:
Protected Sub GV_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles GV.DataBound
Dim Volume As Decimal = 0
For Each r As GridViewRow In GV.Rows
If r.RowType = DataControlRowType.DataRow Then
Volume += Convert.ToDecimal(r.Cells(1).Text)
End If
Next
GV.FooterRow.Cells(1).Text = Math.Round(Volume, 0).ToString()
End Sub

GridView_RowDataBound (If e.Row.RowType = DataControlRowType.DataRow)....include 2 IF's?

I've got a grid view which uses a "If e.Row.RowType = DataControlRowType.DataRow" to calculate the total of a column and hold this in the footer.
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Totalnumbers += Convert.ToInt16(DataBinder.Eval(e.Row.DataItem, "RequestTotalnumbers"))
ElseIf
e.Row.RowType = DataControlRowType.Footer Then
e.Row.Cells(3).Text = String.Format("{0}", Totalnumbers)
End If
However i now wish to also add....
If e.Row.RowType = DataControlRowType.DataRow Then
Dim datakey As String = GridView1.DataKeys(e.Row.RowIndex).Value.ToString()
End If
so that it can transfer on click to another page....
'Handle button click
Protected Sub RowClick(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) _
Handles GridView1.RowCommand
If e.CommandName = "Select" Then
'Add to session variable; translate the index of clicked to Primary Key
Session.Add("ID", GridView1.DataKeys(e.CommandArgument).Value.ToString)
Response.Redirect(" ")
End If
End Sub
I've tried combining to two if's together but have had no success...how can i do this?
Not quite sure, this may be what you want. Make sure you also added "datakeynames" property in your GridView.
'Handle button click
Protected Sub RowClick(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) _
Handles GridView1.RowCommand
If e.CommandName = "Select" Then
Dim row As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow)
'Add to session variable; translate the index of clicked to Primary Key
Session.Add("ID", GridView1.DataKeys(row.RowIndex).Value.ToString)
Response.Redirect(" ")
End If
End Sub

ASP.net - Set focus to editing row

How do you set focus to a specific control in a datagridview row after clicking the edit button? I'm able to do it for a new row when the grid is binding, but not for an existing row. The control doesn't seem to exist yet.
'This doesn't work (existing row)
Protected Sub gvDays_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs) Handles gvDays.RowEditing
Try
gvDays.EditIndex = e.NewEditIndex
gvDays.Rows(e.NewEditIndex).FindControl("txtDayText").Focus()
Catch ex As Exception
Helper.WriteException(ex)
End Try
End Sub
'This does work for a newly bound row
Private Sub gvDays_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvDays.RowDataBound
If e.Row.RowState = DataControlRowState.Edit Then
e.Row.Cells(3).Controls(0).Focus()
End If
End Sub
gvDays_RowDataBound should work, the problem is you are looking at e.Row.RowState using the = operator, but RowState is a bitflag
try this
Private Sub gvDays_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvDays.RowDataBound
If (e.Row.RowState And DataControlRowState.Edit) = DataControlRowState.Edit Then
e.Row.Cells(3).Controls(0).Focus()
End If
End Sub

Resources