none
Set DataGrid Checkbox Default Value RRS feed

  • Question

  • How do I set the default value of a checkbox in a DataGrid? The DataGrid is bound to a datasource so the CheckBox will display the data it reads from the database but for new records I want it to default to false.
    • Moved by CoolDadTx Monday, December 21, 2009 2:26 PM WinForms related (From:Visual C# General)
    Monday, December 21, 2009 1:38 PM

Answers

  • Use defaultValueNeeded event.

        Private Sub DataGridView1_DefaultValuesNeeded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles DataGridView1.DefaultValuesNeeded
            e.Row.Cells("CheckBoxColumnName").Value = False
        End Sub

    Gaurav Khanna
    • Marked as answer by DAMW Tuesday, December 22, 2009 4:37 PM
    Monday, December 21, 2009 2:38 PM

All replies

  • See http://msdn.microsoft.com/en-us/library/a127sbc5.aspx
    With best regards, Yasser Zamani
    Monday, December 21, 2009 2:29 PM
  • Use defaultValueNeeded event.

        Private Sub DataGridView1_DefaultValuesNeeded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles DataGridView1.DefaultValuesNeeded
            e.Row.Cells("CheckBoxColumnName").Value = False
        End Sub

    Gaurav Khanna
    • Marked as answer by DAMW Tuesday, December 22, 2009 4:37 PM
    Monday, December 21, 2009 2:38 PM
  • Hello DAMW,

    Do you use the DataGrid control in .NET 1.1 or the DataGridView control in .NET 2.0? If you use DataGridView, you can take Gaurav Khanna’s suggestion. If you use DataGrid control, it is not easy to set that value in the DataGrid. You’d better set DefaultValue in your datasource. For example, “dt.Columns[0].DefaultValue = true;”, dt is a datatable.

    If you have anything unclear, please feel free to tell us.

    Sincerely,
    Kira Qian
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    Tuesday, December 22, 2009 3:54 AM
  • Hi,

    I have added your suggestion but whenever I call the Update() method of the DataAdapter I get a message telling me that the database field I am trying to update does not accept the value NULL even though I am setting the default value as per your code snippet.

    My code is exactly the same as yours apart from the CheckBox column name (and I'm using C#).

    The column in the DataGridView is a CheckBox and the field in the db is of type BIT. Any suggestions??

    Thanks
    Tuesday, December 22, 2009 11:42 AM
  • Check whether checkbox column is bounded to correct field name and also check your code for update.
    Gaurav Khanna
    Tuesday, December 22, 2009 12:44 PM
  • Looks like it was my fault, I was missing some syntax from my SELECT statement! Thanks for your help, all is working as expected now.
    Tuesday, December 22, 2009 4:36 PM