怎么更改DataGridViewButtonColumn的文本值,原来显示的“停用”,我点击一下,他就变成“启用”,
答案:4 悬赏:0
解决时间 2021-04-27 10:20
- 提问者网友:萌萌小主
- 2021-04-26 22:33
跪谢,高手进,这个一问题搞了一天了,
最佳答案
- 二级知识专家网友:摧毁过往
- 2021-04-26 23:09
实现的具体方法如下:
1. 给DataGridView添加一列 DataGridViewButtonColumn,设置该列的属性如下:
DefaultCellStyle 的 NullValue 设置为 “启用”
UseColumnTextForButtonValue = False
其他属性自己根据需要设置
2. 在DataGridView的CellContentClick事件中,写入如下的类似代码:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//注释:
//dataGridView1.Columns[e.ColumnIndex] is DataGridViewButtonColumn 说明点击的列是DataGridViewButtonColumn列,当然你也根据e.ColumnIndex == 你的按钮列的索引来做
// e.RowIndex > -1 ,说明点击的不是列头
if (dataGridView1.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex > -1)
{
//获取当前被点击的单元格
DataGridViewButtonCell vCell =
(DataGridViewButtonCell)dataGridView1.CurrentCell;
if (vCell.Tag == null)
{
vCell.Value = "停用";
vCell.Tag = true;
}
}
}
1. 给DataGridView添加一列 DataGridViewButtonColumn,设置该列的属性如下:
DefaultCellStyle 的 NullValue 设置为 “启用”
UseColumnTextForButtonValue = False
其他属性自己根据需要设置
2. 在DataGridView的CellContentClick事件中,写入如下的类似代码:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//注释:
//dataGridView1.Columns[e.ColumnIndex] is DataGridViewButtonColumn 说明点击的列是DataGridViewButtonColumn列,当然你也根据e.ColumnIndex == 你的按钮列的索引来做
// e.RowIndex > -1 ,说明点击的不是列头
if (dataGridView1.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex > -1)
{
//获取当前被点击的单元格
DataGridViewButtonCell vCell =
(DataGridViewButtonCell)dataGridView1.CurrentCell;
if (vCell.Tag == null)
{
vCell.Value = "停用";
vCell.Tag = true;
}
}
}
全部回答
- 1楼网友:夢想黑洞
- 2021-04-27 02:08
我暂时保留我的看法!
- 2楼网友:悲观垃圾
- 2021-04-27 01:28
这个问题以前有很多人问过的,你可以找找:
基本就是在按钮点击事件中,判断if text 等于 停用 { text=启用} else {text=停用}
- 3楼网友:气场征服一切
- 2021-04-26 23:58
在单击事件里写:Button btn=((Button)sender).Text;
if(btn.Text=="启用")
{
btn.Text="停用";
}
else
{
btn.Text="启用";
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