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

Odd Behaviour : RadGrid

3 Answers 64 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ulysses
Top achievements
Rank 1
Ulysses asked on 12 Dec 2011, 06:42 AM
Found an issue on the following scenario:
1. Radgrid is filled from NeedDataSource event, with GridEditColumn
2. A dropdownlist that will serve as a selective filter for the contents for the radgrid, placed either in the commandtemplate or somewhere else; on dropdownlist selectedindexchange, bind the radgrid according to the selection method
3. Here's the problem
    Radgrid is filled with selected record but when "Edit" is clicked on the current record (item), it will rebind the radgrid from the first load (no filter, ALL record) and the item in Edit is now the "first" item in the record.

Any solution? Thanks.

3 Answers, 1 is accepted

Sort by
0
Milena
Telerik team
answered on 14 Dec 2011, 06:36 PM
Hello Ulysses,

We suspect that the issue could be caused by incorrect data-binding of the RadGrid. Note that, when handling the NeedDataSource event for binding the RadGrid control, the only place where the data source should be set is the NeedDataSource event handler. On this event you should ensure that the right data set is applied and in SelectedIndexChange event is needed just to databind(or rebind) the RadGrid.
 
Additionally, if you could send us a code snippet of your NeedDataSource and SelectedIndexChange events, we can inspect the problem.

Kind regards,
Milena
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Ulysses
Top achievements
Rank 1
answered on 14 Dec 2011, 11:35 PM
<!-- HTML Markup -->
<asp:ScriptManager runat="server" ID="scriptManager">
    </asp:ScriptManager>
    <telerik:RadAjaxManager runat="server" ID="ajaxManager"
        DefaultLoadingPanelID="loadingPanel">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="gridData">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="gridData" LoadingPanelID="loadingPanel" />
                </UpdatedControls>
            </telerik:AjaxSetting>
            <telerik:AjaxSetting AjaxControlID="ddlFilterCategory">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="gridData" LoadingPanelID="loadingPanel" />
                
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    <telerik:RadAjaxLoadingPanel runat="server" ID="loadingPanel"
        InitialDelayTime="5000" Transparency="50">
        Loading...
    </telerik:RadAjaxLoadingPanel>
    <telerik:RadGrid ID="gridData" runat="server" AutoGenerateColumns="False" CellSpacing="0"
        PageSize="15" GridLines="None" ShowStatusBar="true" AllowPaging="True"
        OnNeedDataSource="gridData_NeedDataSource"
        onitemdatabound="gridData_ItemDataBound">
        <MasterTableView DataKeyNames="id" CommandItemDisplay="Top">
            <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
            <CommandItemStyle Wrap="False" />
            <CommandItemTemplate>
                <div style="padding: 5px">
                    <table>
                        <tr>
                            <td>
                                Filter
                            </td>
                            <td>
                                <asp:DropDownList runat="server" ID="ddlFilterCategory" AutoPostBack="true" OnSelectedIndexChanged="ddlFilterCategory_SelectedIndexChanged" AppendDataBoundItems="true"
                                    Font-Names="Tahoma" Font-Size="11px">
                                    <asp:ListItem Text="Show All Category" Value="0"></asp:ListItem>
                                </asp:DropDownList>
                            </td>
                        </tr>
                    </table>
                </div>
            </CommandItemTemplate>
            <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
                <HeaderStyle Width="20px"></HeaderStyle>
            </RowIndicatorColumn>
            <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
                <HeaderStyle Width="20px"></HeaderStyle>
            </ExpandCollapseColumn>
            <Columns>
                <telerik:GridEditCommandColumn ButtonType="ImageButton" EditImageUrl="~/Images/pencil.png"
                    FilterControlAltText="Filter EditCommandColumn column">
                </telerik:GridEditCommandColumn>
                <telerik:GridBoundColumn DataField="id" Display="false" FilterControlAltText="Filter column column"
                    HeaderText="ID" UniqueName="id" Visible="False">
                    <HeaderStyle Font-Names="Tahoma" Font-Size="11px" />
                    <ItemStyle Font-Names="Tahoma" Font-Size="11px" />
                    <FooterStyle Font-Names="Tahoma" Font-Size="11px" />
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="data" FilterControlAltText="Filter column1 column"
                    HeaderText="Data" UniqueName="data">
                    <HeaderStyle Font-Names="Tahoma" Font-Size="11px" />
                    <ItemStyle Font-Names="Tahoma" Font-Size="11px" />
                    <FooterStyle Font-Names="Tahoma" Font-Size="11px" />
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="category" FilterControlAltText="Filter column2 column"
                    HeaderText="Category" UniqueName="category">
                    <HeaderStyle Font-Names="Tahoma" Font-Size="11px" />
                    <ItemStyle Font-Names="Tahoma" Font-Size="11px" />
                    <FooterStyle Font-Names="Tahoma" Font-Size="11px" />
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="date" FilterControlAltText="Filter column3 column"
                    HeaderText="Date Added" UniqueName="date">
                    <HeaderStyle Font-Names="Tahoma" Font-Size="11px" />
                    <ItemStyle Font-Names="Tahoma" Font-Size="11px" />
                    <FooterStyle Font-Names="Tahoma" Font-Size="11px" />
                </telerik:GridBoundColumn>
            </Columns>
            <EditFormSettings>
                <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                </EditColumn>
            </EditFormSettings>
        </MasterTableView>
        <FilterMenu EnableImageSprites="False">
        </FilterMenu>
        <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default">
        </HeaderContextMenu>
    </telerik:RadGrid>
 
 
