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
Related
on textbox blank, i wanted to clear my gridview source
But i was not able to do it in vb.net.
After referring several answers i tried following unsuccessfull attempts:
grdUsers.rows.clear() : Does not work with vb.net
grdUsers.DataSource=""
grdUsers.columns.clear()
But it does not worked out.
Please help me to clear my datasource of gridview.
If your DataGridView is bound to a DataSource and you want to clear it then you can use the Nothing keyword followed by a DataBind().
grdUsers.DataSource = Nothing
grdUsers.DataBind()
Here is more information on the DataBind() method.
If you want to clear your rows when the text is empty in TextBox1, you would create a TextChanged event for your textbox ...
Private Sub TextBox1_TextChanged(sender As Object, e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text.Trim = "" Then
grdUsers.DataSource = Nothing
grdUsers.DataBind()
End If
End Sub
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
I have a gridview which has a List (Of MyObject) as its datasource.
gv.DataSource = MyListObject
gv.Databind()
Under a templatefield i have a button configured to delete a record from MyListObject and then rebind it. To add a record i have the below code
Protected Sub btnAddRecord_Click(sender As Object, e As EventArgs) Handles btnAddRecord.Click
Dim Customer As New Customer
With Customer
.Name = txtName.Text
.Surname = txtSurname.Text
.....
.ID += MyListObject.Count
End With
MyListObject.Add(Customer)
gv.DataSource = MyListObject
gv.DataBind()
End Sub
This works fine, but then i need to allow the user to delete a record if need be:
Private Sub gv_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv.RowCommand
If e.CommandName = "Delete" Then
MyListObject.RemoveAt(e.CommandArgument)
gv.DataSource = Nothing
gv.DataBind()
gv.DataSource = MyObjectList
gv.DataBind()
upnl.UpdateMode = UpdatePanelUpdateMode.Conditional
upnl.Update()
End If
End Sub
When i click the button it deletes the record but doesnt refresh the data. By that i mean when the record is added i am assigning the ID as a row ID and then use that ID to remove the record. Since the List and Gridview values are now out of sync i set the datasource to nothing and rebind it in order that i was going to have the values reset and the ID would be the correct one - but this doesnt works as i expected.
Could anyone advise where im going wrong and how to correct this problem?
Is gridview in the updatepanel? If yes, that panel should also be refreshed.
I added CommandArgument='<%# Container.DataItemIndex %>' which resolved the issue as i was then deleting the row the user clicked against.
Just as a side note, I'd probably handle what get's updated when on the client side using the ajax script manager rather than doing it in the code behind. Saves headaches. The above might be updating the update panel correctly, but the ajax plumbing may not be there on the client side.
i have got a datatable in witch i have got image url. i dont want to create a tamplet column. i just want to assign my datatable as datasource of gridview and it should show image in its field like:
dim dt as new datatable
dr = dt.NewRow
dr("HotelName") = "citypalace"
dr("image") ="<img src='" & "www.mycity.com/aa.jpg" & "'/>" 'but this is not working its showing url in that column...
dt.rows.add(dr)
gridview1.datasource = dt
gridview1.databind()
please give me solution that what should i do with that column of datatable so it will show image in gridview. (please dont say me to create a template column).
The GridView control automatically html-encodes everything, but there are a couple ways you can turn it off. If you have the image field declared in the aspx page, the easiest option is to just set the HtmlEncode property to false.
<asp:BoundField DataField="image" HtmlEncode="false" />
If this isn't feasible in your case, you can 'undo' the html encoding for these cells on the GridView's RowDataBound event.
Private Sub gridview1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridview1.RowDataBound
e.Row.Cells(1).Text = Server.HtmlDecode(e.Row.Cells(1).Text)
End Sub
I'm assuming from your example that your GridView has 2 columns and the image column is the second one, hence Cells(1) in each row. Hope this helps!
Do you know how to access textboxes added to a radgrid that are not bound but are used to trap any row related input a user typed in to the textbox for that column.
I need to access this data server side when a postback occurs.
Your thoughts are greatly appreciated
Thanking you
Tony
That depends on how those textboxes are being added/created. If by 'not bound' you mean they are in Template columns you should be able to use .FindControl in one of the grid's events to grab that textbox.
And again which event will depend on what is causing the postback to happen.
For the purpose of this code example I'll assume you are dealing with a Command item on the grid
Private Sub radGrid_ItemCommand(ByVal source As Object, ByVal e As Telerik.WebControls.GridCommandEventArgs) Handles RadGrid1.ItemCommand
Select Case e.CommandName
Case "Update"
Dim txt as Textbox
txt = e.Item.FindControl("textboxID")
If Not txt is Nothing Then someObject.someString = txt.Text
Case Else
'do something else
End Sub
Hope that helps.
Private Sub radGrid_ItemCommand(ByVal source As Object, ByVal e As Telerik.WebControls.GridCommandEventArgs) Handles RadGrid1.ItemCommand
Select Case e.CommandName
Case "Update"
Dim txt as Textbox
txt = e.Item.FindControl("textboxID")
If Not txt is Nothing Then someObject.someString = txt.Text
Case Else
'do something else
End Sub