Winform 中DataGridView、dev Gridview控件添加行標題


有很多種方法。 

1、可以在DataGridView控件中的RowStateChanged事件改變行標題單元格的值(Row.HeaderCell.Value)

1         /// <summary>
2         /// 行狀態更改時發生
3         /// </summary>
4         /// <param name="sender"></param>
5         /// <param name="e"></param>
6         private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
7         {             
8    //e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1); 9 e.Row.HeaderCell.Value = (e.Row.Index + 1).ToString();//添加行號 10 }

 

2、可以在DataGridView控件中的RowPostPaint事件例進行設置,TextRenderer類的DrawText()方法使用指定的設備上下文、字體、顏色和格式說明在指定界限中繪制指定文本。

 1         /// <summary>
 2         /// 所有單元格繪制后,執行 行繪制時發生
 3         /// </summary>
 4         /// <param name="sender"></param>
 5         /// <param name="e"></param>
 6         private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
 7         {
 8             //
 9             System.Drawing.Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Y, this.dataGridView1.RowHeadersWidth - 4, this.dataGridView1.ColumnHeadersHeight);
10             TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), this.dataGridView1.RowHeadersDefaultCellStyle.Font, rectangle, this.dataGridView1.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);//”TextFormatFlags.VerticalCenter | TextFormatFlags.Right“中“|”有增加的作用,此處添加了兩種文本字符格式樣式
11         }

 

dev 的DevExpress.XtraGrid.Views.Grid.GridView控件添加行號:

        /// <summary>
        /// GridView  顯示行號   設置行號列的寬度
        /// </summary>
        /// <param name="gv">GridView 控件名稱</param>
        /// <param name="width">行號列的寬度 如果為null或為0 默認為30</param>
        public void DrawRowIndicator(DevExpress.XtraGrid.Views.Grid.GridView gv, int width)
        {
            gv.CustomDrawRowIndicator += new DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventHandler(gv_CustomDrawRowIndicator);
            //if (width != null)
            //{
            if (width != 0)
            {
                gv.IndicatorWidth = width;
            }
            else
            {
                gv.IndicatorWidth = 30;
            }
            //}
            //else
            //{
            //    gv.IndicatorWidth = 30;
            //}
        }
        /// <summary>
        /// 行號設置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gv_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator && e.RowHandle > -1)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM