asp.netgridviewtemplatefield

How to pass GridView row to TemplateField function?


I have a GridView that has 3 columns: FirstName, LastName and a TemplateField FullName where I string together FirstName and LastName.

Assuming calling DisplayFullName is the function I want to use to concatenate FirstName and LastName, how do I pass the row argument to the function and how to declare the parameter of the function? Thanks.

Here's my code for the FullName column:

        <asp:TemplateField HeaderText="FullName">
            <ItemTemplate>
                <%# DisplayFullName(???) %>
            </ItemTemplate>
        </asp:TemplateField>

Here's my declaration for the function:

protected string DisplayFullName(???)
  { ... }

The ??? are where I need help. OR do I need to pass the row at all? If each time DisplayFullName is called, the 'current' row is known. If so, how do I access the current row in DisplayFullName?

I simplified the operation for the sake of clarity of the question. In reality, there can be up to 20 values in the row I need and I want to do some calculations in the function called.


Solution

  • @RJIGO: You can use function like this:

    <asp:TemplateField HeaderText="Name">
         <ItemTemplate>
            <%# DisplayFullName(Eval("FirstName"), Eval("LastName"))%>         
            </ItemTemplate>
         </asp:TemplateField>
    

    and your code behind method will like this

      protected string DisplayFullName(object FirstName, object LastName)
      {
          return Convert.ToString(FirstName)+Convert.ToString(LastName);
      }