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

Client side batch update with insert option

2 Answers 52 Views
Grid
This is a migrated thread and some comments may be shown as answers.
neebs
Top achievements
Rank 2
neebs asked on 24 Aug 2010, 04:32 PM
Hello,

I am trying to implement a solution similar to the one shown in this demo:

http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/clienteditbatchupdates/defaultcs.aspx

In my scenario, the columns are variable. (I am implementing a form to enter index fund monthly returns, and the index funds will grow over time. So each row represents a month ending date and each column represents an index fund. The cell intersection represents the rate of return for an index fund for a period.) I have successfully implemented the edit capability as provided in the example, with modifications due to the fact that the columns defined dynamically. 

Now I need to address the insert issue. Specifically, when the "Add a new row" button is clicked, I need to change the styles of the controls in each cell of the insert row such that the label does not display and the edit controls (all are textboxes) display. I have not yet figured out how to accomplish this. I can trap the ItemCommand "InitInsert" in the code-behind, but what to do then? Any help would be appreciated.

Thanks, Steve

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 25 Aug 2010, 07:20 AM
Hello Steve,

The following code shows how to hide Label control placed in EditItemTemplate of GridTemplateColumn, when clicking the "Add New Record" button. Please make neccessary changes to meet your requirement.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
   {
      if (e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted)
       {
           GridEditFormInsertItem insertItem = (GridEditFormInsertItem)e.Item;
           Label lb = (Label)insertItem.FindControl("Label1"); //access the Label inside EditItemTemplate using its id
           lb.Visible = false;//hide the label
       }
   }

Thanks,
Princy.
0
neebs
Top achievements
Rank 2
answered on 26 Aug 2010, 03:50 PM
Actually, I found a way to do this, but it differes. I'll try your approach however. Here are the pertinent code elements:

