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

Delete Radgrid

9 Answers 86 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Brad
Top achievements
Rank 1
Brad asked on 30 Sep 2013, 10:16 PM
Lets say i have 5 radgrids on my page

Radgrid1
Radgrid2
Radgrid3
Radgrid4
Radgrid5

And they each have their own datasource like this

Radgrid1.datasource = ds1
Radgrid2.datasource = ds2
Radgrid3.datasource = ds3
Radgrid4.datasource = ds4
Radgrid5.datasource = ds5

Is it possible to be able to delete lets say Radgrid4 from the page and then reset all the numbers so they are in sync? If so, can someone please provide a simple example on how to do that?

9 Answers, 1 is accepted

Sort by
0
Brad
Top achievements
Rank 1
answered on 03 Oct 2013, 02:18 PM
here is the code i have so far, can someone please help me with this. I am able to delete the grid, but the issue is whenever i delete a grid, it deletes it fine, but the numbers go out of sync, until i click on something on the page and it reloads.

So for example if i have

Radgrid1
Radgrid2
Radgrid3

and i decide to delete Radgrid2, the numbers will look like this

Radgrid1
Radgrid3

Until i click on something and then it will change to

Radgrid1
Radgrid2

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
 
    protected void Page_Init(object sender, EventArgs e)
    {
        string ctrlname = this.Page.Request.Params.Get("__EVENTTARGET");
 
        if (Session["Tables"] == null)
        {
 
            Session.Add("Tables", 1);
 
            DataTable dt = GetTable();
 
            Session.Add(Session["Tables"].ToString(), dt);
            RadGrid gv = new RadGrid();
            gv.ID = "Grid-" + Session["Tables"].ToString();
            gv.DataSource = dt;
            gv.DataBind();
            LinkButton lb = new LinkButton();
            lb.ID = "Delete-Grid-" + Session["Tables"].ToString();
            lb.Text = "Delete Grid " + Session["Tables"].ToString();
            lb.Click += new EventHandler(DeleteGrid);
            PlaceHolder1.Controls.Add(lb);
            PlaceHolder1.Controls.Add(gv);
 
        }
        if (IsPostBack)
        {
            PlaceHolder1.Controls.Clear();
            int next = (int)Session["Tables"];
 
            for (int i = 1; i <= (int)Session["Tables"]; i++)
            {
                RadGrid gv = new RadGrid();
                gv.ID = "Grid-" + i.ToString();
                gv.DataSource = (DataTable)Session[i];
                gv.DataBind();
                LinkButton lb = new LinkButton();
                lb.ID = "Delete-Grid-" + i.ToString();
                lb.Text = "Delete Grid " + i.ToString();
                lb.Click += new EventHandler(DeleteGrid);
                PlaceHolder1.Controls.Add(lb);
                PlaceHolder1.Controls.Add(gv);
 
            }
            if (ctrlname == "Button1")
            {
                next = next + 1;
                Session["Tables"] = next;
                DataTable dt = GetTable();
                Session.Add(Session["Tables"].ToString(), dt);
                RadGrid gv = new RadGrid();
                gv.ID = "Grid-" + next.ToString();
                gv.DataSource = (DataTable)Session[next];
                gv.DataBind();
                LinkButton lb = new LinkButton();
                lb.ID = "Delete-Grid-" + next.ToString();
                lb.Text = "Delete Grid " + next.ToString();
                lb.Click += new EventHandler(DeleteGrid);
                PlaceHolder1.Controls.Add(lb);
                PlaceHolder1.Controls.Add(gv);
            }
 
        }
    }
 
 
 
    protected void Button1_Click(object sender, EventArgs e)
    {
 
    }
 
 
    protected void DeleteGrid(object sender, EventArgs e)
    {
        LinkButton gridLink = (LinkButton)sender;
        String gridNum = gridLink.ID.ToString().Split('-').Last();
 
        RadGrid grid = (RadGrid)this.Page.FindControl("Grid-" + gridNum);
        LinkButton lbd = (LinkButton)this.Page.FindControl("Delete-Grid-" + gridNum);
 
        PlaceHolder1.Controls.Remove(grid);
        PlaceHolder1.Controls.Remove(lbd);
 
        int next = (int)Session["Tables"];
        next = next - 1;
        Session.Add("Tables", next);
        Session.Remove(gridNum);
    }
 
 
 
 
 
 
 
 
    public DataTable GetTable()
    {
        //
        // Here we create a DataTable with a few columns.
        //
        // Create Datatable to store all colums
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("Size", typeof(string)));
        dt.Columns.Add(new DataColumn("Description", typeof(string)));
        dt.Columns.Add(new DataColumn("Quantity", typeof(string)));
        dt.Columns.Add(new DataColumn("Unit", typeof(string)));
        dt.Columns.Add(new DataColumn("Duration", typeof(string)));
        dt.Columns.Add(new DataColumn("DurationType", typeof(string)));
        dt.Columns.Add(new DataColumn("Amount", typeof(string)));
        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["Size"] = string.Empty;
        dr["Description"] = string.Empty;
        dr["Quantity"] = string.Empty;
        dr["Unit"] = string.Empty;
        dr["Duration"] = string.Empty;
        dr["DurationType"] = string.Empty;
        dr["Amount"] = string.Empty;
        dt.Rows.Add(dr);
 
        dr = dt.NewRow();
        dr["RowNumber"] = 2;
        dr["Size"] = string.Empty;
        dr["Description"] = string.Empty;
        dr["Quantity"] = string.Empty;
        dr["Unit"] = string.Empty;
        dr["Duration"] = string.Empty;
        dr["DurationType"] = string.Empty;
        dr["Amount"] = string.Empty;
        dt.Rows.Add(dr);
 
        dr = dt.NewRow();
        dr["RowNumber"] = 3;
        dr["Size"] = string.Empty;
        dr["Description"] = string.Empty;
        dr["Quantity"] = string.Empty;
        dr["Unit"] = string.Empty;
        dr["Duration"] = string.Empty;
        dr["DurationType"] = string.Empty;
        dr["Amount"] = string.Empty;
        dt.Rows.Add(dr);
 
        dr = dt.NewRow();
        dr["RowNumber"] = 4;
        dr["Size"] = string.Empty;
        dr["Description"] = string.Empty;
        dr["Quantity"] = string.Empty;
        dr["Unit"] = string.Empty;
        dr["Duration"] = string.Empty;
        dr["DurationType"] = string.Empty;
        dr["Amount"] = string.Empty;
        dt.Rows.Add(dr);
 
        dr = dt.NewRow();
        dr["RowNumber"] = 5;
        dr["Size"] = string.Empty;
        dr["Description"] = string.Empty;
        dr["Quantity"] = string.Empty;
        dr["Unit"] = string.Empty;
        dr["Duration"] = string.Empty;
        dr["DurationType"] = string.Empty;
        dr["Amount"] = string.Empty;
        dt.Rows.Add(dr);
        return dt;
    }
 
 
}
0
Angel Petrov
Telerik team
answered on 03 Oct 2013, 03:33 PM
Hi Brad,

