Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
647 views
in Technique[技术] by (71.8m points)

c# - Disabling the button column in the datagridview

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;
           }
     }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...