public void DrawFocus_ValidRowIndex_Success() { using (var image = new Bitmap(10, 10)) using (Graphics graphics = Graphics.FromImage(image)) { var dataGridView = new DataGridView(); dataGridView.Columns.Add("name", "text"); var e = new DataGridViewRowPostPaintEventArgs(dataGridView, graphics, Rectangle.Empty, Rectangle.Empty, 0, DataGridViewElementStates.Displayed, null, new DataGridViewCellStyle(), false, false); e.DrawFocus(new Rectangle(1, 2, 3, 4), true); } }
public void DrawFocus_InvalidRowIndex_ThrowsInvalidOperationException(int rowIndex) { using (var image = new Bitmap(10, 10)) using (Graphics graphics = Graphics.FromImage(image)) { var dataGridView = new DataGridView(); dataGridView.Columns.Add("name", "text"); var e = new DataGridViewRowPostPaintEventArgs(dataGridView, graphics, Rectangle.Empty, Rectangle.Empty, rowIndex, DataGridViewElementStates.Displayed, null, new DataGridViewCellStyle(), false, false); Assert.Throws <InvalidOperationException>(() => e.DrawFocus(new Rectangle(1, 2, 3, 4), true)); } }
private void PackagesDataGrid_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { if (e.RowIndex < 0) { return; } int MainOrderID = Convert.ToInt32(PackagesDataGrid.Rows[e.RowIndex].Cells["MainOrderID"].Value); int GroupType = Convert.ToInt32(PackagesDataGrid.Rows[e.RowIndex].Cells["GroupType"].Value); Color BackColor = Color.White; Color ForeColor = Color.Black; Color SelectionBackColor = Color.FromArgb(31, 158, 0); Color SelectionForeColor = Color.White; int rowHeaderWidth = PackagesDataGrid.RowHeadersVisible ? PackagesDataGrid.RowHeadersWidth : 0; Rectangle rowBounds = new Rectangle( rowHeaderWidth, e.RowBounds.Top, PackagesDataGrid.Columns.GetColumnsWidth( DataGridViewElementStates.Visible) - PackagesDataGrid.HorizontalScrollingOffset + 1, e.RowBounds.Height); if (GroupType != CheckTray.CurrentGroupType) { BackColor = Color.Red; ForeColor = Color.White; SelectionBackColor = Color.Red; SelectionForeColor = Color.White; } if (GroupType == 2 && CheckTray.IsWrongClient(MainOrderID)) { BackColor = Color.Red; ForeColor = Color.White; SelectionBackColor = Color.Red; SelectionForeColor = Color.White; } PackagesDataGrid.Rows[e.RowIndex].DefaultCellStyle.BackColor = BackColor; PackagesDataGrid.Rows[e.RowIndex].DefaultCellStyle.ForeColor = ForeColor; PackagesDataGrid.Rows[e.RowIndex].DefaultCellStyle.SelectionBackColor = SelectionBackColor; PackagesDataGrid.Rows[e.RowIndex].DefaultCellStyle.SelectionForeColor = SelectionForeColor; if (PackagesDataGrid.CurrentCellAddress.Y == e.RowIndex) { // Paint the focus rectangle. e.DrawFocus(rowBounds, true); } }
private void dgvPassenger_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { int width = dgvPassenger.Columns.GetColumnsWidth(DataGridViewElementStates.Visible);// +dgvPassenger.Width - 2; Rectangle rowBounds = new Rectangle( 0, e.RowBounds.Top, width, e.RowBounds.Height - 1); if (dgvPassenger.CurrentCellAddress.Y == e.RowIndex) { //设置选中边框 e.DrawFocus(rowBounds, true); } }
/// <summary> /// Row重绘后处理 /// </summary> /// <param name="e"></param> protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e) { base.OnRowPostPaint(e); int width = this.Columns.GetColumnsWidth(DataGridViewElementStates.Visible);// + _RowHeadWidth; Rectangle rowBounds = new Rectangle( 0, e.RowBounds.Top, width, e.RowBounds.Height); if (this.CurrentCellAddress.Y == e.RowIndex) { //设置选中边框 e.DrawFocus(rowBounds, true); } }
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { if (e.RowIndex == this.messageBS.Position) { e.DrawFocus(e.RowBounds, true); } else { // Draw background Pen p; using (p = new Pen(Color.LightGray)) { e.Graphics.DrawRectangle(p, e.RowBounds); } } }
//</Snippet20> //<Snippet30> // Paints the content that spans multiple columns and the focus rectangle. void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { // Calculate the bounds of the row. Rectangle rowBounds = new Rectangle( this.dataGridView1.RowHeadersWidth, e.RowBounds.Top, this.dataGridView1.Columns.GetColumnsWidth( DataGridViewElementStates.Visible) - this.dataGridView1.HorizontalScrollingOffset + 1, e.RowBounds.Height); SolidBrush forebrush = null; try { //<Snippet34> // Determine the foreground color. if ((e.State & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected) { forebrush = new SolidBrush(e.InheritedRowStyle.SelectionForeColor); } else { forebrush = new SolidBrush(e.InheritedRowStyle.ForeColor); } //</Snippet34> //<Snippet35> // Get the content that spans multiple columns. object recipe = this.dataGridView1.Rows.SharedRow(e.RowIndex).Cells[2].Value; if (recipe != null) { String text = recipe.ToString(); // Calculate the bounds for the content that spans multiple // columns, adjusting for the horizontal scrolling position // and the current row height, and displaying only whole // lines of text. Rectangle textArea = rowBounds; textArea.X -= this.dataGridView1.HorizontalScrollingOffset; textArea.Width += this.dataGridView1.HorizontalScrollingOffset; textArea.Y += rowBounds.Height - e.InheritedRowStyle.Padding.Bottom; textArea.Height -= rowBounds.Height - e.InheritedRowStyle.Padding.Bottom; textArea.Height = (textArea.Height / e.InheritedRowStyle.Font.Height) * e.InheritedRowStyle.Font.Height; // Calculate the portion of the text area that needs painting. RectangleF clip = textArea; clip.Width -= this.dataGridView1.RowHeadersWidth + 1 - clip.X; clip.X = this.dataGridView1.RowHeadersWidth + 1; RectangleF oldClip = e.Graphics.ClipBounds; e.Graphics.SetClip(clip); // Draw the content that spans multiple columns. e.Graphics.DrawString( text, e.InheritedRowStyle.Font, forebrush, textArea); e.Graphics.SetClip(oldClip); } //</Snippet35> } finally { forebrush.Dispose(); } if (this.dataGridView1.CurrentCellAddress.Y == e.RowIndex) { // Paint the focus rectangle. e.DrawFocus(rowBounds, true); } }
// Paints the content that spans multiple columns and the focus rectangle. void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { // Calculate the bounds of the row. Rectangle rowBounds = new Rectangle( this.dataGridView1.RowHeadersWidth, e.RowBounds.Top, this.dataGridView1.Columns.GetColumnsWidth( DataGridViewElementStates.Visible) - this.dataGridView1.HorizontalScrollingOffset + 1, e.RowBounds.Height); SolidBrush forebrush = null; try { // Determine the foreground color. if ((e.State & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected) { forebrush = new SolidBrush(e.InheritedRowStyle.SelectionForeColor); } else { forebrush = new SolidBrush(e.InheritedRowStyle.ForeColor); } // Get the content that spans multiple columns. object recipe = this.dataGridView1.Rows.SharedRow(e.RowIndex).Cells[2].Value; if (recipe != null) { String text = recipe.ToString(); // Calculate the bounds for the content that spans multiple // columns, adjusting for the horizontal scrolling position // and the current row height, and displaying only whole // lines of text. Rectangle textArea = rowBounds; textArea.X -= this.dataGridView1.HorizontalScrollingOffset; textArea.Width += this.dataGridView1.HorizontalScrollingOffset; textArea.Y += rowBounds.Height - e.InheritedRowStyle.Padding.Bottom; textArea.Height -= rowBounds.Height - e.InheritedRowStyle.Padding.Bottom; textArea.Height = (textArea.Height / e.InheritedRowStyle.Font.Height) * e.InheritedRowStyle.Font.Height; // Calculate the portion of the text area that needs painting. RectangleF clip = textArea; clip.Width -= this.dataGridView1.RowHeadersWidth + 1 - clip.X; clip.X = this.dataGridView1.RowHeadersWidth + 1; RectangleF oldClip = e.Graphics.ClipBounds; e.Graphics.SetClip(clip); // Draw the content that spans multiple columns. e.Graphics.DrawString( text, e.InheritedRowStyle.Font, forebrush, textArea); e.Graphics.SetClip(oldClip); } } finally { forebrush.Dispose(); } if (this.dataGridView1.CurrentCellAddress.Y == e.RowIndex) { // Paint the focus rectangle. e.DrawFocus(rowBounds, true); } }