This is a migrated thread and some comments may be shown as answers.

How do I clear grid?

6 Answers 215 Views
Grid
This is a migrated thread and some comments may be shown as answers.
William
Top achievements
Rank 1
William asked on 02 Dec 2008, 10:27 PM
I'm sure there is an easy answer, but how do I clear a grid which has been data bound? I have a condition in NeedsDataSource where I may need to clear the grid. I have tried setting the data source to null but any records which are already in the grid still appear. There is also no Clear() method.

Thanks

6 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 03 Dec 2008, 04:20 AM
Hi William,

Try the following code snippet to clear the Grid in the NeedDataSource event.

CS:
 protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e) 
    { 
        conn.Open(); 
        SqlDataAdapter adp = new SqlDataAdapter("select ProductName,ProductID,SupplierID from  Products ", conn); 
        DataTable dt = new DataTable(); 
        adp.Fill(dt); 
        RadGrid1.DataSource = dt
        conn.Close(); 
 
        //clear the Grid if the condition is satisfied 
        if (RadGrid1.CurrentPageIndex != 0) 
        { 
            RadGrid1.DataSource = string.Empty; 
        } 
         
    } 


Thanks
Shinu.
0
William
Top achievements
Rank 1
answered on 03 Dec 2008, 04:50 AM
Thanks for the reply.

I managed to get it working with:

radgDocuments.DataSource =

new List<string>(0);

...which is a similar method to yours. However, I just feel this is a hack. There used to be a Clear() method but this isn't there any more, and I would have thought that setting the DataSource to null would have worked. Why does this work when setting it to null doesn't? Surely this can't be by design??

 

0
Neil
Top achievements
Rank 1
answered on 25 Mar 2009, 11:54 AM
Maybe we are being stupid (as usual) but we cannot get either of these ideas to work. We have tried :
    Dim dtEmptyTable As New DataTable
    Me.trgListGrid.DataSource = dtEmptyTable
    Me.trgListGrid.DataSource.databind()
- - and we have tried:
Me.trgListGrid.DataSource.DataSource = String.Empty

- - bnut we get a very similar error on both: Object variable or With block variable not set. "Check to determine if the object is null before calling the method

Any better ideas please?
Neil
0
Yavor
Telerik team
answered on 25 Mar 2009, 12:22 PM
Hello Neil ,

Attached to this message, is a small application, which handles a similar task.
I hope it helps.

Regards,
Yavor
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
William
Top achievements
Rank 1
answered on 25 Mar 2009, 09:45 PM

Hi
It's interesting to see someone from Telerik replying with this solution. As I said, it seems like a hack. Why can't we just have the Clear() method back or something which makes sense? This is not a very intuitive way to do it and obviously a few people agree.

0
Sebastian
Telerik team
answered on 26 Mar 2009, 09:31 AM
Hello William,

Since RadGrid is data bound control, the proper means to display its NoRecords template or clear its data is to bind the control to an empty data source. We do not consider this as a hack because the same would be applicable for any other Telerik/MS control in data-bound context.

Kind regards,
Sebastian
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
William
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
William
Top achievements
Rank 1
Neil
Top achievements
Rank 1
Yavor
Telerik team
Sebastian
Telerik team
Share this question
or