How to Clear RadGrid??

3 Answers 4642 Views
Grid
joni
Top achievements
Rank 1
joni asked on 21 May 2008, 03:45 PM
Hi,
This seems like it should be so simple - but I can't get it to work. I have a RadGrid , using AjaxManager and NeedDataSource.  I execute different queries - depending on the search criteria selected and return a dataset that is set to the DataSource:

this.RadGrid1.MasterTableView.DataSource = ds;

this.RadGrid1.MasterTableView.DataMember = ds.Tables[0].ToString();


This works great - however I have a clear button that I am trying to clear the grid with - the code is executed but the rows still are in the grid on the screen  - I set the DataSource and DataMember to null and rebind.

protected

void btnClear_Click(object sender, EventArgs e)

{

this.RadGrid1.MasterTableView.DataSource = null;

this.RadGrid1.MasterTableView.DataMember = null;

this.RadGrid1.DataSource = null;

this.RadGrid1.Rebind();

}

So when that didn't work I thought I would manually try to remove the rows and I can't seem to find any RowsCollecion to iterate through and remove.

What am I missing here?? Any help is appreciated. Thanks!

Edited to add - I have EnableViewState set to True on the grid

Edited again - I was able to get the functionality I wanted by creating an empty table and setting the dataset to the datasource and the empty table to the datamember and then doing the this.RadGrid1.Rebind(); 

I would still be curious as to why setting the datasource and datamember to null would not work. It seems as though this functionality works when the NeedDataSource event is called automatically without having to do a Rebind.

joni

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 22 May 2008, 06:46 AM
Hi Joni,

Try the following line code snippet to clear the RadGrid.

CS:
 protected void Button1_Click(object sender, EventArgs e)  
    {  
        RadGrid1.DataSource = new string[] { };  
    } 

Thanks
Princy.
joni
Top achievements
Rank 1
commented on 22 May 2008, 01:23 PM

Hi Princy,
Thanks for the response. That seems to work well if you don't go over your page limit. If you do and you have paging enabled it leaves the Change Page and Page Numbers and the message regarding how many items of how many items are being displayed on the page status bar.  Any ideas how to clear this information? Thanks!!

Edited to add: I installed the latest hotfix and this issue has been fixed - thanks!!

joni
Jason
Top achievements
Rank 1
commented on 10 Jan 2011, 03:39 AM

RadGrid1.DataSource = new string[] { }; 
does not work even if you also do RadGrid1.DataBind();
Need a way to clear the grid...rows are getting duplicated.


.
Please explain how I can display a RadGrid after making changes
to the datasource and not get duplicated rows. What is happening is...
RadGrid1.DataSource = null; <--does nothing
RadGrid1.DataBind(); <--does nothing...item collection still has items











RadGrid1.DataSource = List<of myObjects> <--is appended to the current RadGrid1.DataSource





Daniel
Telerik team
commented on 13 Jan 2011, 09:46 AM

Hello Christopher,

You won't get duplicated rows under normal circumstances. Are you changing the grid structure dynamically on postback?
If this is not the case, could you please let me know how to reproduce the problem on my end?

Best regards,
Daniel
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Anjan Nimmana
Top achievements
Rank 1
commented on 17 Mar 2011, 12:04 AM

I am facing problem with duplicate records in Radgrid after postback.

I tried with below statement to clear the radgrid. but it is not working.
RadGrid1.DataSource = new string[] { };

I also tried with below statement to clear the radgrid. I put this statement in button click before redirecting to next page. but it is also not working. 
RadGrid1.Dispose();

Can you please suggest, how to clear or remove data in Radgrid while moving to next page.
Gowtham
Top achievements
Rank 1
commented on 22 Jan 2015, 12:59 PM

Working fine... thanks Princy
Robert
Top achievements
Rank 1
commented on 22 Jul 2016, 12:08 PM

Using RadGrid1.DataSource = new string[] { };

 

does not working in the NeedDataSource event.

Viktor Tachev
Telerik team
commented on 27 Jul 2016, 09:59 AM

Hi Robert,

When you set the DataSource for the RadGrid to an empty array there would be no records displayed in the grid.

Would you elaborate in more detail on what is not working as expected on your end? What is the behavior that you would like to implement?


