Results 1 to 4 of 4

Thread: Unable to edit checkbox column at runtime in datagridview

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2014
    Posts
    5

    Question Unable to edit checkbox column at runtime in datagridview

    I have a datagridview with a checkbox column.The datagridview is bound to a datasource through code using the method
    datagridview1.datasource=my Dataset.

    The problem is while I want the rest of the grid to remain readonly,I want the checkbox column to remain editable.I have tried various methods and events like 'grid MouseDown,MouseClick,CellClick,etc and properties such as editable=true and all that,based on results of internet searches.
    A solution would be appreciated.
    Another problem is that at the very end of the gridview,there are two columns which I want remain hidden.While I know how to hide a column,despite all my tries,I could not locate the index of these columns.
    Also,if a grid is bound through code,how do I center-justify all the cells?

    Thanks in advance,
    Khalid.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: Unable to edit checkbox column at runtime in datagridview

    You don't need to handle any events. If you want all the other columns to be read-only then you set the ReadOnly properties of those columns to True, leaving it False for your check box column.

    In relation to all your issues, how about you add all the columns in the designer and set the AutoGenerateColumns propert of the grid to False? That way you can configure everything you want in the designer and not have to worry about code. You just set the DataPropertyName of each column to tell it which data source column to bind to.

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2014
    Posts
    5

    Re: Unable to edit checkbox column at runtime in datagridview

    Quote Originally Posted by jmcilhinney View Post
    You don't need to handle any events. If you want all the other columns to be read-only then you set the ReadOnly properties of those columns to True, leaving it False for your check box column.
    Thanks,I'll try that.

    Quote Originally Posted by jmcilhinney View Post
    In relation to all your issues, how about you add all the columns in the designer and set the AutoGenerateColumns propert of the grid to False? That way you can configure everything you want in the designer and not have to worry about code. You just set the DataPropertyName of each column to tell it which data source column to bind to.
    That is exactly what I had done earlier.I Had designed the DGV in the designer,but later,due to a change in the program configuration,I needed to refresh the DGV repeatedly and apparently that is not easy if the the DGV is bound through the designer.To get around that,I loaded the data in the DGV through code and its almost working like a dream except that now the checkbox column has disappeared.
    Due to these developments,I'll need to rephrase my query.Now I have the difficult task of rendering a checkbox column at runtime with all checkboxes checked at start and any checkbox being able to be unchecked at will with a corresponding event being also available.

    I know that it is a difficult task but any help will be appreciated.I have explored options on the internet about creating the column at runtime but could not find a viable solution.Maybe the following could be explored,though I find it rather daunting:
    http://www.vbforums.com/showthread.p...-column-header

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: Unable to edit checkbox column at runtime in datagridview

    Quote Originally Posted by khldindia View Post
    That is exactly what I had done earlier.I Had designed the DGV in the designer,but later,due to a change in the program configuration,I needed to refresh the DGV repeatedly and apparently that is not easy if the the DGV is bound through the designer.
    I didn't say anything about binding the grid in the designer. I said that you should add the columns in the designer and tell each one which column of the data source they should bind to. At run time, you set the AutoGenerateColumns property to False and set the DataSource, i.e. bind the data to the grid. For each column in the data source, it will bind to the grid column with the appropriate DataPropertyName or it will not bind at all. That way, there's no need to try to hide any columns you don't want the user to see because they won't be created in the first place.

Posting Permissions

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



Click Here to Expand Forum to Full Width