开发者

inserting row numbers in datagridview by only changing its proprty in c# .net,

I want开发者_如何学C to generate row number automatically in Datagridveiw header by changing its property in c# .net,Thanks in advance


<asp:TemplateField>
    <ItemTemplate>
        <%# Container.DataItemIndex + 1 %>
    </ItemTemplate>
</asp:TemplateField>

For Generating row number DataGridView using c# you can use RowPostPaint event of DataGridView

  private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            // get the row number in leading zero format, 
            //  where the width of the number = the width of the maximum number
            int RowNumWidth = dataGridView1.RowCount.ToString().Length;
            StringBuilder RowNumber = new StringBuilder(RowNumWidth);
            RowNumber.Append(e.RowIndex + 1);
            while (RowNumber.Length < RowNumWidth)
                RowNumber.Insert(0, "0");

            // get the size of the row number string
            SizeF Sz = e.Graphics.MeasureString(RowNumber.ToString(), this.Font);

            // adjust the width of the column that contains the row header cells 
            if (dataGridView1.RowHeadersWidth < (int)(Sz.Width + 20))
                dataGridView1.RowHeadersWidth = (int)(Sz.Width + 20);

            // draw the row number
               e.Graphics.DrawString(
                RowNumber.ToString(), 
                this.Font, 
                SystemBrushes.ControlText, 
                e.RowBounds.Location.X + 15, 
                e.RowBounds.Location.Y + ((e.RowBounds.Height - Sz.Height) / 2));
        }

Right Click on the DataGridView, go through Properties. Then you will get a window like below. Go through the events in the top of the properties you can see several events that can be handled by the DataGridView.Select the RowPostPaint and double click on the right side will create an event on code behind.

inserting row numbers in datagridview by only changing its proprty in c# .net,


You can use the CurrentRow property of the Data Grid

eg. like to change the back color, as follows:

myDataGrid.CurrentRow.DefaultCellStyle.BackColor = Color.Yellow;

hope it vl b useful to you kitty...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