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

Clearing Grid, NeedDataSource, Empty List, Not Working! :(

9 Answers 114 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Felix
Top achievements
Rank 1
Felix asked on 12 Jun 2013, 10:27 PM
Hi All,

I have a radGrid in a radTabStrip that has paging enabled like so:
        </telerik:RadTabStrip>
            <telerik:RadGrid ID="gridWORKORDERS" runat="server" OnRowDrop="gridWORKORDERS_RowDrop"  Height="400px"
                         AllowMultiRowSelection="false" EnableHeaderContextMenu="false" Width="444px" HorizontalAlign="Center"
                         OnItemCommand="radgridWORKORDERS_ItemCommand"
                         OnNeedDataSource="gridWORKORDERS_NeedDataSource">
 
            <MasterTableView DataKeyNames="pm_strWorkOrderID" Width="444px" HorizontalAlign="Center"
                             TableLayout="Fixed" ClientDataKeyNames="pm_strWorkOrderID"
                             AllowPaging="True"
                             AllowCustomPaging="true"
                             AutoGenerateColumns="false">
... etc.

In the code behind I have this:
protected void gridWORKORDERS_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
    int recordCount = 0;
    List<WorkOrderEx> workOrders = new List<WorkOrderEx>();
    string sessionTab = Session[_WO_FILTER_TAB] == null ? "Unassigned" : Session[_WO_FILTER_TAB].ToString();
    gridWORKORDERS.DataSource = null;
 
    switch (sessionTab)
    {
        case "Unassigned":
            // Need to do this in a couple of steps or the grid paging doesn't work properly
            recordCount = DbContext.WorkOrders.Where(f => f.AssignedTechs.Count <= 0).Count();
 
            // Save ourselves some time..
            if (recordCount > 0)
                workOrders = DbContext.WorkOrders.Where(f => f.AssignedTechs.Count <= 0)
                                      .OrderBy(x => x.WorkOrderId)
                                      .Skip(gridWORKORDERS.PageSize * gridWORKORDERS.CurrentPageIndex)
                                      .Take(gridWORKORDERS.PageSize)
                                      .Select(k => new WorkOrderEx(DbContext, k)).ToList();
            break;
 
        case "blah blah blah.. Looks very much like the first case statement.":
            break;
    }
 
    gridWORKORDERS.VirtualItemCount = recordCount;
    gridWORKORDERS.DataSource = workOrders;
    //gridWORKORDERS.Rebind();      <-- Enabling this blows everything up.. You know what I mean. :)
}

This whole thing kind of works.  I can't use .Bind() or .Rebind() because of the implicit binding that happens with this event. However, the paging doesn't work all that great.  If I page to the last page the final two records are displayed.  If I change tabs (yes in a tab strip) I manually call the NeedDataSource(null, null) to get the dataset to change from 'Unassigned' to some other filtered value of the data.  The problem is that the old data that was on the grid + the new data shows up! What the heck?  What I could use right now is a way to clear this grid of it's contents and bind it to a new list.  Where is the .ClearGridContents() method I so desire right now? Maybe a grid.Refresh thrown in for good measure.

So, long story short. How do I clear this darn grid and have it auto-bind (like i have a choice), with a fresh set of data that is artifact free?

Thanks,

Felix
BTW, I'm using OpenAccess which I find very easy to use.

9 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 13 Jun 2013, 12:28 PM
Hi,

I'm not sure if this is what you require,please try this,and let know if you dint find the solution.
Try setting
gridWORKORDERS.DataSource = null;
gridWORKORDERS.DataBind();
or have a look at this forum,cleaning grid items.

Thanks,
Princy



0
Felix
Top achievements
Rank 1
answered on 14 Jun 2013, 05:29 PM
Hi Princy,

Thanks for taking the time.  That won't work because of the way the grid event flow seems to be working when doing advanced binding (i.e. using the NeedDataSource event handler.)  I have tried that solution and been all around these boards looking for a solution but to no avail so far.

On a good note though, It looks like I have solved this conundrum and will be posting the solution later today once I verify the solution works.

Thanks Again for your time,

Felix
0
John
Top achievements
Rank 1
answered on 11 Sep 2013, 06:44 AM
Hi Felix,

How did you resolve your problem? I have a similar issue where i want to clear a grid that has been populated with NeedDataSource.

Any help would he great,

Thanks,
JC
0
Princy
Top achievements
Rank 1
answered on 11 Sep 2013, 08:31 AM
Hi John,

Please try the following code snippet.

C#:
public void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
  {   
    if //(Some Condition)
     {
      this.RadGrid1.DataSource = new string[0];
     }           
  }

Thanks,
Princy
0
John
Top achievements
Rank 1
answered on 11 Sep 2013, 11:19 PM
Hi Princy,

Thanks for your reply, this does not null the datasource thou. The grid will then display 'no records' when i want it to be null'd and not render like if the datasource wasn't set.

Thanks,
John
0
John
Top achievements
Rank 1
answered on 11 Sep 2013, 11:20 PM
Also on another note, once I have set a datasource to this value i can no longer set it again with data it keeps showing 'no records' even if i set it with a populated datasource. Any ideas?
0
Princy
Top achievements
Rank 1
answered on 13 Sep 2013, 09:50 AM
Hi John,

Please try the below sample code that i tried which works fine at my end.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="true" OnNeedDataSource="RadGrid1_NeedDataSource">
    <MasterTableView DataKeyNames="CustomerID">    
    </MasterTableView>
</telerik:RadGrid>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Sumbit" OnClick="Button1_Click" />

C#:
public DataTable GetDataTable(string query)
   {
       String ConnString = ConfigurationManager.ConnectionStrings["Northwind_newConnectionString3"].ConnectionString;
       SqlConnection conn = new SqlConnection(ConnString);
       SqlDataAdapter adapter = new SqlDataAdapter();
       adapter.SelectCommand = new SqlCommand(query, conn);
 
       DataTable myDataTable = new DataTable();
 
       conn.Open();
       try
       {
           adapter.Fill(myDataTable);
       }
       finally
       {
           conn.Close();
       }
 
       return myDataTable;
   }
 
   protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
   {
       RadGrid1.DataSource = GetDataTable("SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, PostalCode FROM Customers");
   }
 
   protected void Button1_Click(object sender, EventArgs e)
   {
       if (TextBox1.Text == "Some Condition")
       {
           RadGrid1.DataSource = null;
           RadGrid1.DataBind();
       }
       else
       {
           RadGrid1.Rebind();
       }
   }

Thanks,
Princy
0
John
Top achievements
Rank 1
answered on 15 Sep 2013, 11:03 PM
Hi Princy,

Thanks for your reply, my understanding from the telerik documentation is that we shouldn't use DataBind() or the simple binding methods when using advanced binding.

Can someone from Telerik give some input on this please?

Thanks,
John
0
Princy
Top achievements
Rank 1
answered on 18 Sep 2013, 07:05 AM
Hi John,

Yes,It is important to remember that,while using AdvancedDataBinding ,you should never call DataBind() method in a NeedDataSource event handler or mix simple data-binding mode with advanced data-binding .

Thanks,
Princy
Tags
Grid
Asked by
Felix
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Felix
Top achievements
Rank 1
John
Top achievements
Rank 1
Share this question
or