Now as far as I understand the code the grid is deleted in the button click event handler. Now it will be thrown after the PageLoad event which means that the grid will already be initialized. What I would recommend is setting the Visible property of the grid to False in order to prevent it's rendering or prevent the creation of the control in the PageInit event. From the Request object you can check which control fired the postback and if it is a delete button skip the creation of the related grid. Note that you should ensure that each grid will later refer to the correct data table stored in the Session.

Regards,
Angel Petrov
Telerik
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 the blog feed now.
0
Brad
Top achievements
Rank 1
answered on 03 Oct 2013, 03:56 PM
Thank you for your response, but if i make the visible=false, the control is still on the page. And the datasource still exists. I have the code in order to delete it and resync the numbers, but now the big issue is, as soon as i delete the grid, the session number does not subtract 1 from the Session["Tables"], since this event occurs after page_load, and because of that it allows me to delete all the grids i want fine, but as soon as i try to add another one this line of code causes and Argumentoutofrange error.

void grid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
   {
       RadGrid grid = (RadGrid)sender;
       string id = grid.ID;
        
       DataTable current = (DataTable)HttpContext.Current.Session[int.Parse(id.Split(new string[] {"RadGrid"},StringSplitOptions.RemoveEmptyEntries)[0])]; // This is the line that causes that error when i click to add a grid after one has already been deleted.
       grid.DataSource = current;
   }

