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
Related
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
I have a GridView that shows particular data list or reports when clicking Generate button.
GridView Records
When I'm moving to the next page of the GridView or when I click the next page, the GridView binding becomes empty and it shows no GridView list.
Page Index Changed
Here's my code in .aspx
<asp:GridView ID="GridView1"
runat="server"
CellPadding="4"
HeaderStyle-ForeColor="White"
ForeColor="#333333"
GridLines="None"
CssClass="mGrid"
PagerStyle-CssClass="pgr"
AlternatingRowStyle-CssClass="alt"
RowStyle-CssClass="row"
AllowSorting="True"
AllowPaging="True"
PageSize="25" Visible="false" OnRowDataBound="GridView1_RowDataBound"
EnableViewState="false">
<EmptyDataTemplate>
No record found.
</EmptyDataTemplate>
</asp:GridView>
Here's my code for Page_Load
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
userid = IIf(SessionHandler.UserID = "", User.Identity.Name.ToUpper, SessionHandler.UserID)
V3Substance.Attributes.Add("onfocus", "javascript:if (this.value=='Substance Cas Number') { this.value = ''; }")
V3Substance.Attributes.Add("onblur", "javascript:if (this.value=='') { this.value = 'Substance Cas Number'; }")
If Not Page.IsPostBack Then
V1Division.DataSource = GetDivision()
V1Division.DataBind()
V1BR.Items.Clear()
V1BR.DataSource = GetBusinessRule(V1Division.Text)
V1BR.DataBind()
MultiView1.ActiveViewIndex = 0
Panel1.DefaultButton = "V1Generate"
End If
End Sub
Here's my code for GridView1_PageIndexChanging
Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
Dim dt As DataTable = TryCast(GridView1.DataSource, DataTable)
GridView1.DataSource = SortTable(dt)
GridView1.PageIndex = e.NewPageIndex
GridView1.DataBind()
End Sub
And here's my code for BindGridView
Private Sub BindGridview(ByVal query As String, ByVal countquery As String)
ViewState.Add("Query", query)
ViewState.Add("CountQuery", countquery)
Dim count As Integer = 0
If Description.Text = "1" Then
ValidateFamily(query)
Else
DataSource = dbHelper.GetRecordDT(query)
End If
count = DataSource.Rows.Count
'GridView1.DataSource = Nothing
'GridView1.DataBind()
'GridView1.PageIndex = 0
GridView1.DataSource = DataSource
GridView1.Visible = True
GridView1.DataBind()
ExportToExcel.Visible = IIf(count < 1, False, True)
Panel1.Visible = IIf(count < 1, False, True)
SetTotalResult(count)
End Sub
I hope you can help me with this. I can't seem to know what could be the problem. I tried different approach to this and find a solutions from the internet but it didn't help. Thanks a lot.
You should be using
GridView1.DataSource = DataSource
It's unclear from your snippet what DataSource is, but you use it in the BindGridview method also.
Start using different names for variables, naming them exactly as properties is very confusing and will lead to errors.
Update
Dim source As SqlDataSource
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
source = New SqlDataSource
source.ID = "mySource"
source.SelectCommand = "select * from table"
source.ConnectionString = connStr
If Not Page.IsPostBack Then
GridView1.DataSource = source
GridView1.DataBind
End If
End Sub
Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
GridView1.DataSource = source
GridView1.PageIndex = e.NewPageIndex
GridView1.DataBind
End Sub
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
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
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