简体   繁体   English

如何允许GridView排序并以链接形式显示文本

[英]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 我希望能够通过单击以下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: 我从一个SQL查询填充以上内容:

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: 显示以下内容:

在此处输入图片说明

  • How can I make it so that the Task Name , Department , Status , Due Date , and Link are clickable which will sort ASC/DESC? 我如何才能使“ Task Name ,“ Department ,“ Status ,“ Due Date ”和“ Link可单击,从而对ASC / DESC进行排序?
  • How can I make the Link rows all links with the display to be the Task Name ? 如何使“ Link行的所有具有显示内容的链接成为“ Task Name (ex: <a href="http://...objectid=8087&classid=1224">Test Event 1</a> ) (例如: <a href="http://...objectid=8087&classid=1224">Test Event 1</a>

I changed my GridView to this: 我将GridView更改为:

<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: 这就是显示的内容:

在此处输入图片说明

Add columns explicitly as shown here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.columns(v=vs.110).aspx 显式添加列,如下所示: http : //msdn.microsoft.com/zh-cn/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). 您的DataTextFields必须与列名对齐-不能与“ Text”或“ Link”对齐(除非Text和Link是有效的列名)。

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. 不要在查询中包含完整链接,而要在HyperLinkField中使用DataNavigateUrlFormatString。 The HyperLinkField allows you to define the DataNavigateUrlField and the DataTextField separately. HyperLinkField允许您分别定义DataNavigateUrlField和DataTextField。

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

To make columns sortable, be sure to set the SortExpression on the column. 为了使列可排序,请确保在列上设置SortExpression。

When defining columns in this way, you must also set AutoGenerateColumns to false on the GridView. 以这种方式定义列时,还必须在GridView上将AutoGenerateColumns设置为false。

<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部分​​更改为如下所示:

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

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. 如果您将不编辑信息,则可以删除EditItemTemplate,这将对其进行一些清理。 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. 要将它们转换为模板字段,当您在设计视图中时,将需要单击gridview上的智能标记。 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. 完成此操作后,如果要使用链接按钮,则在转换标签时必须从标签上更改标签,文本框默认用于编辑,标签默认用于项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM