顯示DataGrid序號的一個適用的方法_datagrid

來源:互聯網
上載者:User
作者Blog: http://blog.csdn.net/wangj2001/

如果資料量小的話沒有問題,一旦資料量大,顯示特別慢,還有個缺點就是拖動行高時行號不隨行高的變化而變動,出現是幾個序號在一個儲存格中顯示。我自己對他們的演算法進行總結,寫出一個效果比較不錯的帶序號的 DataGrid 。原理:只顯示表格中顯示行的序號,並且拖動行,行號一起移動。

override protected void OnPaint(PaintEventArgs e)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

{

base.OnPaint(e);

    try

    {

        if(this.DataSource!=null)

        {          

        int yDelta;

        System.Drawing .Rectangle cell=this.GetCellBounds(0,0);

        int y=cell.Top +2;

        e.Graphics.DrawString("編號", this.Font, new SolidBrush(Color.Black), 8, y-18); //

        if(this.VisibleRowCount >0)//只在有記錄集時在表格中顯示序號

        {                  

            CurrencyManager cm;

            cm = (CurrencyManager) this.BindingContext[this.DataSource, this.DataMember];

            if(cm.Count >0)

            {                      

                int nRow=-1;

                y=41;           //為第一行預設高度

                while(nRow<0)

                {

nRow=this.HitTest (8,y).Row ;

                    y++;

                }

                int nCount=0;

                while(y<this.Height && nCount<this.VisibleRowCount )

                {

                    string text = string.Format("{0}", nRow+nCount+1);

                    e.Graphics.DrawString(text, this.Font, new SolidBrush(Color.Black), 10, y);

                    yDelta = this.GetCellBounds( nRow+nCount,0).Height + 1;//****表示一行高度的參數

                    y += yDelta;

//如果下面有子行顯示序號的區分顯示  

                    if(this.IsExpanded (nRow+nCount)&& nRow+nCount+1<cm.Count )                                 {

                        y+=this.GetCellBounds (nRow+nCount+1,0).Height +3;

                    }

                    nCount++;      

                }

}

        }

        }

    }

    catch

    {}

}

重載了DataGrid中的Paint,這樣用起來會特別方便,區區雕蟲小技,希望和大家共同分享。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.