I have updated the ticket with the new code.

0
Brad
Top achievements
Rank 1
answered on 04 Oct 2013, 02:15 PM
Adding code here:

invoicer.cs
public class invoicer
{
    public class MyTemplate : ITemplate
    {
        private string colname;
        protected Label lControl;
 
        public MyTemplate(string cName)
        {
            colname = cName;
        }
        public void InstantiateIn(System.Web.UI.Control container)
        {
            lControl = new Label();
            lControl.ID = "Label-DurationType";
            lControl.DataBinding += new EventHandler(lControl_DataBinding);
            container.Controls.Add(lControl);
        }
 
        public void lControl_DataBinding(object sender, EventArgs e)
        {
            Label l = (Label)sender;
            GridDataItem container = (GridDataItem)l.NamingContainer;
            l.Text = ((DataRowView)container.DataItem)[colname].ToString() + "<br />";
        }
    }
 
 
    public class MyEditTemplate : IBindableTemplate
    {
        public void InstantiateIn(Control container)
        {
            GridEditableItem item = ((GridEditableItem)(container.NamingContainer));
            DropDownList drop = new DropDownList();
            drop.ID = "DurationType-DDL";
            drop.DataSource = (DataTable)GetTableForDropDown();
            drop.DataTextField = "DurationType";
            drop.DataValueField = "DurationType";
            container.Controls.Add(drop);
        }
 
        public System.Collections.Specialized.IOrderedDictionary ExtractValues(System.Web.UI.Control container)
        {
            OrderedDictionary od = new OrderedDictionary();
            od.Add("DurationType", ((DropDownList)(((GridDataItem)(container)).FindControl("DurationType-DDL"))).DataValueField);
            return od;
        }
    }
 
    void grid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        RadGrid grid = (RadGrid)sender;
        string id = grid.ID;
         
