DataGridView is a very powerful control from winfrom, but sometimes the appearance of this control needs to be redrawn by yourself. The following is a personal encounter with some problems in the project.
1. Whether adding a serial number to a cell or adding text to a specified cell, is actually redrawing a cell.
I personally prefer to trigger his own mechanism to redraw when the form is painted. RowPostPaint.
Start the code directly in this event.
private void dgv1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)///Name of dgv1_RowPostPaint event
{ StringFormat StringFormat = new StringFormat() { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center };///Set the centering style here
System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(e.RowBounds.Location.X,
e.RowBounds.Location.Y, AGING_DETAIL.RowHeadersWidth -4, e.RowBounds.Height);//Here is to get the table The cell start position of the header, height and width, ready for redraw sequence. The parameters inside are explained below
TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
AGING_DETAIL.RowHeadersDefaultCellStyle.Font, rectangle, AGING_DETAIL.RowHeadersDefaultCellStyle.ForeColor,
TextFormatFlags.VerticalCenter | TextFormatFlags.Right);///This is redrawing a cell at the row level
Y = this.AGING_DETAIL.GetCellDisplayRectangle(4, e.RowIndex, false).Y;///Here is to get the Y coordinate of the 4th column of the current row
X = this.AGING_DETAIL.GetCellDisplayRectangle(4, e.RowIndex, false). X;///Here is to get the X coordinate of the 4th column of the current row
W = this.AGING_DETAIL.GetCellDisplayRectangle(4, e.RowIndex, false).Width;///Here is to get the width of the current row
H = this.AGING_DETAIL .GetCellDisplayRectangle(4, e.RowIndex, false).Height;///Here is to get the height of the current row
System.Drawing.Rectangle rectangle1 = new System.Drawing.Rectangle(X,Y,
W, H);
e. Graphics.DrawString(AGING_DETAIL.Rows[e.RowIndex].Cells[4].Value.ToString()+"%",
AGING_DETAIL.RowHeadersDefaultCellStyle.Font, new SolidBrush(AGING_DETAIL.RowHeadersDefaultCellStyle.ForeColor), rectangle1, StringFormat);///This is to draw text within the cell range we just got, and the above sequence can also use this method. Personally prefer this.
}
/// Let's illustrate the above parameters
~~~
~~~~
-------------------------------------------------- -------------------------------- Small tail segmentation --------------- -------------------------------------------------- -------
The record of the rookie is also progress~ If it is helpful to you, then give it a small thumbs up. -----From the programmer radish without code