在DataGridView的单元格中动态添加多个控件。例如在DataGridViewTextBox单元格中,添加CheckBox和Button控件。主题思路就是一个动态控件的大小,位置,显示,事件设置,和平常控件一样使用。
代码下载链接:https://download.csdn.net/download/c_gyl/10861487。
如果不熟悉此控件的使用,参考链接:https://blog.csdn.net/C_gyl/article/details/85067599。
如下图:
AddControl()动态添加控件,绑定事件。
//添加按钮
private void AddControl()
{
dataGridView1.Columns[InfoColumnName].Width = 70;
for (int i = 0; i < Data.Length; i++)
{
TextBox tbx = new TextBox();
CheckBox chx = new CheckBox(); //添加CheckBox
Button btn = new Button(); //添加Button
//添加
dataGridView1.Controls.Add(chx);
dataGridView1.Controls.Add(btn);
//获取大小
Rectangle rect = dataGridView1.GetCellDisplayRectangle(ColumnIndex, i, true);
//大小设置
tbx.Size = new Size((rect.Width / 3), rect.Height);
chx.Size = new Size((rect.Width / 3), rect.Height);
btn.Size = new Size((rect.Width / 3), rect.Height);
//位置设置
tbx.Location = new Point(rect.Left, rect.Top );
chx.Location = new Point(rect.Left + tbx.Width, rect.Top);
btn.Location = new Point(rect.Left + tbx.Width + chx.Width, rect.Top);
//绑定事件
chx.CheckedChanged += new EventHandler(chx_CheckedChanged);
btn.Click += new EventHandler(btn_Click);
//属性设置
chx.Tag = i;
Chx[i] = chx;
btn.Tag = i;
Btn[i] = btn;
}
}
DataTable Table = new DataTable(); //数据,绑定DataGridView
structData[] Data = new structData[10]; //数据,长度自行更改
string NoColumnName = Enum.GetName(typeof(enumData), 0); //数据源列名
string InfoColumnName = Enum.GetName(typeof(enumData), 1); //数据源列名
CheckBox[] Chx = new CheckBox[10]; //按钮CheckBox
Button[] Btn = new Button[10]; //按钮Button
int ColumnIndex = 1; //要添加在哪一个列的索引
dataGridView1.Controls.Add(chx);
Rectangle rect = dataGridView1.GetCellDisplayRectangle(ColumnIndex, i, true);
chx.Size = new Size((rect.Width / 3), rect.Height);
chx.Location = new Point(rect.Left + tbx.Width, rect.Top);
chx.CheckedChanged += new EventHandler(chx_CheckedChanged);
private void chx_CheckedChanged(object sender, EventArgs e)
{
CheckBox ch = sender as CheckBox;
int index = int.Parse(ch.Tag.ToString());
bool value = ch.Checked;
//Do Something
}
当用到滚动时,还需要把对应的控件显示出来。
//Scroll事件
private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
{
ControlRefresh(e);
}
//更新显示
//1.滚动时需要启用,否则按钮会看不到
private void ControlRefresh(ScrollEventArgs e)
{
//垂直方向
if (e.ScrollOrientation == ScrollOrientation.VerticalScroll)
{
int curFirstIndex = dataGridView1.FirstDisplayedScrollingRowIndex;
int disCount = dataGridView1.DisplayedRowCount(true);
for (int j = 0; j < Data.Length; j++)
{
Chx[j].Visible = false;
Btn[j].Visible = false;
}
for (int i = curFirstIndex; i < curFirstIndex + disCount; i++)
{
Rectangle rect = dataGridView1.GetCellDisplayRectangle(ColumnIndex, i, true);
Chx[i].Size = new Size((rect.Width / 3), rect.Height);
Chx[i].Location = new Point(rect.Left + (rect.Width / 3), rect.Top);
Chx[i].Visible = true;
Btn[i].Size = new Size((rect.Width / 3), rect.Height);
Btn[i].Location = new Point(rect.Left + (rect.Width * 2 / 3), rect.Top);
Btn[i].Visible = true;
}
}
else //水平方向
{
int curFirstIndex = dataGridView1.FirstDisplayedScrollingColumnIndex;
int disCount = dataGridView1.DisplayedColumnCount(true);
int ctlColumn = 1; //控件所在列的位置
if ( ctlColumn + 1 <= curFirstIndex + disCount)
{
for (int i = 0; i < Data.Length; i++)
{
Rectangle rect = this.dataGridView1.GetCellDisplayRectangle(ctlColumn, i, true);
Chx[i].Size = new Size((rect.Width / 3), rect.Height);
Chx[i].Location = new Point(rect.Left + (rect.Width / 3), rect.Top);
Chx[i].Visible = true;
Btn[i].Size = new Size((rect.Width / 3), rect.Height);
Btn[i].Location = new Point(rect.Left + (rect.Width * 2 / 3), rect.Top);
Btn[i].Visible = true;
}
}
else
{
for (int j = 0; j < Data.Length; j++)
{
Chx[j].Visible = false;
Btn[j].Visible = false;
}
}
}
}
调用ControlRefresh()函数。
int curFirstIndex = dataGridView1.FirstDisplayedScrollingRowIndex;
int disCount = dataGridView1.DisplayedRowCount(true);
Rectangle rect = dataGridView1.GetCellDisplayRectangle(ColumnIndex, i, true);
Chx[i].Size = new Size((rect.Width / 3), rect.Height);
Chx[i].Location = new Point(rect.Left + (rect.Width / 3), rect.Top);
Chx[i].Visible = true;
Btn[i].Size = new Size((rect.Width / 3), rect.Height);
Btn[i].Location = new Point(rect.Left + (rect.Width * 2 / 3), rect.Top);
Btn[i].Visible = true;