protected void RadGridIndexReturns_CreateColumnEditor(object sender, GridCreateColumnEditorEventArgs e)
{
    if (e.Column is GridTemplateColumn)
    {
        GridTemplateColumn templateColumn = (GridTemplateColumn) e.Column;
        if (templateColumn.ItemTemplate is DateTimeItemTemplate)
        {
            DateTimeItemTemplate dt = (DateTimeItemTemplate) templateColumn.ItemTemplate;
            dt.InEditMode = true;
        }
        else if (templateColumn.ItemTemplate is PercentItemTemplate)
        {
            PercentItemTemplate pt = (PercentItemTemplate) templateColumn.ItemTemplate;
            pt.InEditMode = true;
        }
    }
}
public class DateTimeItemTemplate : ITemplate
{
    private Panel panel0;
    private TextBox textValue;
    private Label labelValue;
    public string ColumnName { get; set; }
    public string HeaderName { get; set; }
    public DateTimeItemTemplate(string headername, string columnname)
    {
        ColumnName = columnname;
        HeaderName = headername;
    }
    public bool InEditMode
    {
        set
        {
            switch (value)
            {
                case true:
                    textValue.Style.Add("display", "inline");
                    break;
                case false:
                    textValue.Style.Remove("display");
                    break;
            }
        }
    }
    public void InstantiateIn(Control container)
    {
        ((GridTableCell)container).Wrap = false;
        panel0 = new Panel();
        //panel0.CssClass = "IndexFundReturnAsOfDate";
        textValue = new TextBox {ID = "tb_" + ColumnName};
        textValue.DataBinding += textValue_DataBinding;
        textValue.Enabled = true;
        //textValue.Attributes.Add("OnKeyDown", "return TextBoxChanged(this);");
        labelValue = new Label { ID = "lbl_" + ColumnName };
        labelValue.DataBinding += lblValue_DataBinding;
        panel0.Controls.Add(textValue);
        panel0.Controls.Add(labelValue);
        container.Controls.Add(panel0);
    }
    public void lblValue_DataBinding(object sender, EventArgs e)
    {
        Label lbl = (Label)sender;
        GridDataItem container = (GridDataItem)lbl.NamingContainer;
        try
        {
            labelValue.Text = String.Format("{0:MM/dd/yyyy}",
                               Convert.ToDateTime(
                                   ((DataRowView)(container.DataItem))[ColumnName]));
        }
        catch (Exception)
        {
            labelValue.Text = "";
        }
    }
    public void textValue_DataBinding(object sender, EventArgs e)
    {
        TextBox tBox = (TextBox)sender;
        GridDataItem container = (GridDataItem)tBox.NamingContainer;
        try
        {
            textValue.Text = String.Format("{0:MM/dd/yyyy}",
                               Convert.ToDateTime(
                                   ((DataRowView)(container.DataItem))[ColumnName]));
        }
        catch (Exception)
        {
            textValue.Text = "";
        }
        textValue.CssClass = "textBoxIndexFundReturnAsOfDateNoDisplay";
    }
}
public class PercentItemTemplate : ITemplate
{
    private Panel panel0;
    private TextBox textValue;
    private Label labelValue;
    public string ColumnName { get; set; }
    public string HeaderName { get; set; }
    public int ColumnNumber { get; set; }
    public PercentItemTemplate(string headername, string columnname, int columnnumber)
    {
        ColumnName = columnname;
        HeaderName = headername;
        ColumnNumber = columnnumber;
    }
    public bool InEditMode
    {
        set
        {
            switch (value)
            {
                case true:
                    textValue.Style.Add("display", "inline");
                    break;
                case false:
                    textValue.Style.Remove("display");
                    break;
            }
        }
    }
    public void InstantiateIn(Control container)
    {
        ((GridTableCell)container).Wrap = false;
        panel0 = new Panel();
        //panel0.CssClass = "IndexFundReturnAsOfDate";
        textValue = new TextBox {ID = "tb_" + ColumnName};
        textValue.DataBinding += textValue_DataBinding;
        textValue.Enabled = true;
        //textValue.Attributes.Add("OnKeyDown", "return TextBoxChanged(this);");
        labelValue = new Label { ID = "lbl_" + ColumnName };
        labelValue.DataBinding += lblValue_DataBinding;
        panel0.Controls.Add(textValue);
        panel0.Controls.Add(labelValue);
        container.Controls.Add(panel0);
    }
    public void lblValue_DataBinding(object sender, EventArgs e)
    {
        Label lbl = (Label)sender;
        GridDataItem container = (GridDataItem)lbl.NamingContainer;
        try
        {
            labelValue.Text = String.Format("{0:P4}",
                               Convert.ToDouble(
                                   ((DataRowView)(container.DataItem))[ColumnName]));
        }
        catch (Exception)
        {
            labelValue.Text = "";
        }
    }
    public void textValue_DataBinding(object sender, EventArgs e)
    {
        TextBox tBox = (TextBox)sender;
        GridDataItem container = (GridDataItem)tBox.NamingContainer;
        try
        {
            textValue.Text = String.Format("{0:P4}",
                               Convert.ToDouble(
                                   ((DataRowView)(container.DataItem))[ColumnName]));
        }
        catch (Exception)
        {
            textValue.Text = "";
        }
        //textValue.Style = "display:none";
        textValue.CssClass = "textBoxIndexFundReturnGrossReturnNoDisplay";
    }
}
protected void RadGridIndexReturns_CreateColumnEditor(object sender, GridCreateColumnEditorEventArgs e)
{
    if (e.Column is GridTemplateColumn)
    {
        GridTemplateColumn templateColumn = (GridTemplateColumn) e.Column;
        if (templateColumn.ItemTemplate is DateTimeItemTemplate)
        {
            DateTimeItemTemplate dt = (DateTimeItemTemplate) templateColumn.ItemTemplate;
            dt.InEditMode = true;
        }
        else if (templateColumn.ItemTemplate is PercentItemTemplate)
        {
            PercentItemTemplate pt = (PercentItemTemplate) templateColumn.ItemTemplate;
            pt.InEditMode = true;
        }
    }
}
protected void ButtonUploadExcel_Click(object sender, EventArgs e)
{
    RadWindowManagerIndexReturns.Windows[0].NavigateUrl = "UploadIndexReturns.aspx";
    RadWindowManagerIndexReturns.Windows[0].VisibleOnPageLoad = true;
}
protected void RadAjaxManagerIndexFundReturns_AjaxRequest(object sender, AjaxRequestEventArgs e)
{
    if (e.Argument == "Rebind")
    {
        RebindGrid();
    }
}
protected void RadGridIndexReturns_ItemCommand(object source, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.RebindGridCommandName)
    {
        RebindGrid();
    }
}
Tags
Grid
Asked by
neebs
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 2
neebs
Top achievements
Rank 2
Share this question
or