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

[Solved] Grid edit command

4 Answers 205 Views
Grid
This is a migrated thread and some comments may be shown as answers.
tali
Top achievements
Rank 1
tali asked on 17 May 2009, 01:24 PM
Hi

I have a grid with self hierarchy. I am creating the grid and columns programatically in the Page_Init event handler (method createGrid()). I set the AutoGenerateEditColumn property to 'true'. When creating the columns, i created a GridTemplateColumn using my own custom template- 'MyTemplate' and edit template- 'MyEditTemplate'. MyTemplate contains only a Label and MyEditTemplate contains a TextBox (both asp controls). The grid is rendered perfectly but when i click the "edit" link button I'm getting a postBack (no ajax behaviour) and the item is not opened for editing...

i attached some of my code - createGrid() method and the item templates

Why doesn't the edit command work?..

private void createGrid()  
    {  
 
 
        grdRuns.ID = "grdRuns";  
 
        grdRuns.ColumnCreated -= new GridColumnCreatedEventHandler(grdRuns_ColumnCreated);  
        grdRuns.ItemCreated -= new GridItemEventHandler(grdRuns_ItemCreated);  
        grdRuns.ItemDataBound -= new GridItemEventHandler(grdRuns_ItemDataBound);  
 
        grdRuns.AutoGenerateColumns = false;  
        grdRuns.HorizontalAlign = HorizontalAlign.Right;  
        grdRuns.MasterTableView.HorizontalAlign = HorizontalAlign.Right;  
        grdRuns.MasterTableView.Dir = GridTableTextDirection.RTL;  
        grdRuns.EnableEmbeddedSkins = false;  
        grdRuns.Skin = "Portal";  
        grdRuns.ClientSettings.Scrolling.UseStaticHeaders = true;  
        grdRuns.ClientSettings.Scrolling.AllowScroll = true;  
        grdRuns.MasterTableView.HierarchyDefaultExpanded = true;  
        grdRuns.MasterTableView.TableLayout = GridTableLayout.Fixed;  
        grdRuns.AutoGenerateEditColumn = true;  
        grdRuns.AllowAutomaticUpdates = true;  
 
        grdRuns.MasterTableView.EditMode = GridEditMode.EditForms;  
                  
        grdRuns.ColumnCreated += new GridColumnCreatedEventHandler(grdRuns_ColumnCreated);  
        grdRuns.ItemCreated += new GridItemEventHandler(grdRuns_ItemCreated);  
        grdRuns.ItemDataBound += new GridItemEventHandler(grdRuns_ItemDataBound);  
          
 
        NostroDataSet.FieldsForGridDataTable dtFields = BLManager.GetFieldsForGrid(int.Parse(ddlRuns.SelectedValue));  
 
        foreach (NostroDataSet.FieldsForGridRow row in dtFields.Rows)  
        {  
 
            if (row.IsEditable)  
            {  
                GridTemplateColumn templateColumn = new GridTemplateColumn();  
                string templateColumnName = row.FieldName;  
 
                templateColumn.ItemTemplate = new MyTemplate(templateColumnName);  
                templateColumn.EditItemTemplate = new MyEditTemplate(templateColumnName);  
                templateColumn.HeaderText = row.ScreenName;  
                templateColumn.Display = row.Display;  
                templateColumn.HeaderStyle.Width = Unit.Pixel(row.Witdh);  
                templateColumn.ItemStyle.Width = Unit.Pixel(row.Witdh);  
                  
                grdRuns.MasterTableView.Columns.Add(templateColumn);  
                  
            }  
 
            else 
            {  
                GridBoundColumn clm = new GridBoundColumn();  
                clm.DataField = row.FieldName;  
                clm.HeaderText = row.ScreenName;  
                clm.UniqueName = row.FieldName;  
 
                clm.HeaderStyle.Wrap = false;  
                clm.ItemStyle.Wrap = false;  
 
                clm.HeaderStyle.Width = Unit.Pixel(row.Witdh);  
                clm.ItemStyle.Width = Unit.Pixel(row.Witdh);  
 
                grdRuns.MasterTableView.Columns.Add(clm);  
 
                clm.Display = row.Display;  
            }  
        }  
 
 
        grdRuns.ClientSettings.AllowExpandCollapse = true;  
        grdRuns.MasterTableView.HierarchyLoadMode = GridChildLoadMode.Client;  
        grdRuns.MasterTableView.HierarchyDefaultExpanded = false;  
        grdRuns.MasterTableView.DataKeyNames = new string[] { "PortfolioID""ParentID" };  
        grdRuns.MasterTableView.SelfHierarchySettings.ParentKeyName = "ParentID";  
        grdRuns.MasterTableView.SelfHierarchySettings.KeyName = "PortfolioID";  
 
        grdRuns.EditCommand += new GridCommandEventHandler(grdRuns_EditCommand);  
        divGrid.Controls.Add(grdRuns);  
 
        grdRuns.DataSource = ApproveRunData;  
            } 
