-1

I have made a DataGridView in a Windows Form with the Name serverListand then added a selection of columns then at the end of each row I have added a button cell using this code

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

is there a way for me to grey this button out or make it so the user can't click it?

EDIT: I check when the button is clicked with this method:

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;
LarsTech
  • 80,625
  • 14
  • 153
  • 225
Logan Young
  • 83
  • 3
  • 12
  • 1
    [Possible duplicate of Disabling the button column in the datagridview](http://stackoverflow.com/questions/12525305/disabling-the-button-column-in-the-datagridview) – P. Pat Apr 11 '17 at 09:16
  • What criteria/logic are you using to determine if a button should or should not be enable? – JohnG Apr 11 '17 at 14:28
  • I want the button to only be clicked once or accept a value eg a 1 or 0 to show its been clicked by someone else – Logan Young Apr 11 '17 at 14:35
  • You mean like a CheckBox column? – JohnG Apr 11 '17 at 14:39
  • No I just want the button to be visible but stop someone clicking the button to stop people deleting rows while others are using them – Logan Young Apr 11 '17 at 14:40
  • Unfortunately, to make the button disabled or invisible is going to involve some extra work, as you cannot simply set these properties as you would a normal button. As per your comment _”stop people deleting rows while others are using them”_ ... how would you know when others are using them? When the deletion fails? – JohnG Apr 11 '17 at 14:53
  • I currently have a value being sent to a server when an edit button is clicked and this is then broadcast to all clients letting them know someone is using it – Logan Young Apr 11 '17 at 15:10
  • I assume a value is also sent to the server and all clients when an item is no longer being edited? Do the clients keep an updated list of these “edited” items? How would the client check to see if item x is being edited? Where is the info stored or retrieved? – JohnG Apr 11 '17 at 15:29
  • when the client retrieves the value it is stored in an XML doc locally which is then added to the list when a refresh is triggered (Every time a value is received) and yes a 0 is sent out when the edit form is closed – Logan Young Apr 11 '17 at 15:33
  • Then simply check this locally stored XML doc and see if the item x is being “edited”. If item x is NOT being “edited” then delete item x. If item x IS being “edited” then send an error message to the user that item x is being “edited”. The thing I do not understand is the button… it appears you want to “Disable” this button if item x is being “edited”, however it appears that this check is made AFTER the user has already pressed the button. – JohnG Apr 11 '17 at 16:00
  • Updating a button to “Disabled” every time an item is “edited” seems like a waste of effort especially if this client is not trying to delete/use that item. So simply checking this after the user clicks the button and letting the user know that the item is currently being used would appear to be sufficient. – JohnG Apr 11 '17 at 16:00
  • If you must “Disable” these buttons, then you will not only have to write a wrapper for the `DataGridViewButtonCell` to accommodate this “Enabled” property, but also you will have to update these buttons whenever an item is “edited”. Again, this seems like a waste of effort to disable these buttons when the client may not be using them. I hope that makes sense. – JohnG Apr 11 '17 at 16:01
  • I see what you are saying. Would it be easier to check the XML when the button is clicked then simply have a pop-up or error message of some kind saying its in use? – Logan Young Apr 11 '17 at 16:05
  • Absolutely, do not do anything until the user initiates it. When the user presses the “Delete” button, THEN check to see if the item is being edited, if not delete the item. If the item IS being “edited”, then you could simply ignore the button click and not inform the user. I will not argue this is not very user friendly but would certainly indicate to the user that the button is “Disabled” for some reason. A nice message indicating the problem would be more user friendly. – JohnG Apr 11 '17 at 16:12
  • Ok that would simplify things I think. Thanks for the help – Logan Young Apr 11 '17 at 16:13
  • Good luck, I am confident you will get it working to your needs. – JohnG Apr 11 '17 at 16:14
  • Don't mark titles "Solved" and put the answer in the question. Just post your own answer and check mark it. – LarsTech Apr 11 '17 at 16:23

3 Answers3

0

The Property you are looking for should be found under "deleteBtn.Enabled", possible Values are true/false.

You can set this property at any time in your code.

kurdy
  • 441
  • 4
  • 15
  • When trying to use this I get an error saying there is no definition for `Enable` under `DataGridViewButtonColumn` – Logan Young Apr 11 '17 at 10:16
  • It looks like, you have to put some custom code into it, because it is not supported. Here is a fitting MSDN article with an example: https://msdn.microsoft.com/en-us/library/ms171619.aspx – kurdy Apr 11 '17 at 10:18
0

Solution:

Rather than disable the button I instead created an error when the button is clicked:

MessageBox.Show("Server Currently Being Edited by Another User");

Logan Young
  • 83
  • 3
  • 12
0

I went a different route with this as I couldn't find an answer anywhere when using the CellContentClick event.

I used an int List to store e.index when the button was clicked and the process successfully began (from a Y/N message box).

When CellContentClick is entered I first check to see if the row index was in the list and to not show the message box again to begin processing until the process finished. I also disabled the button so it greyed out on the form.

When it the process finishes, the button is enabled again and e.index is removed from the List.