How to Access GridViewDataColumn of DetailRow on server side

I am using ASPxGridView and not able to access text box, which is present inside DetailRow (i.e. inside second grid view not in master grid view)

Can any one help me

ASPX

 <Templates>
   <DetailRow>
     <dx:ASPxGridView ID="dgCustomers" runat="server" KeyFieldName="CustomerCode"                  OnBeforePerformDataSelect="dgCustomers_BeforePerformDataSelect" OnDataBinding="dgCustomers_DataBinding" Width="100%" OnRowDeleting="dgCustomers_RowDeleting" OnRowInserting="dgCustomers_RowInserting"
         OnRowUpdating="dgCustomers_RowUpdating">
        <Columns>
          <dx:GridViewDataColumn FieldName="CustomerCode" VisibleIndex="0">
            <EditItemTemplate>
              <asp:TextBox ID="txtCustCode" runat="server" Width="50px" />
            </EditItemTemplate>
          </dx:GridViewDataColumn>
          <dx:GridViewDataColumn>
            <EditItemTemplate>
               <asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" />
            </EditItemTemplate>
          <dx:GridViewCommandColumn ShowNewButton="true" ShowEditButton="true" ShowDeleteButton="true" VisibleIndex="4">
          </dx:GridViewCommandColumn>
     </Columns>

Code Behind

I tried something like this:

 ASPxGridView detail = dgDepots.FindDetailRowTemplateControl(0, "dgCustomers") as ASPxGridView;
        if (detail != null)
        {
            for (int i = 0; i < detail.VisibleRowCount; i++)
            {
                var item = detail.FindDetailRowTemplateControl(0, "txtCustCode");
                TextBox textbox = detail.FindRowCellTemplateControl(i, detail.Columns["CustomerCode"] as GridViewDataColumn, "txtCustCode") as TextBox;
                var anything = textbox.Text;
                if (CustomerCodeArray.Contains(anything))
                {
                   //Something
                }
            }
        }

But while debugging i can see textbox showing null

Where I am doing wrong Please help me

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

well I do this.

First I have an extension method to access a control y cell template.

public static object GetDataItemControl(this ASPxGridView _grid, int _visibleIndex, string _fieldName, string _controlID)
{
    return _grid.FindRowCellTemplateControl(_visibleIndex, _grid.Columns[_fieldName] as GridViewDataColumn, _controlID);
}

next, you should use the property NamingContainer of button to access to gridview located in detail row, and with this and the function you can access to textbox located in the template. For example:

I use devexpress bootstrap but its the same.

The view, you need put a column name for access it.

<dx:BootstrapGridView ID="gv" runat="server" AutoGenerateColumns="False" DataSourceID="DataSource" KeyFieldName="KeyField">
    <SettingsDataSecurity AllowEdit="True" /><SettingsBehavior AllowFocusedRow="true" />
    <Toolbars>
        <dx:BootstrapGridViewToolbar>
            <Items>
                <dx:BootstrapGridViewToolbarItem Command="Edit" />
                <dx:BootstrapGridViewToolbarItem Command="Refresh" />
            </Items>
        </dx:BootstrapGridViewToolbar>
    </Toolbars>
    <Columns>
        <dx:BootstrapGridViewDataColumn Name="CustomCode">
            <DataItemTemplate>
                <asp:TextBox ID="txtCustCode" runat="server" Width="50px" />
            </DataItemTemplate>
        </dx:BootstrapGridViewDataColumn>
        <dx:BootstrapGridViewDataColumn>
            <DataItemTemplate>
                <asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" />
            </DataItemTemplate>
        </dx:BootstrapGridViewDataColumn>
    </Columns>
</dx:BootstrapGridView>

The code behind, we access to gridview using the property NamingContainer, then we access to control in template with the extension method.

protected void btnSearch_Click(object sender, EventArgs e)
{
    Button _button = sender as Button;

    BootstrapGridView _grid = _button.NamingContainer.NamingContainer.NamingContainer as BootstrapGridView;

    TextBox _textBox = _grid.GetDataItemControl(_grid.FocusedRowIndex, "CustomCode", "txtCustCode") as TextBox;

    String _text = _textBox.Text;
}

I use this code and it works for me, if u have a question make it in comments.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x