dataGridView的行号

行号

 private void dataGridView1_Paint(object sender, PaintEventArgs e)
        {
            int irowcount = this.dataGridView1.DisplayedRowCount(true);
            if (irowcount == 0) return;
            Rectangle currrct = new Rectangle();
            int startNo = this.dataGridView1.FirstDisplayedCell.RowIndex;
            int ColNo = this.dataGridView1.FirstDisplayedCell.ColumnIndex;
            string stext = "";
            e.Graphics.DrawString("序号", this.Font, new SolidBrush(Color.Black), 10, this.Font.Height / 2 - 3);
            int nowy = 0;
            int hDelta = 0;
            for (int i = startNo; i < startNo + irowcount; i++)
            {
                currrct = (Rectangle)this.dataGridView1.GetCellDisplayRectangle(ColNo, i, true);
                nowy = currrct.Y + 2;
                stext = string.Format("{0, 3}", i + 1);
                if (hDelta == 0)
                    hDelta = (currrct.Height - this.Font.Height) / 2;
                //if (this.dataGridView1.Rows[i].Selected == true)
                //    e.Graphics.DrawString(stext, this.Font, new SolidBrush(Color.White), 10, nowy + hDelta);
                //else
                e.Graphics.DrawString(stext, this.Font, new SolidBrush(Color.Black), 10, nowy + hDelta);
            }
            int lengths = dataGridView1.Rows.Count.ToString().Length;
            if (lengths <= 3)
            {
//行号宽度 dataGridView1.RowHeadersWidth
= 41; } else { dataGridView1.RowHeadersWidth = lengths * 10 + 10; } }

 

posted @ 2022-03-24 09:34  世人皆萌  阅读(291)  评论(0编辑  收藏  举报