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

Issues in Sorting in the grid.

1 Answer 13 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ankit
Top achievements
Rank 1
Ankit asked on 03 Oct 2011, 11:43 AM
I have a grid with 7 columns of which 3 are checkboxes. I have applied sorting in the grid as shown below :-
<telerik:RadGrid ID="grdSearchResults" runat="server" AllowSorting="true" 
  
GridLines="None" Skin="Windows7" AutoGenerateColumns="False" 
  
PageSize="5" onitemdatabound="grdSearchResults_ItemDataBound" 
  
onneeddatasource="grdSearchResults_NeedDataSource" >
  
<ClientSettings EnablePostBackOnRowClick="true" EnableRowHoverStyle="true"> </ClientSettings>
  
<MasterTableView Width="100%" TableLayout="Auto" CommandItemDisplay="Top" AllowPaging="true" AllowSorting="true" PageSize="10" >
When I click on any of the column headers the grid is sorted correctly for the first 4 columns. However the 3 checkbox columns are not re-ordered according to the newly sorted grid. I am attaching screenshots for "Grid before sorting" and "Grid After Sorting" to get a better idea. The 3 chkbox columns are manipulated in the itemdatabound event of the rad grid. The code of item databound event is as follows :-
protected void grdSearchResults_ItemDataBound(object sender, GridItemEventArgs e)
        {
            try
            {
            if (e.Item is GridDataItem)
            {
                DataTable dt = (DataTable)ViewState["StatementList"];
                if (dt != null)
                {
                     
                        string credit = dt.Rows[e.Item.ItemIndex]["CreditImage"].ToString();
                        string BackImages = dt.Rows[e.Item.ItemIndex]["BackImage"].ToString();
                        string AddImages = dt.Rows[e.Item.ItemIndex]["Image"].ToString();
  
                        CheckBox chkCredits = (CheckBox)e.Item.FindControl("chkCredits");
                        CheckBox chkBackImages = (CheckBox)e.Item.FindControl("chkBackImages");
                        CheckBox chkAddImages = (CheckBox)e.Item.FindControl("chkAddImages");
  
  
                        if (credit == "True")
                            chkCredits.Checked = true;
                        else
                            chkCredits.Checked = false;
                        if (BackImages == "True")
                            chkBackImages.Checked = true;
                        else
                            chkBackImages.Checked = false;
                        if (AddImages == "True")
                            chkAddImages.Checked = true;
                        else
                            chkAddImages.Checked = false;
                }
  
            }
          
            }
            catch (Exception ex)
           {
                Log.WriteLine(LogParams.USERCONTROLS, "StatementGrid : grdSearchResults_ItemDataBound : " + ex.Message, LogLevel.Error, LogParams.ERRORMSG);
                LoadDummyData();
            }
        }
Let me know how can I solve this issue and get the sorting right.

1 Answer, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 03 Oct 2011, 01:29 PM
Hello,

// using datakey
<MasterTableView DataKeyNames="ID"
 
OR
 
//using column
 <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID">
                    </telerik:GridBoundColumn>


protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        DataTable dt = (DataTable)ViewState["StatementList"];
        if (e.Item is GridDataItem)
        {
            GridDataItem item = e.Item as GridDataItem;
            foreach (DataRow dr in dt.Rows)
            {
                //if (dr["ID"].ToString() == item.GetDataKeyValue("ID").ToString()) // using DataKey
                if (dr["ID"].ToString() == item["ID"].Text) // using column value
                {
                    ((CheckBox)item.FindControl("chkCredits")).Checked = true;
                }
            }
        }
    }

let me know if any concern.

Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Ankit
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Share this question
or