简介:
在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;
}
}
1.变量
- InfoColumnName是属性DataPropertyName设置的。
- ColumnIndex是要添加的列索引。
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; //要添加在哪一个列的索引
2.添加控件
dataGridView1.Controls.Add(chx);
3.获取大小
Rectangle rect = dataGridView1.GetCellDisplayRectangle(ColumnIndex, i, true);
4.大小设置
- 控件的宽,要根据实际需要设置宽。
- 控件的高,一般默认原高。
chx.Size = new Size((rect.Width / 3), rect.Height);
5.位置设置
- 控件的起点X,要根据相邻的起点位置加上相邻的宽。
- 控件的起点Y,一般默认原Y。
chx.Location = new Point(rect.Left + tbx.Width, rect.Top);
6.事件
- 先绑定,和正常控件事件一样,根据实际需要自行更改。
- 添加事件。
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;
}
}
}
}
1.Scroll事件
调用ControlRefresh()函数。
2.获取当前首行的索引
int curFirstIndex = dataGridView1.FirstDisplayedScrollingRowIndex;
3.获取当前显示的行数
int disCount = dataGridView1.DisplayedRowCount(true);
4.大小和位置设置
- Size要和AddControl()保持一致。
- Location要和AddControl()保持一致。
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;