Results 1 to 5 of 5

Thread: [RESOLVED] Change datagridview column type to checkbox (Not add)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Resolved [RESOLVED] Change datagridview column type to checkbox (Not add)

    Hello, I'm tying to change my dgv's column type to checkbox. I tried this:

    vb.net Code:
    1. programs_dgv.Columns[1] = new DataGridViewCheckBoxColumn();
    2. programs_dgv.Columns[1].HeaderText = "Seç";
    3. programs_dgv.Columns[1].TrueValue = 1;
    4. programs_dgv.Columns[1].FalseValue = 2;

    However the syntax is invalid. How can I fix this?

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

    Re: Change datagridview column type to checkbox (Not add)

    You can't change an existing column to a different type. What you're doing there replacing an existing column with a new column of a different type. The question is why are you doing that? If you want a check box column then why aren't you just adding a check box column in the first place?

    I'm guessing that what's happening is that you're binding a DataTable or some other list and a text box column is being generated by default because the data is not actually Boolean values. If that is the case then what you should be doing is adding that check box column before you bind. That way, there's no need to have a column you don't need created and then discarded. You should probably be creating the column in the designer too. Just make sure that you set the DataPropertyName of the column so that it knows which column/property of the data source to bind to.

    For an example, follow the CodeBank link in my signature below and check out my thread on how to add a combo box column to a DataGridView. The same principle applies regardless of the type of column.

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

    Re: Change datagridview column type to checkbox (Not add)

    For interest's sake more than anything, the issue with the code you have would be the fact that the indexer for that Columns property returns a DataGridViewColumn reference but you are trying to access properties of the DataGridViewCheckBoxColumn type. You'd need to cast in order to access members of that type but, if you do as I suggested above, then you won't need that code at all.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Re: Change datagridview column type to checkbox (Not add)

    Thank you for your reply. After your suggestions I added columns from ide and here's my final code:

    C# Code:
    1. programs_dgv.Columns[0].DataPropertyName = "id";
    2. programs_dgv.Columns[1].DataPropertyName = "id_isSelected";
    3. programs_dgv.Columns[2].DataPropertyName = "txt_programName";
    4. programs_dgv.Columns[3].DataPropertyName = "id_build";
    5. programs_dgv.Columns[4].DataPropertyName = "txt_fullCommand";

    So I fixed this. Again thank you for your help.

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

    Re: [RESOLVED] Change datagridview column type to checkbox (Not add)

    If you added the columns in the designer then you should set the DataPropertyName properties in the designer too.

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
  •  



Click Here to Expand Forum to Full Width