Access All rows of Grid View - asp.net

I have a grid view with page size of 6. Now in a button click event I need to access all the available rows of grid view as all the control are editable so I need to scan all the element.
However the statement
For Each row As GridViewRow In UserNoteGrid.Rows
run only for the current page not for all the available rows in every page.
How can I access that.
Thanks

please check ...
'after binding
GridView1.AllowPaging = False
For Each row As GridViewRow In GridView1.Rows
'some work
Next
GridView1.AllowPaging = True

you can use somethings like this:
Protected Sub UserNoteGrid_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles UserNoteGrid.RowDataBound
e.Row.Cells(1).Text = "test ..."
End Sub

Related

What is the proper way to use data from a grid view before data binding

I have a user control that creates a grid view of database records. Each record has a checkbox for deleting the record.
My problem is that the the page_load event builds the grid view, then the delete button even fires. The deleteButton_click event is looping over the gridview looking for checked boxes but never finds any because the 'page_load' event just gave me a clean gridview. What is the best way to check for checked boxes before the grid view is re-built? Or can I get the checked values without looking at the grid view?
Protected Sub Page_Load(...) Handles Me.Load
'db calls and other code
gv.DataBind()
End Sub
Protected Sub btnDelAtt_Click(...) Handles btnDelAtt.Click
For Each grdRow As GridViewRow In gvFileViewer.Row
Dim chkBox As CheckBox = CType(grdRow.FindControl("cbItem"), CheckBox)
If chkBox.Checked = True Then 'this is always false thanks to page_load
'code that does not run
end if
next
end sub
As mentioned in the comments, adding !IsPostBack should do it.
You only need to load the grid from the database in the initial call, you don't need to get the data again when post back occurs. You will need to rebind the grid once the delete is over.
Protected Sub Page_Load(...) Handles Me.Load
If(!Page.IsPostBack)
'db calls and other code
gv.DataBind()
End Sub
Protected Sub btnDelAtt_Click(...) Handles btnDelAtt.Click
For Each grdRow As GridViewRow In gvFileViewer.Row
Dim chkBox As CheckBox = CType(grdRow.FindControl("cbItem"), CheckBox)
//Delete your record
end if
next
//Rebind grid
end sub

Dynamically show/hide columns in Gridview in ASP.NET

I'm attempting to show/hide columns in gridview for better viewing.
What I need to do is:
hide columns 8, 9 and 10 on page load
show them upon button click
I successfully hidden them on pageload using RowCreated Event (code below). but as of now, I haven't yet found a way to show them again via button click.
Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
Dim row As GridViewRow = e.Row
' Intitialize TableCell list
Dim columns As New List(Of TableCell)()
For Each column As DataControlField In GridView1.Columns
'Get the first Cell /Column
Dim cell As TableCell = row.Cells(0)
' Then Remove it after
row.Cells.Remove(cell)
'And Add it to the List Collections
columns.Add(cell)
Next
' Add cells
row.Cells.AddRange(columns.ToArray())
e.Row.Cells(8).Visible = False
e.Row.Cells(9).Visible = False
e.Row.Cells(10).Visible = False
e.Row.Cells(11).Visible = False
End Sub
I tried the following methods with unfortunate results:
Set width to 0px then reset to auto upon click- since most of my columns are itemfields that contains either button or checkbox, this doesn't work at all
Use GridView1.Columns(8).Visible = False - same reason as above
Create RowDataBound Event with e.Row.Cells(8).Visible = True but i can't successfully call this event via button click yet.
Please advise. Thanks in advance.
DataGridView1.Columns(n).Visible = False ' working in vb.net
where n represent the column index

Toggle column display in ASP.NET gridview

I have two email fields, one a text field (index 15), the other a mailto: hyperlink (index 16), both in a gridview. (And yes, I know identifying via index isn't the best way to go -- just trying to make it work at this point).
When not editing, I need to show only the hyperlink field (making it available for the user to click on). When editing, I need to show only the text field, so they can modify the value.
I've got everything working as needed except that both fields display when the grid is initially shown. If I try to hide the text field in any of the normal ways (hiding cells on RowDataBound or hiding the column upon declaration), then it doesn't show up when editing.
Here's what I'm doing so far. The RowEditing event has the following code:
GridView1.Columns(16).Visible = False
GridView1.Columns(15).Visible = True
The RowCancelingEdit event has the opposite logic, toggling visibility on both fields. And finally the RowUpdating event has the following, which turns the hyperlink display back on:
GridView1.Columns(16).Visible = True
I'm relatively new to ASP.NET, so I definitely don't know all of the constructs available.
How can I hide the text field upon normal grid display, but still have the field available to show when in edit mode?
Try to RowCommand Event and set Edit button CommandName="name"
If e.CommandName = "name" Then
Dim row As GridViewRow = DirectCast(DirectCast(e.CommandSource, LinkButton).NamingContainer, GridViewRow)
Dim lblwwwhid = CType(row.FindControl("txtwwwhid"), Label)
lblwwwhid .visible =false
End if
It dawned on me that I could simply show/hide columns upon the initial databind (which works), as such:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
DataBind()
GridView1.Columns(16).Visible = True
GridView1.Columns(15).Visible = False
End If
End Sub

asp.net GridView IF empty show message

I've got a grid view. I want it to say "you have nothing to show" if there are no details.
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If GridView1.Rows.Count = 0 Then
Lblemptygridview.Text = "you do no details to show"
Elseif e.Row.RowType = DataControlRowType.DataRow then
Dim datakey As String = GridView1.DataKeys(e.Row.RowIndex).Value.ToString()
End If
End Sub
However; it seems to be working backwards and showing the message when there is data to display in the gird view and continues to be a blank page when there is not data to display in the grid view.
I've tried a variety of combinations with the below IF statement below but no success.
Instead, use the EmptyDataTemplate:
<emptydatatemplate>
No Data Found.
</emptydatatemplate>
This is more of an addendum to Icarus's answer, adding a bit of context as to why your solution does not work. (For educational purposes).
RowDataBound is called when a Row is bound to the gridview. This basically means that this is called for every row in the grid view.
Now, the reason why your solution doesn't work, is that if your GridView simply has nothing in it, RowDataBound will not be called.
The reason why you're getting 'No Data Found' when you DO have data, is because the first time the if statement runs when loading a GridView, the GridView (at the time of execution) has no Rows, which results in your if statement being true.
Just something to keep in mind.
Just a suggestion..
Instead of showing "you have nothing to show" in a gridview which looks outdated, why don't you make it fancy??
You can do
Dim dt As DataTable = getDatatable()
If Not dt Is Nothing AndAlso dt.Rows.Count > 0 Then
GridView1.datasource = dt
GridView1.databind()
div.style.add("display", "none")
Else
GridView1.visible = False
'Add some fancy style here to show no record
div.style.add("display", "block")
End If
thanks

Reset values inside a repeater

I have a repeater which displays products. Users can select a Size - dropdownlist and an Amount - textbox. Then they press Order or Cancel. When they press Cancel I would like the values of Size and Amount to return to their default values.
Protected Sub lbtnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtnCancel.Click
rpt.ProductList is the repeater name
lblFeedback.Text = ("")
lblFeedback.ForeColor = Drawing.Color.Black
End Sub
Any help is welcome!
I am using VB.NET.
What if you rebind your repeater on the Cancel Click, so it will again be populated with your Default Values ?
Use the rptProductList_ItemCommand event to catch the repeateritem line and play with the controls as you please ...

Resources