启用和禁用DataGridView按钮单元格

时间:2017-04-11 08:48:26

标签: c# winforms datagridview

我在一个名为serverList的Windows窗体中创建了一个DataGridView,然后添加了一列列,然后在每行的末尾我使用此代码添加了一个按钮单元

DataGridViewButtonColumn deleteBtn = new DataGridViewButtonColumn();
                deleteBtn.HeaderText = "Delete";
                deleteBtn.Text = "Delete";
                deleteBtn.Name = "deleteBtn";
                deleteBtn.UseColumnTextForButtonValue = true;
                serverList.Columns.Add(deleteBtn);

有没有办法让我将此按钮变灰或制作,以便用户无法点击它?

编辑: 我检查用这种方法点击按钮的时间:

private void serverList_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    try
    {
        switch (e.ColumnIndex)
        {
            case 8:
                try
                {
                    string filePath = @"C:\ProgramData\Server_Manager\default.xml";
                    int serverIndex = Int32.Parse(serverCell);

                    XmlDocument doc = new XmlDocument();
                    doc.Load(filePath);
                    XmlElement nodeToDelete = (XmlElement)doc.SelectSingleNode("/Servers/Server[serverIndex="+serverIndex+"]");
                    if (nodeToDelete != null)
                    {
                        nodeToDelete.ParentNode.RemoveChild(nodeToDelete);
                    }
                    doc.Save(filePath);

                    clearList();
                    populateList();
                }

                catch (Exception ex)
                {
                    string errorMsg = "Unable to Delete Nodes from XML:  " + ex;
                    errorBox(errorMsg, "Failed to Delete List Item", "error");
                }

                break;

2 个答案:

答案 0 :(得分:0)

您要查找的属性应位于" deleteBtn.Enabled"下,可能的值为true / false。

您可以随时在代码中设置此属性。

答案 1 :(得分:0)

解决方案:

而不是禁用按钮我在单击按钮时创建了错误:

function foo() { console.log(this.a); } var a = 10; foo();