Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am developing one web site in that i have given facility to user for generating grid result dynamically by creating user view.

now my problem i am not able to make sorting of the colums.
i am writing query like this
SQL
SELECT    Contact.cFirstName  as [ FIRST NAME],  Contact.cEmail  as [ EMAIL ID], Contact.cPhone  as [ PHONE NO], Contact.cMobile  as [ MOBILE]  FROM         CompanyMaster RIGHT OUTER JOIN  Contact ON CompanyMaster.nCompanyId = Contact.nContactId LEFT OUTER JOIN  LeadSourceMaster ON Contact.nLeadSource = LeadSourceMaster.nLeadSourceID


Contact.cFirstName as [ FIRST NAME], Contact.cEmail as [ EMAIL ID], Contact.cPhone as [ PHONE NO], Contact.cMobile as [ MOBILE] fields are dynamic.so i can't create bound fields.

i have made allow sorting true for gridview.if i will sort first column then give error
Cannot find column FIRST NAME.

pl help me to resolve this problem.
Posted

1.You should change your SQL string similar with the next one:

C#
SELECT    c.cFirstName ,  c.cEmail  , c.cPhone , c.cMobile  FROM         CompanyMaster AS c RIGHT OUTER JOIN  Contact ON CompanyMaster.nCompanyId = Contact.nContactId LEFT OUTER JOIN  LeadSourceMaster ON Contact.nLeadSource = LeadSourceMaster.nLeadSourceID

2.In the ASP page change the sort expression like this:
SortExpression="c.cFirstName"
 
Share this answer
 
Comments
Member 8825505 3-Jun-14 3:45am    
this solution already i know it but i want grid heading as FIRST NAME. and fields are dynamic so i cant add bound fields.
norbitrial 3-Jun-14 3:46am    
You can set alias in grid view for columns.
C#
//try this way to sort in datatable

         DataTable dtGetData = new DataTable();

         DataView dv = dtGetData.DefaultView;


         dv.Sort = "ColumnName" + " " + "ASC";

         //OR (You can pass it dynamically)

         dv.Sort = "ColumnName" + " " + "DESC";

         DataTable dtGetShipments = new DataTable();

         dtGetShipments = dv.ToTable();

         //bind this "dtGetShipments" datatable to gridview
 
Share this answer
 
You can try to change [FIRST NAME] to [FIRST_NAME]. Maybe that causes the problem.

After in GridView you can set up alias for your column as follow:
XML
<asp:GridView ID="yourGV" runat="server">
    <Columns>
       <asp:BoundField DataField="First Name" HeaderText="FIRST_NAME"/>
    </Columns>
</asp:GridView>
 
Share this answer
 
v2

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