DataGridView in winform how to make a column can add two Button controls

Today, at the time of online shopping, I saw a child boots mention of this issue, read the post and found the landlord finally gives his own solution, I feel pretty good, so the content of the post of finishing the next, turn out

Thinking solution is this: create three new button template columns, delete the first picture, a second display to edit images, add a third display picture to see the code.
Code first button template column:
a using the System;
the using the System.Windows.Forms;

namespace two combined heads column headings redraw
{
  public class DataGridViewButtonColumnDel: the DataGridViewColumn
  {
  public DataGridViewButtonColumnDel ()
  {
  this.CellTemplate new new DataGridViewButtonCellDel = ();
  this.HeaderText = "Button";
  }
  }
}


the System the using;
the using the System.Windows.Forms;
the using the System.Drawing;
namespace two combined heads column headings redraw
{
  public class DataGridViewButtonCellDel: DataGridViewButtonCell
  {
  protected void the override the Paint (
  Graphics graphics,
  Rectangle clipBounds,
  Rectangle cellBounds,
  int rowIndex,
  DataGridViewElementStates cellState,
  object value,
  object formattedValue,
  string errorText,
  DataGridViewCellStyle cellStyle,
  DataGridViewAdvancedBorderStyle advancedBorderStyle,
  DataGridViewPaintParts paintParts)
  {
  base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
Image _img = Properties.Resources.imgDelete_x16;
graphics.DrawImage(_img, cellBounds.Location.X + 5, cellBounds.Location.Y+3, _img.Width, _img.Height);

  }
  }
}
Second button code template columns:

the using the System;
the using the System.Windows.Forms;

namespace two combined heads column headings redraw
{
  public class DataGridViewButtonColumnEdi: the DataGridViewColumn
  {
  public DataGridViewButtonColumnEdi ()
  {
  this.CellTemplate new new DataGridViewButtonCellEdi = ();
  the this = .HeaderText "Button";
  }
  }
}

the using the System;
the using the System.Windows.Forms;
the using the System.Drawing;
namespace two combined heads column headings redraw
{
  public class DataGridViewButtonCellEdi: DataGridViewButtonCell
  {
  protected void the override the Paint (
  Graphics Graphics,
  the Rectangle clipBounds,
  Rectangle cellBounds,
  int rowIndex,
  DataGridViewElementStates cellState,
  object value,
  object formattedValue,
  string errorText,
  DataGridViewCellStyle cellStyle,
  DataGridViewAdvancedBorderStyle advancedBorderStyle,
  DataGridViewPaintParts paintParts)
  {
  base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
Image _img = Properties.Resources.imgEdit_x16;
graphics.DrawImage(_img, cellBounds.Location.X + 5, cellBounds.Location.Y + 3, _img.Width, _img.Height);

  }
  }
}

第三个按钮模板列的代码:
using System;
the System.Windows.Forms the using;

namespace two combined heads column headings redraw
{
  public class DataGridViewButtonColumnAdd: the DataGridViewColumn
  {
  public DataGridViewButtonColumnAdd ()
  {
  this.CellTemplate new new DataGridViewButtonCellAdd = ();
  this.HeaderText = "Button";
  }
  }
}

the using the System ;
the using the System.Windows.Forms;
the using the System.Drawing;
namespace two combined heads column headings redraw
{
  public class DataGridViewButtonCellAdd: DataGridViewButtonCell
  {
  protected void the override the Paint (
  Graphics Graphics,
  the Rectangle clipBounds,
  the Rectangle cellBounds,
  int rowIndex,
  CellState DataGridViewElementStates,
  Object value,
  Object formattedValue,
  String the errorText,
  the DataGridViewCellStyle CellStyle,
  DataGridViewAdvancedBorderStyle advancedBorderStyle,
  DataGridViewPaintParts paintParts)
  {
  base.Paint (Graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, the errorText, CellStyle, advancedBorderStyle, paintParts);
Image = Properties.Resources.imgAdd_x16 _img;
Graphics.DrawImage (_img, cellBounds.Location.X. 5 +, + cellBounds.Location.Y. 3, _img.Width, _img.Height);

  }
  }
}

the above code is almost the same, red is loaded with a different part of the picture.
The picture is introduced through the project resource file. Method is: Open Windows Explorer, expand the "Properties" node (this node is hidden by default). Double-click Resources.resx, click "Add Resources", select "Add existing resources"; add three pictures: imgDelete_x16.png, imgEdit_x16, imgAdd_x16.png,

