winform中怎樣使DataGridView的某一列可以添加兩個Button控件


今天在網上逛的時候,看到了一個童靴提的這個問題,看了帖子,發現樓主最終給出了自己的解決方案,感覺還不錯,因此將帖子的內容整理了下,轉出來了

解決方案的思路是這樣:分別創建三個新的按鈕模板列,第一個顯示刪除圖片,第二個顯示編輯圖片,第三個顯示添加圖片.看代碼
第一個按鈕模板列的代碼:
using System;
using System.Windows.Forms;

namespace 兩列合並重繪列標題頭
{
  public class DataGridViewButtonColumnDel : DataGridViewColumn
  {
  public DataGridViewButtonColumnDel()
  {
  this.CellTemplate = new DataGridViewButtonCellDel();
  this.HeaderText = "button";
  }
  }
}


using System;
using System.Windows.Forms;
using System.Drawing;
namespace 兩列合並重繪列標題頭
{
  public class DataGridViewButtonCellDel : DataGridViewButtonCell
  {
  protected override void 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);

  }
  }
}
第二個按鈕模板列的代碼:

using System;
using System.Windows.Forms;

namespace 兩列合並重繪列標題頭
{
  public class DataGridViewButtonColumnEdi : DataGridViewColumn
  {
  public DataGridViewButtonColumnEdi()
  {
  this.CellTemplate = new DataGridViewButtonCellEdi();
  this.HeaderText = "button";
  }
  }
}

using System;
using System.Windows.Forms;
using System.Drawing;
namespace 兩列合並重繪列標題頭
{
  public class DataGridViewButtonCellEdi : DataGridViewButtonCell
  {
  protected override void 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.imgEdit_x16;
graphics.DrawImage(_img, cellBounds.Location.X + 5, cellBounds.Location.Y + 3, _img.Width, _img.Height);

  }
  }
}

第三個按鈕模板列的代碼:
using System;
using System.Windows.Forms;

namespace 兩列合並重繪列標題頭
{
  public class DataGridViewButtonColumnAdd : DataGridViewColumn
  {
  public DataGridViewButtonColumnAdd()
  {
  this.CellTemplate = new DataGridViewButtonCellAdd();
  this.HeaderText = "button";
  }
  }
}

using System;
using System.Windows.Forms;
using System.Drawing;
namespace 兩列合並重繪列標題頭
{
  public class DataGridViewButtonCellAdd : DataGridViewButtonCell
  {
  protected override void 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.imgAdd_x16;
graphics.DrawImage(_img, cellBounds.Location.X + 5, cellBounds.Location.Y + 3, _img.Width, _img.Height);

  }
  }
}

上面的代碼幾乎是一樣的,就是紅色部份載入了不同的圖片。
圖片是通過資源文件引入項目的。方法是:打開資源管理器,展開“Properties”節點(這個節點默認是隱藏的)。雙擊Resources.resx,點擊"添加資源",選擇“添加現有資源”;添加三張圖片:imgDelete_x16.png,imgEdit_x16,imgAdd_x16.png,

之后在form1上拖一個DataGridView1,給DataGridView1手動添加兩列,分別是上面創建的DataGridViewButtonColumnDel,DataGridViewButtonCellEdi 這兩個按鈕模板列,ID分別為Column1,Column2;
form1的后台代碼如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 兩列合並重繪列標題頭
{
  public partial class Form1 : Form
  {
  int top = 0;
  int left = 0;
  int height = 0;
  int width1 = 0;
  public Form1()
  {
  InitializeComponent();
  }

  private void Form1_Load(object sender, EventArgs e)
  {
  BindDataGridView();
  HideCheckBoxCotrol();
  HideCheckBoxCotrol1();
  }
  /// <summary>
  /// 給DataGridView綁定測試數據
  /// </summary>
  private void BindDataGridView()
  {
  DataTable dt = new DataTable();
  dt.Columns.Add("Name", typeof(string));
  dt.Columns.Add("Age", typeof(string));

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

  }

  this.dataGridView1.DataSource = dt.DefaultView;
   
  }

  /// <summary>
  /// 把第一列和第二列的頭部重繪一下,繪成一個表頭
  /// </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>
  /// 最后一行的第一列單元格中的DataGridViewButtonColumnDel按鈕模板換
  /// 成系統的DataGridViewButtonCellAdd
  /// </summary>
  private void HideCheckBoxCotrol()
  {
  DataGridViewButtonCellAdd dvcType1 = new DataGridViewButtonCellAdd();
  dataGridView1.Rows[5].Cells["Column1"] = dvcType1;

  }
  /// <summary>
  /// 最后一行的第二列單元格中的DataGridViewButtonColumnEdi按鈕模板換
  /// 成系統的DataGridViewTextBoxCell
  /// </summary>
  private void HideCheckBoxCotrol1()
  {
  DataGridViewCell dvcType = new DataGridViewTextBoxCell();
  dataGridView1.Rows[5].Cells["Column2"] = dvcType;

  }
  }
}




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM