Ticket T692348
Visible to All Users

ASPxGridView - How to get a value from the controls inside the Data Item template

created 6 years ago

[DevExpress Support Team: CLONED FROM Q252956: ITemplate - different templates]
Hi,
I have similar issue, but using the GridViewDataItemTemplateContainer. How do you iterate every rows and get the value? The new control returns null for me. I really appreciate your help on this.

Thanks,
Phong

public class CustomTemplate : ITemplate
    {
        public void InstantiateIn(Control container)
        {
            AddControls(container);
        }

protected void AddControls(Control container)
        {
            GridViewDataItemTemplateContainer editingContainer = (GridViewDataItemTemplateContainer)container;
            ASPxGridView gv = (ASPxGridView)editingContainer.Grid;
            int key = Convert.ToInt16(editingContainer.KeyValue);

string dataType = Convert.ToString(gv.GetRowValuesByKeyValue(key, "DataType"));

switch (dataType.ToLower())
            {
                case "text":
                    AddTextBox(container);
                    break;
                case "date":
                    AddDateEdit(container);
                    break;
                case "boolean":
                    AddCheckBox(container);
                    break;
                case "integer":
                    AddSpinEdit(container);
                    break;
                case "decimal":
                    AddTextBox(container);
                    break;
            }
        }

protected void AddTextBox(Control container)
        {
            ASPxTextBox tb = new ASPxTextBox();
            tb.ID = "edValue";
            tb.ValidationSettings.ValidateOnLeave = true;
            container.Controls.Add(tb);
        }

protected void AddDateEdit(Control container)
        {
            ASPxDateEdit de = new ASPxDateEdit();
            de.ID = "edValue";
            container.Controls.Add(de);
        }

protected void AddCheckBox(Control container)
        {
            ASPxCheckBox cb = new ASPxCheckBox();
            cb.ID = "edValue";
            cb.ValueUnchecked = false;
            cb.ValueChecked = true;
            container.Controls.Add(cb);
        }

protected void AddSpinEdit(Control container)
        {
            ASPxSpinEdit tb = new ASPxSpinEdit();
            tb.ID = "edValue";
            tb.ValidationSettings.ValidateOnLeave = true;
            container.Controls.Add(tb);
        }
    }

protected void gvItemAttribute_CellEditorInitialize(object sender, EventArgs e)
        {
            ASPxGridView gvItemAttribute = (ASPxGridView)sender;
            ((GridViewDataColumn)gvItemAttribute.Columns["AttributeValue"]).DataItemTemplate = new CustomTemplate();
        }

protected void btnSaveChanges_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i <= (gvItemAttribute.VisibleRowCount - 1); i++)
                {
                    var edDatatype = gvItemAttribute.GetRowValues(i, "DataType").ToString();
                    var edValue = gvItemAttribute.GetRowValues(i, "edValue");   //always return null
                }
            }
            catch (Exception ex)
            {
            }
        }

