Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
150
Re-bind from code behind
posted

Hi,

I'm binding programatically the WHDG to List<TestObj> from code behind. I have a button Delete that has server event handler which deletes a row from the list and tries to rebind the grid. The problem is that when the page is show again, the grid contains the deleted row and the last row disappears. I have a button that simply do a postback and then everything seems to be OK after the rebind in Page_Load. How can I rebind the grid in the Delete event handler to show the correct data?  I want the grid with ViewStateMode=Disabled and I don't want to use declarative DataSource controls. Here is the sample code:

    <asp:Button ID="Button1" runat="server" Text="Button" />
    <ig:WebHierarchicalDataGrid ID="whdg" runat="server" Height="400px"
        Width="700px" HeightAutoGenerateBands="False" AutoGenerateColumns="False" Key="LLL"
        AutoGenerateBands="False" DataKeyFields="Pk" InitialDataBindDepth="-1" InitialExpandDepth="2"
        EnableAjax="False" ViewStateMode="Disabled">
        <Bands>
        </Bands>
        <Columns>
            <ig:TemplateDataField Key="Delete" VisibleIndex="0" Width="16">
                <ItemTemplate>
                    <asp:ImageButton ID="btnDelete" runat="server" CommandArgument='<%# Eval("Pk") %>'
                        OnCommand="btnDelete_Command" AlternateText="Delete" ImageUrl="~/delete.gif"
                        CausesValidation="False" />
                </ItemTemplate>
            </ig:TemplateDataField>
            <ig:TemplateDataField Key="Name" VisibleIndex="1">
                <ItemTemplate>
                    <asp:Literal ID="Literal1" runat="server" Text='<%# DataBinder.Eval(((Infragistics.Web.UI.TemplateContainer)Container).DataItem, "Name") %>'></asp:Literal>
                </ItemTemplate>
                <Header Text="name" />
            </ig:TemplateDataField>
        </Columns>
        <Behaviors>
            <ig:Selection CellClickAction="Row" RowSelectType="Single">
            </ig:Selection>
        </Behaviors>
    </ig:WebHierarchicalDataGrid>
        public class TestObj
        {
            private string _pk;
            private string _name;

            public string Pk
            {
                get { return _pk; }
                set { _pk = value; }
            }

            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
        }

        public List<TestObj> L
        {
            get { return (List<TestObj>)Session["TestObj"]; }
            set { Session["TestObj"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                L = new List<TestObj>() 
                { 
                    new TestObj() { Name = "Name1", Pk = "1" }, 
                    new TestObj() { Name = "Name2", Pk = "2" }, 
                    new TestObj() { Name = "Name3", Pk = "3" }, 
                    new TestObj() { Name = "Name4", Pk = "4" } 
                };
            BindGrid();
        }

        private void BindGrid()
        {
            Infragistics.Web.UI.DataSourceControls.WebHierarchicalDataSource whds = new Infragistics.Web.UI.DataSourceControls.WebHierarchicalDataSource();
            Infragistics.Web.UI.DataSourceControls.DataView view = new Infragistics.Web.UI.DataSourceControls.DataView();
            view.DataSource = L;
            view.ID = "L";
            whds.DataViews.Add(view);
            whdg.DataSource = whds;
            whdg.DataBind();
        }

        public void btnDelete_Command(object sender, CommandEventArgs e)
        {
            L.RemoveAll(p => p.Pk == e.CommandArgument.ToString());
            BindGrid();

        }
  • 19693
    Verified Answer
    posted

    Hello ppopov,

    You should use ClearDataSource()  when you are setting different data source.

    For example if you have changed your datasource (like in your case) before assigning the new

    DataSource you should invoke ClearDataSource() .

    When you are setting DataSource because of the standard procedure needed by

    WebHierarchicalDataGrid, than you do not need to invoke ClearDataSource().

    Let me know if you need further assistance.