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

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.
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
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
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
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.
Using RadGrid1.DataSource = new string[] { };
does not working in the NeedDataSource event.
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
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.
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
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

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."
RadGrid1.DataSource = new string[] { };
RadGrid1.DataBind();
RadGrid1.DataBind();
Have you tried supplying an empty array to RadGrid as suggested by Viktor?
Regards,
Daniel
Telerik by Progress

Hi,
radGridView1.Columns.Clear();
radGridView1.Rows.Clear();
E,