总结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 = "序号";        

            }

      }

你可能感兴趣的:(datagridview)