After dragging on form1 a DataGridView1, to DataGridView1 manually add two, respectively DataGridViewButtonColumnDel created above, DataGridViewButtonCellEdi two buttons template columns, ID, respectively Column1, Column2;
form1 background code is as follows:
a using System;
a using the System.Collections .Generic;
the using the System.ComponentModel;
the using the System.Data;
the using the System.Drawing;
the using the System.Text;
the using the System.Windows.Forms;

namespace two combined heads column headings redraw
{
  public partial class the Form1: Form1
  {
  int Top = 0;
  int left = 0;
  int height = 0;
  int width1 = 0;
  public the Form1 ()
  {
  the InitializeComponent ();
  }

  Private void the Form1_Load (SENDER Object, EventArgs E)
  {
  BindDataGridView ();
  HideCheckBoxCotrol ();
  HideCheckBoxCotrol1 ();
  }
  /// <Summary>
  /// binding test data to the DataGridView
  /// </ Summary>
  Private void BindDataGridView ()
  {
  the DataTable the DataTable dt = new new ();
  dt .Columns.Add ( "the Name", typeof (String));
  dt.Columns.Add ( "Age", typeof (String));

  for (int I = 0; I <. 5; I ++)
  {
  the DataRow dt = DR. the NewRow ();
  DR [0] = i.ToString ();
  DR [. 1] = i.ToString ();
  dt.Rows.Add (DR);

  }

  this.dataGridView1.DataSource = dt.DefaultView;
   
  }

  /// <Summary>
  /// head of the first column and the second column at redraw, plotted as a header
  /// </ summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
  {

  #region 重绘datagridview表头
  DataGridView dgv = (DataGridView)(sender);
  if (e.RowIndex == -1 && (e.ColumnIndex == 0 || e.ColumnIndex == 1))
  {
  if (e.ColumnIndex == 0)
  {
  top = e.CellBounds.Top;
  left = e.CellBounds.Left;
  height = e.CellBounds.Height;
  width1 = e.CellBounds.Width;
  }


  int width2 = this.dataGridView1.Columns[1].Width;

  Rectangle rect = new Rectangle(left, top, width1 + width2, e.CellBounds.Height);
  using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
  {
  //抹去原来的cell背景
  e.Graphics.FillRectangle(backColorBrush, rect);
  }
  using (Pen pen = new Pen(Color.White))
  {
  e.Graphics.DrawLine(pen, left + 1, top + 1, left + width1 + width2 - 1, top + 1);
  }
  using (Pen gridLinePen = new Pen(dgv.GridColor))
  {
  e.Graphics.DrawLine(gridLinePen, left, top, left + width1 + width2, top);
  e.Graphics.DrawLine(gridLinePen, left, top + height - 1, left + width1 + width2, top + height - 1);
  e.Graphics.DrawLine(gridLinePen, left, top, left, top + height);
  e.Graphics.DrawLine(gridLinePen, left + width1 + width2 - 1, top, left + width1 + width2 - 1, top + height);

  //计算绘制字符串的位置
  string columnValue = "";
  SizeF sf = e.Graphics.MeasureString(columnValue, e.CellStyle.Font);
  float lstr = (width1 + width2 - sf.Width) / 2;
  float rstr = (height / 2 - sf.Height);
  //画出文本框

  if (columnValue != "")
  {
  e.Graphics.DrawString(columnValue, e.CellStyle.Font,
  new SolidBrush(e.CellStyle.ForeColor),
  left + lstr,
  top + rstr,
  StringFormat.GenericDefault);
  }

  }
  e.Handled = true;


  }
  #endregion
  }
  /// <summary>
  /// Finally, the first column of cells in a row of buttons in the template DataGridViewButtonColumnDel transducer
  /// into system DataGridViewButtonCellAdd
  /// </ Summary>
  Private void HideCheckBoxCotrol ()
  {
  DataGridViewButtonCellAdd dvcType1 new new DataGridViewButtonCellAdd = ();
  dataGridView1.Rows [. 5 ] .Cells [ "Column1"] = dvcType1;

  }
  /// <Summary>
  the second column of cells in the last row /// DataGridViewButtonColumnEdi button template transducer
  /// system into the DataGridViewTextBoxCell
  /// </ Summary>
  Private HideCheckBoxCotrol1 void ()
  {
  the DataGridViewCell dvcType the DataGridViewTextBoxCell new new = ();
  dataGridView1.Rows [. 5] .Cells [ "Column2"] = dvcType;

  }
  }
}



Reproduced in: https: //www.cnblogs.com/kevinGao/archive/2012/01/29/2336482.html

Guess you like

Origin blog.csdn.net/weixin_34166472/article/details/93766927