        DataTable current = (DataTable)HttpContext.Current.Session[int.Parse(id.Split(new string[] {"RadGrid"},StringSplitOptions.RemoveEmptyEntries)[0])];
        grid.DataSource = current;
    }
 
 
 
    protected void grid_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridEditableItem editItem = (GridEditableItem)e.Item;
            DropDownList ddl = (DropDownList)editItem.FindControl("DurationType-DDL");
            ddl.DataSource = (DataTable)GetTableForDropDown();
            ddl.DataTextField = "DurationType";
            ddl.DataValueField = "DurationType";
            ddl.SelectedIndex = editItem.ItemIndex;
            ddl.SelectedValue = DataBinder.Eval(editItem.DataItem, "DurationType").ToString(); // To get the selected value        
        }
    }
 
 
    public void DefineGridStructure(int i, PlaceHolder ph,Boolean bl)
    {
 
        RadGrid grid = new RadGrid();
        grid.ID = "RadGrid" + i.ToString();
        grid.Visible = bl;
        grid.NeedDataSource += new GridNeedDataSourceEventHandler(grid_NeedDataSource);
        grid.AutoGenerateEditColumn = true;
        grid.AutoGenerateDeleteColumn = true;
        grid.AllowAutomaticInserts = false;
        grid.Width = Unit.Percentage(100);
        grid.PageSize = 15;
        grid.AllowPaging = true;
        grid.AllowFilteringByColumn = false;
        grid.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
        grid.AutoGenerateColumns = false;
        grid.MasterTableView.Width = Unit.Percentage(100);
        grid.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.TopAndBottom;
        grid.AllowAutomaticDeletes = false;
        grid.AllowAutomaticUpdates = false;
        grid.ItemDataBound += new GridItemEventHandler(grid_ItemDataBound);
        grid.InsertCommand += grid_InsertCommand;
        grid.DeleteCommand += grid_DeleteCommand;
        grid.UpdateCommand += grid_UpdateCommand;
        grid.MasterTableView.DataKeyNames = new string[] { "RowNumber" };
        GridBoundColumn boundColumn = new GridBoundColumn();
        boundColumn.DataField = "RowNumber";
        boundColumn.HeaderText = "RowNumber";
        boundColumn.ReadOnly = true;
        grid.MasterTableView.Columns.Add(boundColumn);
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "Size";
        boundColumn.HeaderText = "Size";
        grid.MasterTableView.Columns.Add(boundColumn);
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "Description";
        boundColumn.HeaderText = "Description";
        grid.MasterTableView.Columns.Add(boundColumn);
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "Quantity";
        boundColumn.HeaderText = "Quantity";
        grid.MasterTableView.Columns.Add(boundColumn);
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "Unit";
        boundColumn.HeaderText = "Unit";
        grid.MasterTableView.Columns.Add(boundColumn);
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "Duration";
        boundColumn.HeaderText = "Duration";
        grid.MasterTableView.Columns.Add(boundColumn);
 
        GridTemplateColumn objGridTemplateColumn = new GridTemplateColumn();
        objGridTemplateColumn.HeaderText = "DurationType";
        objGridTemplateColumn.DataField = "DurationType";
        objGridTemplateColumn.ItemTemplate = new MyTemplate("DurationType");
        objGridTemplateColumn.EditItemTemplate = new MyEditTemplate();
        grid.MasterTableView.Columns.Add(objGridTemplateColumn);
 
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "Amount";
        boundColumn.HeaderText = "Amount";
        grid.MasterTableView.Columns.Add(boundColumn);
        grid.MasterTableView.EditMode = GridEditMode.InPlace;
 
        LinkButton lb = new LinkButton();
        lb.ID = "Show-Grid-" + i.ToString();
        lb.Text = "Show Grid " + i.ToString();
        lb.Click += new EventHandler(ShowGrid);
 
        LinkButton lbd = new LinkButton();
        lbd.ID = "Delete-Grid-" + i.ToString();
        lbd.Text = "Delete Grid " + i.ToString();
        lbd.Click += (sender, e) => DeleteGrid(sender, e, ph);
        Label lbl = new Label();
        lbl.ID = "Break-" + i.ToString();
        lbl.Text = "<br>";
 
        ph.Controls.Add(lb);
        ph.Controls.Add(lbd);
        ph.Controls.Add(grid);
        ph.Controls.Add(lbl);
    }
 
 
    public void DeleteGrid(object sender, EventArgs e, PlaceHolder ph)
    {
        LinkButton gridLink = (LinkButton)sender;
        String gridNum = gridLink.ID.ToString().Split('-').Last();
 
        System.Web.UI.Page currentPage;
        currentPage = (System.Web.UI.Page)System.Web.HttpContext.Current.Handler;
 
        RadGrid grid = (RadGrid)currentPage.FindControl("RadGrid" + gridNum);
        Label lbl = (Label)currentPage.FindControl("Break-" + gridNum);
        LinkButton lbd = (LinkButton)currentPage.FindControl("Delete-Grid-" + gridNum);
        LinkButton lb = (LinkButton)currentPage.FindControl("Show-Grid-" + gridNum);
 
        ph.Controls.Remove(grid);
        ph.Controls.Remove(lbl);
        ph.Controls.Remove(lb);
        ph.Controls.Remove(lbd);
 
        int next = Convert.ToInt32(GetSession());
        next = next - 1;
        HttpContext.Current.Session.Add("Tables", next);
        HttpContext.Current.Session.Remove(gridNum);
        ph.Controls.Clear();
        loopGrids(ph);
    }
 
 
 
    public void loopGrids(PlaceHolder ph)
    {
        System.Web.UI.Page currentPage;
        currentPage = (System.Web.UI.Page)System.Web.HttpContext.Current.Handler;
 
        string ctrlname = currentPage.Request.Params.Get("__EVENTTARGET");
        for (int i = 1; i <= (int)HttpContext.Current.Session["Tables"]; i++)
        {
            if (i == (int)HttpContext.Current.Session["Tables"])
            {
                if (ctrlname == "Button1")
                {
                    DefineGridStructure(i, ph, false);
                }
                else
                {
                    DefineGridStructure(i, ph, true);
                }
            }
            else
            {
                DefineGridStructure(i, ph, false);
            }
        }
    }
 
 
 
 
    public void ShowGrid(object sender, EventArgs e)
    {
        LinkButton gridLink = (LinkButton)sender;
        String gridNum = gridLink.ID.ToString().Split('-').Last();
 
        System.Web.UI.Page currentPage;
        currentPage = (System.Web.UI.Page)System.Web.HttpContext.Current.Handler;
 
        HttpContext.Current.Response.Write("Current gridnum: " + gridNum);
 
        RadGrid grid = (RadGrid)currentPage.FindControl("RadGrid" + gridNum);
        if (grid.Visible)
        {
            grid.Visible = false;
        }
        else
        {
            grid.Visible = true;
        }
    }
 
 
    public void grid_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        GridEditableItem editItem = e.Item as GridEditableItem;
        Hashtable newValues = new Hashtable();
        newValues["RowNumber"] = (editItem["RowNumber"].Controls[0] as TextBox).Text;
        newValues["Size"] = (editItem["Size"].Controls[0] as TextBox).Text;
        newValues["Description"] = (editItem["Description"].Controls[0] as TextBox).Text;
        newValues["Quantity"] = (editItem["Quantity"].Controls[0] as TextBox).Text;
        newValues["Unit"] = (editItem["Unit"].Controls[0] as TextBox).Text;
        newValues["Duration"] = (editItem["Duration"].Controls[0] as TextBox).Text;
        newValues["DurationType"] = (editItem.FindControl("DurationType-DDL") as DropDownList).SelectedValue;
        newValues["Amount"] = (editItem["Amount"].Controls[0] as TextBox).Text;
        DataTable dtCurrentTable = (DataTable)HttpContext.Current.Session[int.Parse(editItem.OwnerGridID.Split(new string[] { "RadGrid" }, StringSplitOptions.RemoveEmptyEntries)[0])];
 
        foreach (DictionaryEntry entry in newValues)
        {
            dtCurrentTable.Rows[e.Item.ItemIndex][entry.Key.ToString()] = entry.Value;
        }
        SaveTable(editItem.OwnerGridID);
 
    }
 
    public void grid_DeleteCommand(object sender, GridCommandEventArgs e)
    {
        GridDataItem item = e.Item as GridDataItem;
        DataTable dt = (DataTable)HttpContext.Current.Session[int.Parse(item.OwnerGridID.Split(new string[] { "RadGrid" }, StringSplitOptions.RemoveEmptyEntries)[0])];
        dt.Rows.Remove(dt.Rows[item.ItemIndex]);
        ResetRowID(dt);
        SaveTable(item.OwnerGridID);
    }
 
    public void grid_InsertCommand(object sender, GridCommandEventArgs e)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;
        GridEditManager editMan = editedItem.EditManager;
        //Set new values
        Hashtable newValues = new Hashtable();
        //The GridTableView will fill the values from all editable columns in the hash
 
        DataTable dtCurrentTable = (DataTable)HttpContext.Current.Session[int.Parse(editedItem.OwnerGridID.Split(new string[] { "RadGrid" }, StringSplitOptions.RemoveEmptyEntries)[0])];
        DataRow dr = null;
        int count = dtCurrentTable.Rows.Count;
        count++;
        dr = dtCurrentTable.NewRow();
        dr["RowNumber"] = count;
        newValues["RowNumber"] = count;
        newValues["Size"] = (editedItem["Size"].Controls[0] as TextBox).Text;
        newValues["Description"] = (editedItem["Description"].Controls[0] as TextBox).Text;
        newValues["Quantity"] = (editedItem["Quantity"].Controls[0] as TextBox).Text;
        newValues["Unit"] = (editedItem["Unit"].Controls[0] as TextBox).Text;
        newValues["Duration"] = (editedItem["Duration"].Controls[0] as TextBox).Text;
        newValues["DurationType"] = (editedItem.FindControl("DurationType-DDL") as DropDownList).SelectedValue;
        newValues["Amount"] = (editedItem["Amount"].Controls[0] as TextBox).Text;
        foreach (DictionaryEntry entry in newValues)
        {
            dr[entry.Key.ToString()] = entry.Value;
        }
        dtCurrentTable.Rows.Add(dr);
        SaveTable(editedItem.OwnerGridID);
    }
 
 
 
    public string GetSession()
    {
        string SID = HttpContext.Current.Session["Tables"].ToString();
        return SID;
    }
 
    public void SaveTable(string id)
    {
        DataTable dtCurrentTable = (DataTable)HttpContext.Current.Session[int.Parse(id.Split(new string[] { "RadGrid" }, StringSplitOptions.RemoveEmptyEntries)[0])];
        HttpContext.Current.Session.Add(id.Split(new string[] { "RadGrid" }, StringSplitOptions.RemoveEmptyEntries)[0], dtCurrentTable);
    }
 
    public DataTable GetTable()
    {
        //
        // Here we create a DataTable with a few columns.
        //
        // Create Datatable to store all colums
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("Size", typeof(string)));
        dt.Columns.Add(new DataColumn("Description", typeof(string)));
        dt.Columns.Add(new DataColumn("Quantity", typeof(string)));
        dt.Columns.Add(new DataColumn("Unit", typeof(string)));
        dt.Columns.Add(new DataColumn("Duration", typeof(string)));
        dt.Columns.Add(new DataColumn("DurationType", typeof(string)));
        dt.Columns.Add(new DataColumn("Amount", typeof(string)));
        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["Size"] = string.Empty;
        dr["Description"] = string.Empty;
        dr["Quantity"] = string.Empty;
        dr["Unit"] = string.Empty;
        dr["Duration"] = string.Empty;
        dr["DurationType"] = string.Empty;
        dr["Amount"] = string.Empty;
        dt.Rows.Add(dr);
 
        dr = dt.NewRow();
        dr["RowNumber"] = 2;
        dr["Size"] = string.Empty;
        dr["Description"] = string.Empty;
        dr["Quantity"] = string.Empty;
        dr["Unit"] = string.Empty;
        dr["Duration"] = string.Empty;
        dr["DurationType"] = string.Empty;
        dr["Amount"] = string.Empty;
        dt.Rows.Add(dr);
 
        dr = dt.NewRow();
        dr["RowNumber"] = 3;
        dr["Size"] = string.Empty;
        dr["Description"] = string.Empty;
        dr["Quantity"] = string.Empty;
        dr["Unit"] = string.Empty;
        dr["Duration"] = string.Empty;
        dr["DurationType"] = string.Empty;
        dr["Amount"] = string.Empty;
        dt.Rows.Add(dr);
 
        dr = dt.NewRow();
        dr["RowNumber"] = 4;
        dr["Size"] = string.Empty;
        dr["Description"] = string.Empty;
        dr["Quantity"] = string.Empty;
        dr["Unit"] = string.Empty;
        dr["Duration"] = string.Empty;
        dr["DurationType"] = string.Empty;
        dr["Amount"] = string.Empty;
        dt.Rows.Add(dr);
 
        dr = dt.NewRow();
        dr["RowNumber"] = 5;
        dr["Size"] = string.Empty;
        dr["Description"] = string.Empty;
        dr["Quantity"] = string.Empty;
        dr["Unit"] = string.Empty;
        dr["Duration"] = string.Empty;
        dr["DurationType"] = string.Empty;
        dr["Amount"] = string.Empty;
        dt.Rows.Add(dr);
        return dt;
    }
 
 
    static DataTable GetTableForDropDown()
    {
        //
        // Here we create a DataTable with a few columns.
        //
        // Create Datatable to store all colums
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("DurationType", typeof(string)));
 
        dr = dt.NewRow();
        dr["DurationType"] = "Hours";
        dt.Rows.Add(dr);
 
        dr = dt.NewRow();
        dr["DurationType"] = "Days";
        dt.Rows.Add(dr);
 
        dr = dt.NewRow();
        dr["DurationType"] = "Weeks";
        dt.Rows.Add(dr);
 
        dr = dt.NewRow();
        dr["DurationType"] = "Months";
        dt.Rows.Add(dr);
        return dt;
    }
 
    private void ResetRowID(DataTable dt)
    {
        int rowNumber = 1;
 
        if (dt.Rows.Count > 0)
        {
            foreach (DataRow row in dt.Rows)
            {
                row[0] = rowNumber;
                rowNumber++;
            }
        }
    }
}


