Clearing gridview upon certain condition in vb.net - asp.net

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

Related

UpdatePanel Crashing Other Update Panels Not Working

I have very strange issues. I have used Update Panels before and never had issues but because I am grouping listviews I get the issues. I have about 4 Update Panels on a page which I call by using the panelname.update() in code behind, and used to all work.
Then because I had to group a bunch of listviews inside each other I had to use a PageLoad to DataBind rather than actually putting the data sources on the asp page. The data all works with Listview when page loads, but now the update panels don't work on async postback at all.
If I take out uppnlSOL.Update() in code behind all the rest start working again. The update panel that causes the issue is the same one that contains the listview with the DataBind.
ASP page has all panels have childrenastriggers="false" UpdateMode="Conditional" hence I call them all from code behind. I also tried removing the uppnlSOL.Update() from code behind and placing a trigger on the uppnlSOL on the asp page. As soon as it launches I get same result. I removed the trigger then the other 3 panels work again. I need all 4 working and
I am confused, its almost like its rendering while its trying to do the update panel or something. I even tried a pause for 3 seconds after DataBind then trying updatepanel.Update() and all 4 still didn't work.
I will try to put some code below of what is sort of going on.
Protected Sub Packing_Load(sender As Object, e As EventArgs) Handles Me.Load
If IsPostBack = False Then
lvSOLGrpDelAdd.DataSource = tblDespatchA.DespatchPackSOLGrpDelAdd_Get(IDSO:=hdnIDSO.Value)
lvSOLGrpDelAdd.DataBind()
End If
End Sub
Protected Sub lvSOLGrpDelAdd_RowDataBound(sender As Object, e As ListViewItemEventArgs) Handles lvSOLGrpDelAdd.ItemDataBound
Dim lvSOLGrpDelMeth As ListView = DirectCast(e.Item.FindControl("lvSOLGrpDelMeth"), ListView)
lvSOLGrpDelMeth.DataSource = tblDespatchA.DespatchPackSOLGrpDelMeth_Get(IDSO:=hdnIDSO.Value, IDGrpDelAdd:=DataBinder.Eval(e.Item.DataItem, "IDGrpDelAdd"))
lvSOLGrpDelMeth.DataBind()
End Sub
Protected Sub lvSOLGrpDelMeth_RowDataBound(sender As Object, e As ListViewItemEventArgs)
Dim lvSOL As ListView = DirectCast(e.Item.FindControl("lvSOL"), ListView)
lvSOL.DataSource = tblDespatchA.DespatchPackSOL_Get(IDSO:=hdnIDSO.Value, IDGrpDelAdd:=DataBinder.Eval(e.Item.DataItem, "IDGrpDelAdd").ToString, IDGrpDelMeth:=DataBinder.Eval(e.Item.DataItem, "IDGrpDelMeth").ToString)
lvSOL.DataBind()
End Sub
Protected Sub btnAllocateLine_Click(sender As Object, e As EventArgs)
Dim lvRow As Object = DirectCast(sender, Object).Parent
Dim hdnIDSOL As HiddenField = DirectCast(lvRow.FindControl("hdnIDSOL"), HiddenField)
Dim lstQtyAvail As DropDownList = DirectCast(lvRow.FindControl("lstQtyAvail"), DropDownList)
tblDespatchA.DespatchPackSOLAllocate_Save(IDSO:=hdnIDSO.Value, IDSOL:=hdnIDSOL.Value, AllocateQty:=lstQtyAvail.SelectedValue)
Bind()
End Sub
Protected Sub Bind()
uppnlDOL.DataBind()
uppnlDOL.Update()
uppnlDBox.DataBind()
uppnlDBox.Update()
uppnlFooter.DataBind()
uppnlFooter.Update()
'I HAVE TO REGET FROM DATABASE CHANGES THAT HAVE HAPPEN AND
'I KNOW THIS BIT WORKS BECAUSE I HAVE TESTED THE DATA.
lvSOLGrpDelAdd.DataSource = tblDespatchA.DespatchPackSOLGrpDelAdd_Get(IDSO:=hdnIDSO.Value)
lvSOLGrpDelAdd.DataBind()
uppnlSOL.Update() ' THIS BIT WHEN I PUT IN THIS MAKES ALL THE OTHER PANELS CRASH
End Sub
Here we go again I answer my own question because nobody would help, but I will help anyone else with similar situation because I'm nice.
The reason it crashed all the other panels is because on my ASP.net page had some generated code in there using <% Response.Write("stuff here") %> and because of using the Response.Write caused it crash.
The Update Panels are doing a async post back and me calling a Response.Write() at the same time as PanelName.Update() caused this issue. I am looking for another method to write to the screen without using response.write and that would solve my 2nd problem.
Any Ideas would be appreciated.

Gridview data not refreshing

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.

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

what is wrong in my this vb.net code

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim lab As Label = Me.GridView1.FindControl("Label1")
If TextBox2.Text = "7" Then
GridView1.SelectedRow.Cells(2).Text = "500"
Else
GridView1.SelectedRow.Cells(2).Text = "950"
End If
End Sub
The following error occurs : Object reference not set to an instance of an object.
You've got this code in your Page Load event, so it will run when the page is first loaded, and on every single postback. This probably isn't what you want.
I imagine that on the very first load, there isn't a selected row in your GridView, so GridView1.SelectedRow is going to be null. If this isn't null, then Cells or Cells(2) definitely will be. Trying to access a property on null is going to throw a NullReferenceException - "Object reference not set to an instance of an object".
As this MSDN example shows, you're probably better off accessing the SelectedRow property in an event handler for the SelectedIndexChanged event of the GridView.
Dim lab As Label = Me.GridView1.FindControl("Label1")
It doesn't look like you're doing anything with this label. Put a breakpoint on that line and see if it finds it. If it doesn't and you don't even use it, take the line out.
Also, check if textbox2 is valid whilst debugging.

Accessing TextBox Column in RadGrid By Telerik

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

Resources