重写WebControl-GridView

前台:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewTest.aspx.cs" Inherits="Sale_GridViewTest" %>

<%@ Register Assembly="SmartGridView" Namespace="SmartGridView" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Test GridView</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <cc1:SmartGridView ID="SmartGridView1" runat="server" AutoGenerateColumns="False"
                DataSourceID="ObjectDataSource1" Width="1000px">
                <Columns>
                    <asp:BoundField DataField="no" HeaderText="序号" SortExpression="no" />
                    <asp:BoundField DataField="name" HeaderText="名称" SortExpression="name" />
                    <asp:BoundField DataField="no" HeaderText="序号" SortExpression="no" />
                    <asp:BoundField DataField="name" HeaderText="名称" SortExpression="name" />
                    <asp:BoundField DataField="no" HeaderText="序号" SortExpression="no" />
                    <asp:BoundField DataField="name" HeaderText="名称" SortExpression="name" />
                    <asp:BoundField DataField="no" HeaderText="序号" SortExpression="no" />
                    <asp:BoundField DataField="name" HeaderText="名称" SortExpression="name" />
                    <asp:BoundField DataField="no" HeaderText="序号" SortExpression="no" />
                    <asp:BoundField DataField="name" HeaderText="名称" SortExpression="name" />
                </Columns>
                <FixRowCol FixColumnIndices="0,1" FixRowIndices="0" IsFixHeader="True" TableHeight="300px"
                    TableWidth="300px" EnableScrollState="true" />
            </cc1:SmartGridView>
            <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Select"
                TypeName="OjbData"></asp:ObjectDataSource>
        </div>
    </form>
</body>
</html>

 

*********************************************************

后台:需要新建一个类

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.ComponentModel;

/**/
/// <summary>
/// OjbData 的摘要说明
/// </summary>
public class OjbData
{
    public OjbData()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    [DataObjectMethod(DataObjectMethodType.Select, true)]
    public DataTable Select()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("no", typeof(string));
        dt.Columns.Add("name", typeof(string));

        for (int i = 0; i < 30; i++)
        {
            DataRow dr = dt.NewRow();
            dr[0] = "no" + i.ToString().PadLeft(2, '0');
            dr[1] = "name" + i.ToString().PadLeft(2, '0');

            dt.Rows.Add(dr);
        }

        return dt;
    }
}

 

 

要给Yii2-GridView中的自定义字段增加排序功能,可以通过以下步骤实现: 1. 在GridView的columns属性中,为自定义字段添加‘attribute’属性(指定排序用的字段),‘label’属性(指定在表头显示的名称),以及‘value’属性(指定自定义字段的值)。 2. 在GridView的dataProvider属性中,为排序用的字段添加一个Sort属性。 3. 在GridView中的排序链接中,为自定义字段添加一个sort参数,指定排序用的字段。 以下是一个示例代码: ``` use yii\grid\GridView; use yii\data\ActiveDataProvider; use yii\data\Sort; $dataProvider = new ActiveDataProvider([ 'query' => $query, 'sort' => [ 'attributes' => [ 'custom_field' => [ 'asc' => ['custom_field' => SORT_ASC], 'desc' => ['custom_field' => SORT_DESC], 'label' => 'Custom Field', ], ], ], ]); echo GridView::widget([ 'dataProvider' => $dataProvider, 'columns' => [ 'id', 'name', [ 'attribute' => 'custom_field', 'label' => 'Custom Field', 'value' => function ($model) { return $model->customField; } ], ], 'sorter' => [ 'attributes' => [ 'custom_field' => [ 'asc' => ['custom_field' => SORT_ASC], 'desc' => ['custom_field' => SORT_DESC], ], ], ], ]); ``` 在以上代码中,我们为自定义字段‘custom_field’添加了一个Sort属性,然后在GridView中为该字段添加了‘attribute’、‘label’和‘value’属性。最后,在排序链接中为自定义字段添加了‘sort’参数,指定排序用的字段。这样就可以为Yii2-GridView中的自定义字段增加排序功能了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值