Default.aspx.cs
public partial class _Default : System.Web.UI.Page
{
 
    invoicer inv = new invoicer();
   
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
 
    protected void Page_Init(object sender, EventArgs e)
    {
        string ctrlname = this.Page.Request.Params.Get("__EVENTTARGET");
 
        if (Session["Tables"] == null)
        {
            Session.Add("Tables", 1);
            DataTable dt = inv.GetTable();
 
            Session.Add(Session["Tables"].ToString(), dt);
            inv.DefineGridStructure(1, PlaceHolder2, true);
        }
        if (IsPostBack)
        {
            int next = Convert.ToInt32(inv.GetSession());
            PlaceHolder2.Controls.Clear();
            inv.loopGrids(PlaceHolder2);
             
            if (ctrlname == "Button1")
            {
                next = next + 1;
                Session.Add("Tables",next);
                Response.Write("Button getSession: " + inv.GetSession());
                DataTable dt = inv.GetTable();
                Session.Add(Session["Tables"].ToString(), dt);
                inv.DefineGridStructure(next, PlaceHolder2,true);
            }
 
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
 
    }
     
}
0
Angel Petrov
Telerik team
answered on 08 Oct 2013, 12:52 PM
Hello Brad,

I have already provided a suggestion for the problem in the official support ticket you have opened regarding this matter. In order to help other community members resolve such problems I will copy the reply here.

"Now what I see as problematic in the provided code is the deletion of the grids in the button click event handler. The click event is fired too late for changing the controls structure and modifying it may cause unexpected behavior. What I would suggest is creating the exact same structure but setting the visible property for the grids you want to delete to false. Additionally you may set the session variables which store the data tables for these grids to null in order to optimize the Session usage. Note that if you follow another approach and more precisely removing the controls programmatically, if the controls structure is not recreated accordingly the ViewState will break. That said setting the Visible property to false seems like the easier solution as this would not require a re-indexing of the data sources."

Regards,
Angel Petrov
Telerik
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 the blog feed now.
0
Brad
Top achievements
Rank 1
answered on 08 Oct 2013, 02:05 PM
You're right that does work well, but the only issue i have with it is that the numbers don't stay in sync.

So if i have 4 grids.

Grid1
Grid2
Grid3
Grid4

And i click to delete Grid3 and set the visible=false, then i still end up with:

Grid1
Grid2
Grid4


Below is the following code i use for the delete method:

public void DeleteGrid(object sender, EventArgs e, PlaceHolder ph)
 {
     LinkButton gridLink = (LinkButton)sender;
     String gridNum = gridLink.ID.ToString().Split('-').Last();
 
     System.Web.UI.Page currentPage;
     currentPage = (System.Web.UI.Page)System.Web.HttpContext.Current.Handler;
 
     RadGrid grid = (RadGrid)currentPage.FindControl("RadGrid" + gridNum);
     Label lbl = (Label)currentPage.FindControl("Break-" + gridNum);
     LinkButton lbd = (LinkButton)currentPage.FindControl("Delete-Grid-" + gridNum);
     LinkButton lb = (LinkButton)currentPage.FindControl("Show-Grid-" + gridNum);
 
     grid.Visible = false;
     lbl.Visible = false;
     lbd.Visible = false;
     lb.Visible = false;
 
 
     HttpContext.Current.Session.Add(gridNum, null);
 
 }
0
Angel Petrov
Telerik team
answered on 11 Oct 2013, 11:42 AM
Hello Brad,

I am a little confused by the problem. Actually when you click to delete grid 3 it becomes invisible and only grids 1,2 and 4 are visible. That seems a correct behavior to me. Is this not the actual requirement? Am I missing something?

Regards,
Angel Petrov
Telerik
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 the blog feed now.
0
Brad
Top achievements
Rank 1
answered on 11 Oct 2013, 01:05 PM
Correct, that is fine, but i wanted the numbers to resync:

So if i had 4 grids

Grid1
Grid2
Grid3
Grid4

and i delete grid3

instead of me ending up with

Grid1
Grid2
Grid4

I wanted to end up with

Grid1
Grid2
Grid3

0
Angel Petrov
Telerik team
answered on 16 Oct 2013, 11:37 AM
Hi Brad,

The easiest way for achieving this is to include another counter which to count only the visible grids on the page. You can use the value of this counter and assign it to the link buttons and labels text. That way in reality the grids will be arranged:

Grid1
Grid2
Grid4

But the user will see

Grid1
Grid2
Grid3


Regards,
Angel Petrov
Telerik
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 the blog feed now.
Tags
Grid
Asked by
Brad
Top achievements
Rank 1
Answers by
Brad
Top achievements
Rank 1
Angel Petrov
Telerik team
Share this question
or