Click here to Skip to main content
15,065,639 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how sort filed gridview with call function in asp.net

please see in sample


ASP.NET
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowSorting="True" DataKeyNames="FUserId" OnSorting="GVAS_Sorting"  CssClass="GridViewStyle" DataSourceID="SqlDataSource1" Height="103px" Width="511px" ShowFooter="True" OnRowDataBound="GridView1_RowDataBound">
                          <AlternatingRowStyle CssClass="AltRowStyle" />
                        <Columns>
                         
                             <asp:TemplateField HeaderText="مجموع حقوق" SortExpression="SumWage">
                         <ItemTemplate>
                                    <asp:Label ID="lSumWage" Font-Underline="false" runat="server"
                                        Text='<%# SumWage() %>' />
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" />
                                   <FooterTemplate>
                    <asp:Label ID="lbSumWage" runat="server" Text="Label"></asp:Label>
                </FooterTemplate>
                            </asp:TemplateField>
<asp:TemplateField HeaderText="نام کاربر">
                                <ItemTemplate>
                                    <asp:Label ID="fullNAME" Font-Underline="false" runat="server"
                                        Text='<%# TempFullName() %>' />
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" />
                            </asp:TemplateField>

  </Columns>
</asp:GridView>
                    
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ICTPortalDBConnectionString %>" SelectCommand="SELECT   FFamily,FName,FUserId From Tuser "></asp:SqlDataSource>


C#
public string SumWage()
   {
      //

       return temp.retpricereq(u2,str).ToString();
   }


how sort with SortExpression="SumWage" sumwage()
Posted

1 solution

You can change your mathod to a static method like

C#
public static string SumWage()
    {
       //

        return temp.retpricereq(u2,str).ToString();
    }


Then use like

Text='<%# Class.SumWage() %>' 


// Class is the Class where your SumWage Method resides.

Hope this helps...
   
v2
Comments
saeed1364 3-Oct-13 13:08pm
   
iam dont sorting
JoCodes 3-Oct-13 13:15pm
   
any error you are getting? Have you tried replacing the method with a hardcoded string and checked whether it works?
saeed1364 3-Oct-13 13:23pm
   
this main function

public string SumWage()
{
string str = string.Empty;
int u2 = int.Parse(Eval("FUserId").ToString());
if (ConstList.CheckFormatDate(Ttimef.Text.Trim()) != 0 && Ttimef.Text.Trim() != "")
str += "and [FReqDate] >= " + ConstList.DateStoM(Ttimef.Text.Trim()).Date.ToShortDateString();

if (ConstList.CheckFormatDate(Ttimet.Text.Trim()) != 0 && Ttimet.Text.Trim() != "")
str += " and FReqDate <= " + ConstList.DateStoM(Ttimet.Text.Trim()).Date.ToShortDateString();

return temp.retpricereq(u2, str).ToString();
//return "1";
}

dont use static
JoCodes 3-Oct-13 13:36pm
   
IS your method in your codebhind page?
JoCodes 3-Oct-13 13:31pm
   
static funtion was to make the function access like '<%# %>'
saeed1364 3-Oct-13 13:35pm
   
when use static dont know Eval("FUserId").ToString());
saeed1364 3-Oct-13 13:37pm
   
Error 4 An object reference is required for the non-static field, method, or property 'System.Web.UI.TemplateControl.Eval(string)'
JoCodes 3-Oct-13 13:43pm
   
Eval wont work since its non statc . check the below comment which includes a link how to use non static methods with Eval
JoCodes 3-Oct-13 13:40pm
   
Can you check this if you want to use Eval ..
http://www.mallstuffs.com/blogs/BlogDetails.aspx?BlogId=278&BlogType=Technical&Topic=Call%20Codebehind%20method%20from%20GridView%20ItemTemplate%20Eval%20function

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900