SearchForKnowledge
SearchForKnowledge

Reputation: 3751

How to allow GridView sort and display text as link

I want to be able to allow the user to sort through by clicking on each header of the following GridView

<asp:GridView OnSorting="yourTasksGV_Sorting" AllowSorting="true" ID="yourTasksGV" runat="server" ClientIDMode="Static" EmptyDataText="There is no data to display"></asp:GridView>

I am populating the above from a SQL query:

string query = @"SELECT  CT.ATTR2739 'Task Name'
                ,UG.USERGROUPNAME 'Department'
                ,CT.ATTR2812 'Status'
                ,CT.ATTR2752 'Due Date'
                ,'http://dvmag/appnet/workview/objectPop.aspx?objectid=' + CAST(CT.OBJECTID AS VARCHAR) + '&classid=1224' 'Link'
            FROM HSI.RMOBJECTINSTANCE1224 CT LEFT JOIN HSI.USERGROUP UG on CT.FK2743 = UG.USERGROUPNUM
            WHERE CT.ACTIVESTATUS = 0";

using (SqlConnection conn = new SqlConnection(connString))
{
    try
    {
        SqlCommand cmd = new SqlCommand(query, conn);

        // create data adapter
        SqlDataAdapter da = new SqlDataAdapter(query, conn);
        // this will query your database and return the result to your datatable
        da.Fill(taskData);
        //conn.Close();
        yourTasksGV.DataSource = taskData;
        yourTasksGV.DataBind();

    }
    catch (Exception ex)
    {
        string error = ex.Message;
    }
}

Which displays the following:

enter image description here

I changed my GridView to this:

<asp:GridView OnSorting="yourTasksGV_Sorting" AllowSorting="true" ID="yourTasksGV" runat="server" ClientIDMode="Static" EmptyDataText="There is no data to display">
                        <Columns>
                            <asp:BoundField DataField="Text" HeaderText="Task Name" SortExpression="TaskName" />
                            <asp:BoundField DataField="Text" HeaderText="Department" SortExpression="DepartmentName" />
                            <asp:BoundField DataField="Text" HeaderText="Status" SortExpression="TheStatus" />
                            <asp:BoundField DataField="Text" HeaderText="Due Date" SortExpression="DueDate" />
                            <asp:BoundField DataField="Link" HeaderText="Complete Task" SortExpression="CompleteTask" />
                        </Columns>
                    </asp:GridView>

And this is what showed up:

enter image description here

Upvotes: 0

Views: 2791

Answers (2)

Humpy
Humpy

Reputation: 2012

I'd suggest converting them to template fields. Here is an example of how one would look.

<asp:TemplateField HeaderText="Link" SortExpression="Link">
         <EditItemTemplate>
                  <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Link") %>'></asp:TextBox>
         </EditItemTemplate>
         <ItemTemplate>
                  <asp:LinkButton ID="LinkButton1" runat="server" Text='<%# Bind("Link") %>'></asp:Label>
         </ItemTemplate>
         <ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>

If you won't be editing the information, you can remove the EditItemTemplate, and that will clean it up a bit. Hope this helps!

EDIT:

To convert them to templatefields, you will want to click on the smart tag on your gridview when you are in design view. Once you do that, click edit columns, select the columns you want to convert, then select the convert to templatefield option in the lower right hand corner of that page. Once you do that, if you want to use a link button you will have to change the ones you want from a label as when it converts them, textboxes are default for edit and labels are default for item.

Upvotes: 1

Mike
Mike

Reputation: 633

Add columns explicitly as shown here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.columns(v=vs.110).aspx

Your DataTextFields must align with the column names - not "Text" or "Link" (unless Text and Link are valid column names).

This will allow you to specify the column type as a hyperlink. Rather than including your full link in the query, use the DataNavigateUrlFormatString in the HyperLinkField. The HyperLinkField allows you to define the DataNavigateUrlField and the DataTextField separately.

See http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hyperlinkfield(v=vs.110).aspx

To make columns sortable, be sure to set the SortExpression on the column.

When defining columns in this way, you must also set AutoGenerateColumns to false on the GridView.

<asp:GridView OnSorting="yourTasksGV_Sorting" AllowSorting="true" ID="yourTasksGV" runat="server" ClientIDMode="Static" EmptyDataText="There is no data to display" AutoGenerateColumns ="False">
    <Columns>
        <asp:BoundField DataField="Task Name" HeaderText="Task Name" SortExpression="TaskName" />
        <asp:BoundField DataField="Department" HeaderText="Department" SortExpression="DepartmentName" />
        <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="TheStatus" />
        <asp:BoundField DataField="Due Date" HeaderText="Due Date" SortExpression="DueDate" />
        <asp:HyperLinkField DataNavigateUrlFields="Link" DataTextField="Task Name" DataNavigateUrlFormatString="http://...objectid={0}&classid=12240" HeaderText="Complete Task" SortExpression="CompleteTask" />
    </Columns>
</asp:GridView>

Then, change the SELECT portion of your query to something like this:

SELECT  
CT.ATTR2739 'Task Name'
,UG.USERGROUPNAME 'Department'
,CT.ATTR2812 'Status'
,CT.ATTR2752 'Due Date'
,CT.OBJECTID 'Link' 

Upvotes: 1

Related Questions