第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

在DataGridView控件中實(shí)現(xiàn)凍結(jié)列分界線

標(biāo)簽:
資訊

我们在使用Office Excel的时候,有很多时候需要冻结行或者列。这时,Excel会在冻结的行列和非冻结的区域之间绘制上一条明显的黑线。如下图:

 

 

(图1)

WinForm下的DataGridView控件也能实现类似的冻结行或者列的功能(参见:http://msdn.microsoft.com/zh-cn/library/28e9w2e1(VS.85).aspx) ,但是呢,DataGridView控件默认不会在冻结列或者行的分界处绘制一个明显的分界线,这样的话,最终用户很难注意到当前有列或者行是冻结的。如下图所示:你能很快的找到那一列是Freeze的么?

(图2)

正是因为如此,我们如果能做出类似Excel的效果,就可以大大提高数据的可读性。

通常,我们如果想在现有的控件上多画点什么,就会去Override OnPaint方法,然后加入自己的OwnerDraw逻辑,但是呢在DataGridView上有一些困难:

1.如何确定冻结分界线的位置
2.如何保证分界线不会绘制到ScrollBar上
研究了一下,我们可以借用DataGridView提供的CellPainting方法。在DataGridView绘制每一个Cell的时候判断当前Cell是否是分界线所在的位置,然后进行绘制。最终做出的效果如下图:

 

(图3)

以下是DataGridView控件扩展源代码:

?

public class DataGridViewEx : DataGridView{    protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)    {        base.OnCellPainting(e);         //        // Paints the Frozen line        //        int lastFreezeColumnIndex = GetDisplayColumnFrozenLineIndex();        int lastFreezeRowIndex = GetDisplayRowFrozenLineIndex();         bool drawRowLine = lastFreezeRowIndex != -1 && lastFreezeRowIndex == e.RowIndex;        bool drawColumLine = lastFreezeColumnIndex != -1 && lastFreezeColumnIndex == e.ColumnIndex;         if (drawRowLine || drawColumLine)        {            e.Paint(e.ClipBounds, e.PaintParts);             if (drawColumLine)            {                e.Graphics.DrawLine(Pens.Black,                    e.CellBounds.Right - 1, e.CellBounds.Top,                     e.CellBounds.Right - 1, this.ClientRectangle.Bottom);            }            if (drawRowLine)            {                e.Graphics.DrawLine(Pens.Black,                    e.CellBounds.Left, e.CellBounds.Bottom - 1,                    e.CellBounds.Right, e.CellBounds.Bottom - 1);            }             e.Handled = true;        }    }     private int GetDisplayColumnFrozenLineIndex()    {        int lastFreezeColumnIndex = -1;        for (int i = 0; i < this.ColumnCount; i++)        {            DataGridViewColumn column = this.Columns[i];            if (column.Visible && column.Frozen)            {                lastFreezeColumnIndex = i;            }            else if (!column.Frozen)            {                return lastFreezeColumnIndex;            }        }        return lastFreezeColumnIndex;    }     private int GetDisplayRowFrozenLineIndex()    {        int lastFreezeRowIndex = -1;        for (int i = 0; i < this.RowCount; i++)        {            DataGridViewRow row = this.Rows[i];            if (row.Visible && row.Frozen)            {                lastFreezeRowIndex = i;            }            else if (!row.Frozen)            {                return lastFreezeRowIndex;            }        }        return lastFreezeRowIndex;     }}
點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
數(shù)據(jù)庫(kù)工程師
手記
粉絲
52
獲贊與收藏
361

關(guān)注作者,訂閱最新文章

閱讀免費(fèi)教程

  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消