禁用datagridview中的按钮列

禁用datagridview中的按钮列

本文介绍了禁用datagridview中的按钮列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据gridview与4列前2列是组合框列,第三列是文本框列,第4列是按钮列。在窗体加载我必须禁用整个按钮列的datagrid,之后我应该先选择保存三列并保存在数据库中的前三列保存在特定行中的按钮列应该启用。通过单击一个按钮,第三列应该保存在数据库中。
请多多帮忙帮我解决这个问题
这里是我使用的代码

  private void SATAddTemplate_Load(object sender,EventArgs e)
{
foreach(datagrdADDTEMP.Rows中的DataGridViewRow行)
{

DataGridViewButtonCell btn =(DataGridViewButtonCell)行。细胞[3];
btn.ReadOnly = true;
}
}
private void btnSaveSettings_Click(object sender,EventArgs e)
{
foreach(datagrdADDTEMP.Rows中的DataGridViewRow行)
{

DataGridViewButtonCell btn =(DataGridViewButtonCell)row.Cells [3];
btn.ReadOnly = false;
}
}


解决方案

有一些帮助可以设置 DataGridViewButtonColumn 中出现的按钮的启用属性。



您需要扩展 DataGridViewButtonColumn 以创建自己的具有禁用按钮的DataGridView列。 详细介绍了如何执行此操作。



该文章有很多代码,我鼓励你仔细看看,但你真正需要做的是将文章中的以下课程复制并粘贴到项目中:

- DataGridViewDisableButtonColumn

- DataGridViewDisableButtonCell



一旦你这样做,你可以添加 DataGridViewDisableButtonColumn 到您的DataGridView。使用您自定义列中公开的公开启用属性来设置每个单元格的Button控件的启用属性。由于您要设置列中所有按钮的启用属性,您可以编写一个辅助方法,循环遍历DataGridView中的所有行,并设置启用属性:

  private void SetDGVButtonColumnEnable(bool enabled){
foreach(DataGridViewRow dataGridView1.Rows中的行){
//设置DGV中第四列的Enabled属性。
((DataGridViewDisableButtonCell)row.Cells [3])。Enabled = enabled;
}
dataGridView1.Refresh();
}


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 dayshere 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;
           }
     }
解决方案

Here's some help with the problem of setting the Enabled property of the Buttons that appear in a DataGridViewButtonColumn.

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.

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

Once you do this you will be able to add DataGridViewDisableButtonColumns 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();
}

这篇关于禁用datagridview中的按钮列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 11:47