<!-- Code Behind -->
 
namespace telerikODD
{
    public class xmlData
    {
        public string id { get; set; }
        public string data { get; set; }
        public string category { get; set; }
        public string date { get; set; }
 
    }
 
    public class xmlMethods
    {
        public static List<xmlData> ListXML()
        {
            string xmlFile = HttpContext.Current.Request.MapPath("~/App_Data/telerikData.xml");
            XDocument document = XDocument.Load(xmlFile);
            try
            {
                var data = document.Descendants("content").Select(d => new xmlData()
                {
                    category = d.Element("category").Value,
                    data = d.Element("contentData").Value,
                    id = d.Attribute("contentID").Value,
                    date = HttpUtility.HtmlDecode(d.Element("contentDate").Value)
                }).ToList();
                return data;
            }
 
            finally
            {
                if (document != null) document = null;
            }
        }
    }
 
 
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
 
 
    protected void gridData_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
                gridData.DataSource = xmlMethods.ListXML();
        }
 
        protected void gridData_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is GridCommandItem)
            {
                GridCommandItem item = (GridCommandItem)e.Item;
                DropDownList ddlFilterCategory = (DropDownList)item.FindControl("ddlFilterCategory");
                ddlFilterCategory.Items.Add(new ListItem("Category 1", "1"));
                ddlFilterCategory.Items.Add(new ListItem("Category 2", "2"));
                ddlFilterCategory.Items.Add(new ListItem("Category 3", "3"));
                ddlFilterCategory.Items.Add(new ListItem("Category 4", "4"));
            }
        }
 
        protected void ddlFilterCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddlFilterCategory = (DropDownList)sender;
            if (ddlFilterCategory.SelectedValue != "0")
            {
                string value = ddlFilterCategory.SelectedValue;
                gridData.DataSource = xmlMethods.ListXML().Where(d => d.category == value);
                gridData.DataBind();
            }
            else
            {
                gridData.DataSource = xmlMethods.ListXML();
                gridData.DataBind(); //or call ReBind() method?
            }
        }
 
    }
}


0
Milena
Telerik team
answered on 19 Dec 2011, 04:30 PM
Hello Ulysses,

On the base of the provided information I would confirm the suggestion of my previous post, that the issue is caused by incorrect data-binding. To resolve the problem, it is necessary to modify the NeedDataSource  and SelectedIndexChange events as it is shown bellow:

protected void gridData_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
       {
               if (ddlFilterCategory.SelectedValue != "0")
               {
                   string value = ddlFilterCategory.SelectedValue;
                   gridData.DataSource = xmlMethods.ListXML().Where(d => d.category == value);
 
               }
               else
               {
                   gridData.DataSource = xmlMethods.ListXML();
 
               }
       }

protected void ddlFilterCategory_SelectedIndexChanged(object sender, EventArgs e)
     {
          
             gridData.ReBind();
         
     }

Greetings,
Milena
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Grid
Asked by
Ulysses
Top achievements
Rank 1
Answers by
Milena
Telerik team
Ulysses
Top achievements
Rank 1
Share this question
or