禁用数据网格视图中的按钮列 [英] Disabling the button column in the datagridview
问题描述
我有一个包含 4 列的数据网格视图,前 2 列是组合框列,第三列是文本框列,第四列是按钮列.在表单加载中,我必须禁用数据网格的整个按钮列,然后我应该先选择三列,保存后将前三列保存在数据库中,特定行中的按钮列应启用.前三列应通过单击按钮保存在数据库中.请帮我解决这个问题很多天这是我使用的代码
i have a data gridview with 4 columns first 2 columns are combobox columns, third column is textbox column and 4th column is button column.In form load i have to disable the entire button column of datagrid and after this i should select first three columns and save these first three columns in database after saving this the button column in the particular row should enable.first three columns should be saved in databese by clicking a button. Please help me im struck up with this problem from many days here is the code which i used
private void SATAddTemplate_Load(object sender, EventArgs e)
{
foreach (DataGridViewRow row in datagrdADDTEMP.Rows)
{
DataGridViewButtonCell btn = (DataGridViewButtonCell)row.Cells[3];
btn.ReadOnly = true;
}
}
private void btnSaveSettings_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in datagrdADDTEMP.Rows)
{
DataGridViewButtonCell btn = (DataGridViewButtonCell)row.Cells[3];
btn.ReadOnly = false;
}
}
推荐答案
这里有一些关于设置 DataGridViewButtonColumn
中出现的按钮的 Enabled
属性的问题的帮助.
Here's some help with the problem of setting the Enabled
property of the Buttons that appear in a DataGridViewButtonColumn
.
您需要扩展 DataGridViewButtonColumn
以创建您自己的带有禁用按钮的 DataGridView 列.MSDN 上的这篇文章 详细介绍了如何执行此操作.
You'll need to extend DataGridViewButtonColumn
to create your own DataGridView column with disable-able buttons. This article on MSDN details how to do this.
这篇文章有很多代码,我鼓励你仔细看看,但你真正需要做的就是将文章中的以下类复制并粘贴到你的项目中:
-- DataGridViewDisableButtonColumn
-- DataGridViewDisableButtonCell
The article has a lot of code, and I encourage you to take a close look, but all you really need to do is copy and paste into your project the following classes found in the article:
-- DataGridViewDisableButtonColumn
-- DataGridViewDisableButtonCell
完成此操作后,您将能够将 DataGridViewDisableButtonColumn
添加到您的 DataGridView.使用在自定义列中公开的公共 Enabled
属性来设置每个单元格的 Button 控件的 Enabled
属性.由于您想设置列中所有按钮的 Enabled
属性,您可以编写一个辅助方法来循环遍历 DataGridView 中的所有行并设置 Enabled
属性:
Once you do this you will be able to add DataGridViewDisableButtonColumn
s to your DataGridView. Use the public Enabled
property exposed in your custom column to set the Enabled
property of each cell's Button control. Since you want to set the Enabled
property of all the Buttons in the column you can write a helper method that loops through all rows in your DataGridView and sets the Enabled
property:
private void SetDGVButtonColumnEnable(bool enabled) {
foreach (DataGridViewRow row in dataGridView1.Rows) {
// Set Enabled property of the fourth column in the DGV.
((DataGridViewDisableButtonCell)row.Cells[3]).Enabled = enabled;
}
dataGridView1.Refresh();
}
这篇关于禁用数据网格视图中的按钮列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!