Windows form DataGrid buttons
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Windows form DataGrid buttons

  1. #1
    Join Date
    Jan 2009
    Posts
    4

    Question Windows form DataGrid buttons

    I have a datagridview which has its first column of button field. There is another column called "Fee" which can be - ontime or late. I want to disable button for that row in the datagrid which has Fee as "ontime" and enable button when fee is late during the datagrid load or cell formatting event.

    Any ideas of how can I approach on this?
    Thanks.

  2. #2
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Windows form DataGrid buttons

    unless I'm missing something, this should do it:

    Code:
            private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                if (dataGridView1.Rows[e.RowIndex].Cells["fee"].Value.ToString() == "ontime")
                    dataGridView1.Rows[e.RowIndex].Cells["button"].ReadOnly = true;
                else if (dataGridView1.Rows[e.RowIndex].Cells["fee"].Value.ToString() == "late")
                    dataGridView1.Rows[e.RowIndex].Cells["button"].ReadOnly = false;
            }
    It's not a bug, it's a feature!

  3. #3
    Join Date
    Jan 2009
    Posts
    4

    Unhappy Re: Windows form DataGrid buttons

    Thanks for the reply but sorry it did not work.

    What I want is that the button column for row having fee as "ontime" to be disabled when the form loads. And when the fee is "late", button should be enabled so that they can click on it which then will open another form.

    Thanks.

  4. #4
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Windows form DataGrid buttons

    if the column is readonly, it is disabled.
    AFAIK, it is not possible to "gray out" a button in a DataGridViewButtonColumn, which is what I think you're looking for.
    It's not a bug, it's a feature!

  5. #5
    Join Date
    Jan 2009
    Posts
    4

    Re: Windows form DataGrid buttons

    I tried the same thing you posted but when the datagrid loaded on the form, I could still click on the button cloumn for which the fee was "ontime" and it opened another form. I don't care if it not greyed but it is not being disabled.

    Thanks

  6. #6
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Windows form DataGrid buttons

    You need to make sure that
    Code:
    if (dataGridView1.Rows[e.RowIndex].Cells["fee"].Value.ToString() == "ontime")
    will pass evaluation. In other words, the value of the "fee" column (or whatever it's called) needs to be either "ontime" or "late" - these are just examples as I don't know the exact values you are assigning them.
    Also, my example assumed that the other column was called "button" - this needs to be changed to the correct name if it is not...

    Also, try examining the value of the fee column by debugging the CellFormatting eventhandler...
    It's not a bug, it's a feature!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center