private class MyTemplate : ITemplate  
    {  
        //protected LiteralControl lControl;  
        protected Label label;  
        private string colname;  
 
        public MyTemplate(string cName)  
        {  
            colname = cName;  
        }  
 
        public void InstantiateIn(System.Web.UI.Control container)  
        {  
            //lControl = new LiteralControl();  
            //lControl.ID = "lControl";  
            //lControl.DataBinding += new EventHandler(lControl_DataBinding);  
 
            label = new Label();  
            label.ID = "templateColumnLabel";  
            label.DataBinding += new EventHandler(label_DataBinding);  
 
            container.Controls.Add(label);  
        }  
 
        void label_DataBinding(object sender, EventArgs e)  
        {  
            Label lbl = (Label)sender;  
            GridDataItem container = (GridDataItem)lbl.NamingContainer;  
 
            string sValue = ((DataRowView)container.DataItem)[colname].ToString();  
            double dValue = -1;  
            if (double.TryParse(sValue, out dValue))  
            {  
                string sTemp = String.Format("{0:n}", dValue);      //add commas as thousand separator  
                lbl.Text = sTemp.Substring(0, sTemp.IndexOf('.'));  //don't display digits after decimal point.  
                lbl.Style.Add("text-align""left");                    //make the minus sign ('-') appear on the left of the number.  
 
            }  
            else lbl.Text = sValue;  
            if (sValue == "") lbl.Text = " ";  
        }  
                      
        public void lControl_DataBinding(object sender, EventArgs e)  
        {  
            LiteralControl l = (LiteralControl)sender;  
            GridDataItem container = (GridDataItem)l.NamingContainer;  
 
            string sValue = ((DataRowView)container.DataItem)[colname].ToString();  
            double dValue = -1;  
            if (double.TryParse(sValue, out dValue))  
            {  
                string sTemp = String.Format("{0:n}", dValue);      //add commas as thousand separator  
                l.Text = sTemp.Substring(0, sTemp.IndexOf('.'));    //don't display digits after decimal point.  
                //l.Style.Add("text-align", "left");                    //make the minus sign ('-') appear on the left of the number.  
 
            }  
            else l.Text = sValue + "<br/>";  
 
            if (sValue == "") l.Text = "&nbsp;";  
 
 
            //l.Text = ((DataRowView)container.DataItem)[colname].ToString() + "<br />";  
        }  
    }  
 
    private class MyEditTemplate : ITemplate  
    {  
        protected LiteralControl lControl;  
        protected TextBox textBox;  
          
        private string colname;  
 
        public MyEditTemplate(string cName)  
        {  
            colname = cName;  
        }  
 
        public void InstantiateIn(System.Web.UI.Control container)  
        {  
            lControl = new LiteralControl();  
            lControl.ID = "lControl";  
            lControl.DataBinding += new EventHandler(lControl_DataBinding);  
            textBox = new TextBox();  
            textBox.ID = "templateColumnTextBox";  
            textBox.CssClass = "fieldUpdatePortal";  
            textBox.DataBinding += new EventHandler(textBox_DataBinding);  
              
            container.Controls.Add(textBox);  
        }  
 
        void textBox_DataBinding(object sender, EventArgs e)  
        {  
            TextBox tBox = (TextBox)sender;  
            GridDataItem container = (GridDataItem)tBox.NamingContainer;  
 
            string sValue = ((DataRowView)container.DataItem)[colname].ToString();  
            double dValue = -1;  
            if (double.TryParse(sValue, out dValue))  
            {  
                string sTemp = String.Format("{0:n}", dValue);      //add commas as thousand separator  
                tBox.Text = sTemp.Substring(0, sTemp.IndexOf('.')); //don't display digits after decimal point.  
                tBox.Style.Add("direction""ltr");                 //make the minus sign ('-') appear on the left of the number.  
 
            }  
            else tBox.Text = sValue;  
 
        }  
          
        public void lControl_DataBinding(object sender, EventArgs e)  
        {  
            LiteralControl l = (LiteralControl)sender;  
            GridDataItem container = (GridDataItem)l.NamingContainer;  
            l.Text = ((DataRowView)container.DataItem)[colname].ToString() + "<br />";  
 
 
        }  
    }   
 

Thank you

tali.

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 18 May 2009, 08:10 AM
Hi Tali,

In the code given above you are adding the Template column for every dataset rows. Could you please clarify that portion of the code?

Princy.
0
tali
Top achievements
Rank 1
answered on 18 May 2009, 08:25 AM
Hi Princy

I am creating the grid dynamically so the dataset dtFields is not the data source for the grid. It contains the columns names, widths and a boolean 'IsEditable'  that specifies if the value in this column could be edited on the edit command. I'm looping through the rows- each row represents a column to be added to the MasterTableView. I'm adding each column in the following way: If the column could be edited, I am adding a TemplateColumn. If not- a boundColumn.

I hope this clarifies the "foreach" part.

I bind the data source to the grid only at the last row of the method (grdRuns.DataSource = ApproveRunData;)

Tali
0
Princy
Top achievements
Rank 2
answered on 18 May 2009, 10:39 AM
Hi Tali,

You are not calling the DataBind() method after setting the DataSource for the Grid. Try calling the DataBind() method and see whether it is working.

 
grdRuns.DataSource = ApproveRunData;   
grdRuns.DataBind(); 

Princy



0
tali
Top achievements
Rank 1
answered on 18 May 2009, 12:16 PM
Hi

The DataBind() method didn't change anything... I decided not to use the command button, and always allow the user to edit the grid data.


Thanks anyway,
Tali
Tags
Grid
Asked by
tali
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
tali
Top achievements
Rank 1
Share this question
or