DataGridView 单元格中添加按钮

yangbinkk 2007-12-30 10:24:07
今晚学的,说得不是很清楚
//单元格获得焦点
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{

if (e.ColumnIndex.Equals(this.dataGridView1.Columns["Company"].Index))
//判断单元格是否是"Company"列?
{

this.dataGridView1.Controls.Clear(); //移除所有控件
Button btn = new Button(); //创建Button btn
btn.Text = "..."; //设置button文字
btn.Font = new Font("Arial", 7); //设置文字格式
btn.Visible = true; //设置控件允许显示
btn.Width = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex,
e.RowIndex,true).Height; //获取单元格高并设置为btn的宽
btn.Height = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex,
e.RowIndex, true).Height; //获取单元格高并设置为btn的高
btn.Click += new EventHandler(btn_Click); //为btn添加单击事件
this.dataGridView1.Controls.Add(btn); //dataGridView1中添加控件btn
btn.Location = new System.Drawing.Point(((this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex,e.RowIndex,true).Right)-
(btn.Width)),
this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex,e.RowIndex,true).Y); //设置btn显示位置
//(this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex,e.RowIndex,true).Right)-(btn.Width)
//获取单元格方框右边位置减去btn的宽,得到btn在dataGridView1中的横坐标
//this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex,e.RowIndex,true).Y
//获取单元格的纵坐标并设为btn在dataGridView1中的纵坐标

}

}
void btn_Click(object sender, EventArgs e)
{
Form f = new Form();
f.ShowDialog(this.dataGridView1);
}

...全文
609 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
超级奶爸1231 2010-12-06
  • 打赏
  • 举报
回复
当拖动滚动条的时候 问题完全暴漏了
b-junsong 2010-06-21
  • 打赏
  • 举报
回复
butten 是能添加 但是如果主窗体变化 butten的位置怎么处理?
Free_Windy 2008-03-27
  • 打赏
  • 举报
回复
好贴!
yangbinkk 2007-12-30
  • 打赏
  • 举报
回复
有点BUG
以下修正
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex.Equals(this.dataGridView1.Columns["Company"].Index))
{

//this.dataGridView1.Controls.Clear(); 此处的清除控件应该放在CellLeave事件中处理
//
Button btn = new Button();
btn.Text = "...";
btn.Font = new Font("Arial", 7);
btn.Visible = true;
btn.Width = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Height;
btn.Height = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Height;

this.dataGridView1.Controls.Add(btn);
btn.Location = new System.Drawing.Point((this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true)
.Right - btn.Width), this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Y);
btn.Click += new EventHandler(btn_Click);
}

}
void btn_Click(object sender, EventArgs e)
{
Form f = new Form();
f.ShowDialog(this.dataGridView1);
}

private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
{
this.dataGridView1.Controls.Clear(); //清除集合中控件
}
DataGridView控件用法合集 1. DataGridView当前的单元格属性取得、变更 2. DataGridView编辑属性 3. DataGridView最下面一列新追加行非表示 4. DataGridView判断当前选行是否为新追加的行 5. DataGridView删除行可否设定 6. DataGridView行列不表示和删除 7. DataGridView行列宽度高度设置为不能编辑 8. DataGridView行高列幅自动调整 9. DataGridView指定行列冻结 10. DataGridView列顺序变更可否设定 11. DataGridView行复数选择 12. DataGridView选择的行、列、单元格取得 13. DataGridView指定单元格是否表示 14. DataGridView表头部单元格取得 15. DataGridView表头部单元格文字列设定 16. DataGridView选择的部分拷贝至剪贴板 17. DataGridView粘贴 18. DataGridView单元格上ToolTip表示设定(鼠标移动到相应单元格上时,弹出说明信息) 19. DataGridView的ContextMenuStrip属性 20. DataGridView指定滚动框位置 21. DataGridView手动追加列 22. DataGridView全体分界线样式设置 23. DataGridView根据单元格属性更改显示内容 24. DataGridView新追加行的行高样式设置る 25. DataGridView新追加行单元格默认值设置 26. DataGridView单元格数据错误标签表示 27. DataGridView单元格内输入值正确性判断 28. DataGridView单元格输入错误值事件的捕获 29. DataGridView行排序(点击列表头自动排序的设置) 30. DataGridView自动行排序(新追加值也会自动排序) 31. DataGridView自动行排序禁止情况下的排序 32. DataGridView指定列指定排序 33. DataGridView单元格样式设置 34. DataGridView文字表示位置的设定 35. DataGridView单元格内文字列换行 36. DataGridView单元格DBNull值表示的设定 37. DataGridView单元格样式格式化 38. DataGridView指定单元格颜色设定 39. DataGridView单元格文字字体设置 40. DataGridView根据单元格值设定单元格样式 41. DataGridView设置单元格背景颜色 42. DataGridView行样式描画 43. DataGridView显示行号 44. DataGridView焦点所在单元格焦点框不显示的设定 45. DataGridView显示选择框CheckBox 46. DataGridView显示下拉框ComboBox 47. DataGridView单击打开下拉框 48. DataGridView显示按钮 49. DataGridView显示链接 50. DataGridView显示图像 51. DataGridView编辑单元格控件取得 52. DataGridView输入自动完成 53. DataGridView单元格编辑时键盘KEY事件取得 54. DataGridView下拉框(ComboBox)单元格编辑时事件取得 55. DataGridView下拉框(ComboBox)单元格允许文字输入设定 56. DataGridView根据值不同在另一列显示相应图片 57. DataGridView显示进度条(ProgressBar) 58. DataGridView添加MaskedTextBox 59. DataGridViewEnter键按下焦点移至旁边的单元格 60. DataGridView行集合化(Group)

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