怎样在DataGridView的Cell中添加多个按钮 怎样在DataGridView的Cell中添加多个按钮,并且能实现在DataGridView出现滚动条后,Button按钮能够和Cell连动? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //先创建一个Buttonprivate Button btn = new Button();private int m_RowIndex; //所在行的索引先给dataGridView添加一个CellEnter事件,收到输入焦点时发生。private void dataGridView_CellEnter(object sender, DataGridViewCellEventArgs e){ this.dataGridView.Controls.Clear();//移除所有控件 btn.Click -= new EventHandler(btnTimeStart_Click); //判断单元格是否是"按钮"列 if (e.ColumnIndex.Equals(dataGridView.Columns["按钮"].Index)) { //Button btn = new Button();//创建Buttonbtn btn.Text = "开始";//设置button文字 btn.Font = new Font("宋体", 9);//设置文字格式 btn.FlatStyle = FlatStyle.Flat; btn.Visible = true;//设置控件允许显示 //获取单元格宽并设置为btn的宽 btn.Width = dataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Width; //获取单元格高并设置为btn的高 btn.Height = dataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Height; //为btn添加单击事件 btn.Click += new EventHandler(btnTimeStart_Click); //dataGridView中添加控件btn dataGridView.Controls.Add(btn); //记录下所在行的索引 m_RowIndex = e.RowIndex; //设置btn显示位置 btn.Location = new Point(((dataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Right) - (btn.Width)), dataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Y); }}然后再添加dataGridView的RowPostPaint事件,单元格行级绘制时发生。private void dataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e){ //设置btn显示位置 btn.Location = new Point(((dataGridView.GetCellDisplayRectangle(ColumnIndex, RowIndex, true).Right) - (btn.Width)), dataGridView.GetCellDisplayRectangle(ColumnIndex, RowIndex, true).Y);} 在cell中添加控件,控件可以包含子空间,添加几个按钮都行。 Button btn1 = new Button();btn1.Name = "btn";btn1.Text = "btn";btn1.Click+=new EventHandler(btn1_Click);this.dataGridView1.Controls.Add(btn1);public void btn1_Click(object sender, EventArgs e){ Button btn = (Button)(sender);}或DataGridViewButtonColumn dbc = new DataGridViewButtonColumn(); 有一些问题,首先这个Cell里面会有不定的个数的按钮,用户在Cell中添加按钮就会增加一个按钮 2楼能说详细一点吗,怎么在Cell中直接添加控件?在DataGridView.Rows[i].Cells[j]中没有Add控件的方法啊!能说明一下吗?感谢!!! combobox如何从集合里更新数据 登录框与主界面同时出现 关于 Socket UDP 问题(公网上UDP通讯)^^^^^^^(100分) 50分求VS2005如何部署Windows应用程序 安装VS.NET前是否要先安装FrontPage或Office? C#入门经典(第3版) C#窗体间的消息传输和实时响应 传递一个List<T>给类型object的变量,利用反射如何读取List<T>里面的值和数据 索引与数据绑定是否可以同时使用 这个程序总在出错,可是我把它按照MS的例程改了还是。。。。。大家帮帮我把! 求各位大神帮忙。。。。进来看看 C# 关于滚动条
//先创建一个Button
private Button btn = new Button();
private int m_RowIndex; //所在行的索引
先给dataGridView添加一个CellEnter事件,收到输入焦点时发生。private void dataGridView_CellEnter(object sender, DataGridViewCellEventArgs e)
{
this.dataGridView.Controls.Clear();//移除所有控件
btn.Click -= new EventHandler(btnTimeStart_Click);
//判断单元格是否是"按钮"列
if (e.ColumnIndex.Equals(dataGridView.Columns["按钮"].Index))
{
//Button btn = new Button();//创建Buttonbtn
btn.Text = "开始";//设置button文字
btn.Font = new Font("宋体", 9);//设置文字格式
btn.FlatStyle = FlatStyle.Flat;
btn.Visible = true;//设置控件允许显示
//获取单元格宽并设置为btn的宽
btn.Width = dataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Width;
//获取单元格高并设置为btn的高
btn.Height = dataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Height;
//为btn添加单击事件
btn.Click += new EventHandler(btnTimeStart_Click);
//dataGridView中添加控件btn
dataGridView.Controls.Add(btn);
//记录下所在行的索引
m_RowIndex = e.RowIndex;
//设置btn显示位置
btn.Location = new Point(((dataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Right) -
(btn.Width)), dataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Y);
}
}
然后再添加dataGridView的RowPostPaint事件,单元格行级绘制时发生。private void dataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
//设置btn显示位置
btn.Location = new Point(((dataGridView.GetCellDisplayRectangle(ColumnIndex, RowIndex, true).Right) - (btn.Width)), dataGridView.GetCellDisplayRectangle(ColumnIndex, RowIndex, true).Y);
}
btn1.Name = "btn";
btn1.Text = "btn";
btn1.Click+=new EventHandler(btn1_Click);
this.dataGridView1.Controls.Add(btn1);
public void btn1_Click(object sender, EventArgs e)
{
Button btn = (Button)(sender);
}或
DataGridViewButtonColumn dbc = new DataGridViewButtonColumn();