Show previous comments (1)

    <%@ Register Assembly="DevExpress.Web.v18.1, Version=18.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web" TagPrefix="dx" %>
    <asp:Content ID="Content2" ContentPlaceHolderID="cphMain" runat="server">
        <dx:ASPxGridView ID="gvItemAttribute" runat="server" OnInit="gvItemAttribute_CellEditorInitialize" KeyFieldName="AttributeNameId" Width="700">
            <SettingsEditing Mode="EditForm" />
            <Columns>
                <dx:GridViewDataTextColumn FieldName="DataType" VisibleIndex="1" />
                <dx:GridViewDataTextColumn FieldName="AttributeValue" VisibleIndex="3" />
            </Columns>
        </dx:ASPxGridView>
        <div style="padding-bottom: 20px;">
            <dx:ASPxButton ID="btnSaveChanges" runat="server" Text="Save Changes" OnClick="btnSaveChanges_Click"></dx:ASPxButton>
        </div>
        <asp:SqlDataSource ID="dsAttributeDataType" runat="server" ConnectionString="<%$ ConnectionStrings:Product4dbConnectionString %>" SelectCommand="SELECT Id, Name FROM [tblPDItemAttributeDataType]"></asp:SqlDataSource>
    </asp:Content>

    public partial class DynamicControls : AjaxAppPage
        {
            private DynamicAttribute _Entity = null;
            protected void Page_Load(object sender, EventArgs e)
            {
                if (_Entity == null) _Entity = new DynamicAttribute(connection_string);
                gvItemAttribute.DataSource = _Entity.getDynamicAttributes(0);
                gvItemAttribute.DataBind();
            }
            protected void btnSaveChanges_Click(object sender, EventArgs e)
            {
                try
                {
                    for (int i = 0; i <= (gvItemAttribute.VisibleRowCount - 1); i++)
                    {
                        var datatype = gvItemAttribute.GetRowValues(i, "DataType").ToString();
                        var attribute_value = gvItemAttribute.GetRowValuesByKeyValue(i, "AttributeValue");
                    }
                }
                catch (Exception ex)
                {
                }
            }
            protected void gvItemAttribute_CellEditorInitialize(object sender, EventArgs e)
            {
                ASPxGridView gvItemAttribute = (ASPxGridView)sender;
                ((GridViewDataColumn)gvItemAttribute.Columns["AttributeValue"]).DataItemTemplate = new CustomTemplate();
            }
        }

    public class CustomTemplate : ITemplate
        {
            public void InstantiateIn(Control container)
            {
                AddControls(container);
            }
            protected void AddControls(Control container)
            {
                GridViewDataItemTemplateContainer editingContainer = (GridViewDataItemTemplateContainer)container;
                ASPxGridView gv = (ASPxGridView)editingContainer.Grid;
                int key = Convert.ToInt16(editingContainer.KeyValue);
                string dataType = Convert.ToString(gv.GetRowValuesByKeyValue(key, "DataType"));
                switch (dataType.ToLower())
                {
                    case "text":
                        AddTextBox(container);
                        break;
                    case "date":
                        AddDateEdit(container);
                        break;
                    case "boolean":
                        AddCheckBox(container);
                        break;
                    case "integer":
                        AddSpinEdit(container);
                        break;
                    case "decimal":
                        AddTextBox(container);
                        break;
                }
            }
            protected void AddTextBox(Control container)
            {
                ASPxTextBox tb = new ASPxTextBox();
                tb.ID = "edTextBoxValue";
                container.Controls.Add(tb);
            }
            protected void AddDateEdit(Control container)
            {
                ASPxDateEdit de = new ASPxDateEdit();
                de.ID = "edDateEditValue";
                container.Controls.Add(de);
            }
            protected void AddCheckBox(Control container)
            {
                ASPxCheckBox cb = new ASPxCheckBox();
                cb.ID = "edCheckBoxValue";
                cb.ValueUnchecked = false;
                cb.ValueChecked = true;
                container.Controls.Add(cb);
            }
            protected void AddSpinEdit(Control container)
            {
                ASPxSpinEdit tb = new ASPxSpinEdit();
                tb.ID = "edSpinEditValue";
                container.Controls.Add(tb);
            }
        }

      Thanks for your quick reply. I am trying to iterate thru the gridview and get the values of each fields.

      for (int i = 0; i <= (gvItemAttribute.VisibleRowCount - 1); i++)
                      {
                          var datatype = gvItemAttribute.GetRowValues(i, "DataType").ToString();  //works: return value
                          var attribute_value = gvItemAttribute.GetRowValuesByKeyValue(i, "AttributeValue");   //error: return null
                      }

        I have tried BatchEditing, but dynamic controls could not load on init

        Answers approved by DevExpress Support

        created 6 years ago (modified 6 years ago)

        Hello,

        The problem occurs because you're using the GetRowValuesByKeyValue method to get the values of editors located inside the "AttributeValue" column's DataItemTemplate. Note that this method can be used only to get values of grid cells, not template editors' values.
        To get a reference to the control inside the required ASPxGridView's template (the column's DataItemTemplate) you can use the ASPxGridView.FindRowCellTemplateControl method: see the How to sort the ASPxGridView by the value of the ASPxTextBox control in the UnboundColumn's DataItemTemplate example illustrating how to accomplish this task. Then, obtain the value of each obtained control by using its Value property.
        Let us know if this helps.

          Other Answers

          created 6 years ago

          var datatype = gvItemAttribute.GetRowValues(i, "DataType").ToString();
                              switch (datatype)
                              {
                                  case "text":
                                      attribute_value = (gvItemAttribute.FindRowCellTemplateControl(i, gvItemAttribute.Columns["AttributeValue"] as GridViewDataColumn, "edValue") as ASPxTextBox).Text;
                                      break;
                                  case "date":
                                      attribute_value = (gvItemAttribute.FindRowCellTemplateControl(i, gvItemAttribute.Columns["AttributeValue"] as GridViewDataColumn, "edValue") as ASPxDateEdit).Text;
                                      break;
                                  case "boolean":
                                      attribute_value = (gvItemAttribute.FindRowCellTemplateControl(i, gvItemAttribute.Columns["AttributeValue"] as GridViewDataColumn, "edValue") as ASPxCheckBox).Checked;
                                      break;
                                  case "decimal":
                                      attribute_value = (gvItemAttribute.FindRowCellTemplateControl(i, gvItemAttribute.Columns["AttributeValue"] as GridViewDataColumn, "edValue") as ASPxSpinEdit).Text;
                                      break;
                                  case "integer":
                                      attribute_value = (gvItemAttribute.FindRowCellTemplateControl(i, gvItemAttribute.Columns["AttributeValue"] as GridViewDataColumn, "edValue") as ASPxSpinEdit).Text;
                                      break;
                                  default:
                                      attribute_value = (gvItemAttribute.FindRowCellTemplateControl(i, gvItemAttribute.Columns["AttributeValue"] as GridViewDataColumn, "edValue") as ASPxTextBox).Text;
                                      break;
                              }

            Show previous comments (1)
            Lanette (DevExpress Support) 6 years ago

              Thank you for sharing your resulting code. You are welcome!

                Could you please show me how to pre-populate the controls? For example, if I selected an item from an ASPxComboBox, then I want to pre-populate that value inside the ASPxTextBox control of the ASPxGridView.

                Lanette (DevExpress Support) 6 years ago

                  Hello,

                  I've created a separate ticket on your behalf (T694461: ASPxGridView - How to pre-populate ASPxTextBox based on the value of ASPxComboBox) regarding this task. It has been placed in our processing queue and will be answered shortly.

                  Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

                  Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.