Regards,
Viktor Tachev
Telerik by Progress
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Robert
Top achievements
Rank 1
commented on 27 Jul 2016, 11:01 AM

If I use the line RadGrid1.DataSource = new string[] { }; in the NeedDataSource event, I still see the empty rows that were displayed previously.

I have to use RadGrid1.DataSource = null and cross my fingers.

Viktor Tachev
Telerik team
commented on 29 Jul 2016, 12:13 PM

Hello Robert,

Would you open a support ticket and send a runnable sample where the behavior you are seeing is replicated? This will give us better understanding of your scenario. Moreover, we will be able to examine the behavior and give more to-the-point suggestions on how to achieve your goal.

Regards,
Viktor Tachev
Telerik by Progress
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Beniamin
Top achievements
Rank 1
commented on 05 Aug 2019, 07:17 AM

I have problems with clearing RadGrid also. I tried to pass empty string array as new DataSource but it doesn't work. RadGrid throws "InvalidOperationException" -> "Elements DataSource and DataSourceId are already defined [...]". I tried to get to object and clear it: var dataSource = myGrid.DataSource, but there is a NULL. Soo Telerik what I should do? It's funny that so basic function is so hard to do. 
Attila Antal
Telerik team
commented on 07 Aug 2019, 05:10 PM

Hi Beniamin,

Both Programmatic (DataSource) and Declarative (DataSourceID) data binding methods cannot be used at the same time. That is why the described error is thrown.

Clearing the Grid on Client-Side:

If the Grid was bound to data on the Client, the following JavaScript code would clear the records of the Grid:

            function SomeFunction() {
                var grid = $find('<%= RadGrid1.ClientID %>');
                grid.get_masterTableView().set_dataSource([]);
                grid.get_masterTableView().dataBind();
            }

To clear the Grid records when bound using Declarative DataSource, you will need to clear the DataSource control itself. 

Assuming the following DataSource control:

        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
            SelectCommand="SELECT [OrderID], [OrderDate], [Freight], [ShipName], [ShipCountry] FROM [Orders]"></asp:SqlDataSource>

You can clear the Grid records by making the DataSource control return no data:

    protected void RadButton1_Click(object sender, EventArgs e)
    {
        SqlDataSource1.SelectCommand = "SELECT TOP 0 [OrderID], [OrderDate], [Freight], [ShipName], [ShipCountry] FROM [Orders]";
        RadGrid1.Rebind();
    }

If using programmatic binding, you can try to clear the Grid records as described in earlier posts in this forum:

    protected void RadButton1_Click(object sender, EventArgs e)
    {
        RadGrid1.DataSource = null;
        RadGrid1.Rebind();
    }

 

Kind regards, Attila Antal
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Karl
Top achievements
Rank 1
answered on 12 Apr 2011, 11:29 AM
Try the following, it works for us.

RadGrid1.DataSource = null;
RadGrid1.DataBind();

It should be noted though that if your cycling through pages of the grids data, you shouldn't need to clear the data source so you may need to look into how you're binding your data if this is what you mean when you said...

"Can you please suggest, how to clear or remove data in Radgrid while moving to next page."
Dnyaneshwar
Top achievements
Rank 1
commented on 03 Sep 2014, 04:46 AM

You can clear grid and show empty message by using following code.
 RadGrid1.DataSource = new string[] { };
            RadGrid1.DataBind();
Dnyaneshwar
Top achievements
Rank 1
commented on 03 Sep 2014, 04:47 AM

 RadGrid1.DataSource = new string[] { };
            RadGrid1.DataBind();
Jonathan
Top achievements
Rank 1
commented on 17 Oct 2016, 04:54 PM

This does not work in ASPX.VB you encounter an error "Overload Resolution failed because no accesible new accepts this number of arguments "
Daniel
Telerik team
commented on 18 Oct 2016, 08:21 AM

Hello Jonathan,

Have you tried supplying an empty array to RadGrid as suggested by Viktor?

Regards,
Daniel
Telerik by Progress
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
0
EdyTeddy-1975
Top achievements
Rank 2
answered on 06 Sep 2016, 11:19 AM

Hi,

radGridView1.Columns.Clear();
radGridView1.Rows.Clear();

E,

 

Tags
Grid
Asked by
joni
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Karl
Top achievements
Rank 1
EdyTeddy-1975
Top achievements
Rank 2
Share this question
or