Wednesday, November 9, 2011

Find controls in the DataItem template of ASPxGridView column

Add DataItemTemplate to Grid Column to access at server side code. In case of DataRowTemplate, we create template for whole row, but if we want to create template for a single column then we use DataItemTemplate and on the place of DataRowTemplate’s
((sender as ASPxGridView).FindRowTemplateControl(..) method; here we use FindRowCellTempleteControl(..) method on HtmlRowCreated event of ASPxGridView.

Grid Markup:
  <dx:ASPxGridView ID="grvTest" AutoGenerateColumns="False" runat="server" DataSourceID="SqlDataSource1"
        OnHtmlRowPrepared="grvTest_HtmlRowPrepared" OnHtmlRowCreated="grvTest_HtmlRowCreated">
        <Columns>
            <dx:GridViewDataTextColumn Caption="RowID" Name="colRowID" VisibleIndex="0" Width="20px">
                <DataItemTemplate>
                     <dx:ASPxLabel ID="lblRowID" runat="server" Text='Label'>
                    </dx:ASPxLabel>
                </DataItemTemplate>
            </dx:GridViewDataTextColumn>


C#


protected void grvTest_HtmlRowCreated(object sender, ASPxGridViewTableRowEventArgs e)
        {
            if (e.RowType != GridViewRowType.Data) return;

            ASPxLabel label = grvTest.FindRowCellTemplateControl(e.VisibleIndex, null, 
            "lblRowID") as ASPxLabel;
            label.Text = (e.VisibleIndex + 1).ToString();
        }



4 comments :

  1. This comment has been removed by the author.

    ReplyDelete
  2. How can i get the text of the next example?

    dx:GridViewDataTextColumn Width="100px" Caption="" FieldName="modulo" Visible="false"
    DataItemTemplate
    table cellpadding="0" cellspacing="0"
    tr
    td style=" text-align:right;"
    asp:Label ID="lblModulo" runat="server" Text=""
    asp:Label
    td
    tr
    table
    DataItemTemplate
    dx:GridViewDataTextColumn

    ReplyDelete
  3. Thanks, It's really helpful code for me. i was looking for the same solution

    ReplyDelete
  4. But the label value is coming null

    ReplyDelete