C# GridView添加行号_CustomDrawRowIndicator

public static class GridViewEx
    {
        public static void SetShowRowNo(this GridView gv)
        {
            Debug.Assert(gv != null);

            gv.OptionsView.ShowIndicator = true;
            gv.ResetIndicatorWidth();

            //设置行号
            gv.CustomDrawRowIndicator += delegate(object sender, RowIndicatorCustomDrawEventArgs e)
            {
                IndicatorObjectInfoArgs info = e.Info;
                if (info == null || !info.IsRowIndicator || e.RowHandle < 0)
                    return;
                info.Appearance.TextOptions.HAlignment = HorzAlignment.Center;
                info.DisplayText = (e.RowHandle + 1).ToString();
            };

            //行数改变
            gv.RowCountChanged += delegate(object sender, EventArgs e)
            {
                GridView selfView = sender as GridView;
                if (selfView == null)
                    return;
                selfView.ResetIndicatorWidth();
            };

            //gv有子表时,展开子表重置其行号宽度
            gv.MasterRowExpanded += delegate(object sender, CustomMasterRowEventArgs e)
            {
                GridView View = sender as GridView;
                if (View == null)
                    return;
                GridView dView = View.GetDetailView(e.RowHandle, e.RelationIndex) as GridView;
                if (dView == null)
                    return;
                dView.ResetIndicatorWidth();
            };

        }



        /// <summary>
        /// 重置行号宽度
        /// </summary>
        /// <param name="gvw"></param>
        /// <param name="rowCount"></param>
        public static void ResetIndicatorWidth(this GridView gvw, int rowCount = -1)
        {
            Debug.Assert(gvw != null);

            gvw.IndicatorWidth = GetIndicatorWidth(rowCount == -1 ? gvw.RowCount : rowCount);
        }

        //行号宽度
        private static int GetIndicatorWidth(int p)
        {
            return 7 * p.ToString().Length + 20;
        }
    }


引用:

GridView gridView1;

gridView1.SetShowRowNo();



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值