总结DataGridView 和 Gridview 对行标题加序号的不同的方法

1、 DataGridView

写了一个函数实现这个功能,只要把这个函数放在

DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) 事件中

   具体实现函数为:

        public static void CreateNumber(DataGridView dg)

        {

            dg.RowHeadersWidth = 50;

            dg.TopLeftHeaderCell.Value = "序号";

            int r = dg.Rows.Count;

            for (int i = 1; i <= r; i++)

            {

                dg.Rows[i - 1].HeaderCell.Value = i.ToString();

            }

        }

 

2、 Gridview

首先将GridView IndicatorWidth设为50

然后直接在以下事件中实现

        private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)

        {

            e.Info.DisplayText = "1";

            e.Info.ImageIndex = -1;

            int rowIndex = e.RowHandle;

            if (e.Info.IsRowIndicator && rowIndex >= 0)

            {

                rowIndex++;

                e.Info.DisplayText = rowIndex.ToString();

            }

            else

            {

                e.Info.DisplayText = "序号";        

            }

      }

posted @ 2009-07-24 16:39  坚持·坚持  阅读(1201)  评论(0编辑  收藏  举报