C1TemplateField

Posted by: kaytea on 10 September 2017, 11:39 am EST

  • Posted 10 September 2017, 11:39 am EST

    To get the datafield of a C1BoundField I do the following:

    string val = (C1GridView1.Columns[0] as C1BoundField).DataField; 


    How do I achieve the same thing for C1TemplateField, let's say there's a Label inside the ItemTemplate of the C1TemplateField.

    Appreciate your help.
  • Replied 10 September 2017, 11:39 am EST

    Hi Kaytea,

    There isn't a direct property as DataField for the C1TemplateField.
    As a workaround, you can use either of the following two methods:

    1. Set the SortExpression of the C1TemplateField to be same as the name of the field you're binding the label inside the TemplateField to.

    <cc1:C1TemplateField HeaderText="Country" SortExpression="Country">
    <ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%#Eval("Country") %>' ></asp:Label>
    </ItemTemplate>
    </cc1:C1TemplateField>


    and then retrieve it's SortExpression's value :

     string str = ((C1TemplateField)C1GridView1.Columns[1]).SortExpression.ToString();
    Label2.Text = "DataField= " + str;


    2. Create a CustomLabel by inheriting the asp label and add a property DataField of type string.
    Now, use this CustomLabel in the ItemTemplate and set its DataField property same as the name of the Column it is bound to.

     <cc1:C1TemplateField HeaderText="Country" SortExpression="Country">
    <ItemTemplate>
    <MyLabel:CustomLabel ID="CustomLabel1" runat="server" DataField="Country" Text='<%#Eval("Country") %>'></MyLabel:CustomLabel>
    </ItemTemplate>
    </cc1:C1TemplateField>


    and then retrieve the DataField property's value in C1GridView's DataBound event:

    string str = ((CustomLabel)C1GridView1.Rows[0].FindControl("CustomLabel1")).DataField.ToString();
    Label2.Text = "DataField= " + str;


    I've attached the complete sample alongwith.

    Regards
    Abdias
    2011/07/C1GridView_TemplateColumnDataField.zip
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels