private void dgvPassenger_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) { //是否是选中状态 if ((e.State & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected) { // 计算选中区域Size int width = dgvPassenger.Columns.GetColumnsWidth(DataGridViewElementStates.Visible);// +dgvPassenger.Width - 2; Rectangle rowBounds = new Rectangle( 0, e.RowBounds.Top, width, e.RowBounds.Height - 1); // 绘制选中背景色 using (LinearGradientBrush backbrush = new LinearGradientBrush(rowBounds, Color.Wheat, Color.BurlyWood, 90.0f)) { e.Graphics.FillRectangle(backbrush, rowBounds); e.PaintCellsContent(rowBounds); e.Handled = true; } } }
/// <summary> /// Row重绘前处理 /// </summary> /// <param name="e"></param> protected override void OnRowPrePaint(DataGridViewRowPrePaintEventArgs e) { base.OnRowPrePaint(e); //是否是选中状态 if ((e.State & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected) { // 计算选中区域Size int width = this.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) + _RowHeadWidth; Rectangle rowBounds = new Rectangle( 0, e.RowBounds.Top, width, e.RowBounds.Height); // 绘制选中背景色 using (LinearGradientBrush backbrush = new LinearGradientBrush(rowBounds, Color.FromArgb(167, 222, 214), Color.FromArgb(167, 222, 214), //e.InheritedRowStyle.ForeColor, 90.0f)) { e.Graphics.FillRectangle(backbrush, rowBounds); e.PaintCellsContent(rowBounds); e.Handled = true; } } }
private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) { //if (e.RowIndex >= dataGridView1.Rows.Count - 1) // return; DataGridViewRow dgr = dataGridView1.Rows[e.RowIndex]; try { //dgr.Cells[0]是当前性别列的索引值,用以确定判断哪一列的值 if (double.Parse(dgr.Cells[3].Value.ToString()) < 10) { //定义画笔,使用颜色是深灰。 using (SolidBrush brush = new SolidBrush(Color.Red)) { //利用画笔填充当前行 e.Graphics.FillRectangle(brush, e.RowBounds); //将值重新写回当前行。 e.PaintCellsContent(e.ClipBounds); e.Handled = true; } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public void PaintCellsContent_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 DataGridViewRowPrePaintEventArgs(dataGridView, graphics, Rectangle.Empty, Rectangle.Empty, rowIndex, DataGridViewElementStates.Displayed, null, new DataGridViewCellStyle(), false, false); Assert.Throws <InvalidOperationException>(() => e.PaintCellsContent(new Rectangle(1, 2, 3, 4))); } }
public void PaintCellsContent_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 DataGridViewRowPrePaintEventArgs(dataGridView, graphics, Rectangle.Empty, Rectangle.Empty, 0, DataGridViewElementStates.Displayed, null, new DataGridViewCellStyle(), false, false); e.PaintCellsContent(new Rectangle(1, 2, 3, 4)); } }
private void gridSchLibPrimitives_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) { if (_loading || e.RowIndex < 0) { return; } // Draw background depending on the "display mode" matching the current one var primitive = (SchPrimitive)gridSchLibPrimitives.Rows[e.RowIndex].Tag; if (!primitive.IsOfCurrentDisplayMode) { e.PaintCellsBackground(e.RowBounds, (e.State & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected); using (var brush = new HatchBrush(HatchStyle.DiagonalCross, Color.FromKnownColor(KnownColor.ControlDark), Color.Transparent)) { e.Graphics.FillRectangle(brush, e.RowBounds); } // Draw contents e.PaintCellsContent(e.RowBounds); e.Handled = true; } }