Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridViewCheckBoxColumn and CellTemplate.Value
 

DataGridViewCheckBoxColumn and CellTemplate.Value

Hello:

I've a question regarding the DataGridView and an unbound column.

I have a DataGridView that has two columns. One of my columns is NOT databound, so I am adding the column manually as such...

this.dg.AutoGenerateColumns = false;

DataGridViewCheckBoxColumn chkcol = new DataGridViewCheckBoxColumn();
chkcol.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeader;
chkcol.ValueType = typeof(bool);
chkcol.FalseValue = false;
chkcol.TrueValue = true;

// PROBLEM HERE

chkcol.CellTemplate.Value = true;

// END PROBLEM

this.dg.Columns.Add(chkcol);

Well, I can see the column; however, when adding a new row to the DataGridView, the .Valueof the new cell is always null; as such, my new row does not have a check box in the newly added row. I know I could add a handler on the "RowsAdded" event and set the .Value there, but I thought the cell template would handle this by setting the .Value of the new cell to true. It does not. Can anyone tell me why the new value of the cell is always null although I have defined the CellTemplate.Value to be true? Thank you.

Trecius

Trecius  Monday, September 28, 2009 8:22 PM

Hi,

 

Changing the properties of the cell template will not immediately affect the user interface (UI) of the column's existing cells. These changes are only apparent after the column is regenerated (for example, by sorting the column or through a call to the DataGridView.InvalidateColumn method). (From DataGridViewCheckBoxColumn.CellTemplate Property msdn document)

 

For unbound column, you’d better set the value in DataGridView_DefaultValueNeed event.

        void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)

        {

            e.Row.Cells["checkColumn"].Value = true;

        }

 

Or set the DefaultCellStyle.NullValue.

chkcolumn.DefaultCellStyle.NullValue = true;

Then you can get the true value by

dataGridView1.CurrentRow.Cells["checkColumn"].FormattedValue property.

 

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Sunday, October 04, 2009 12:53 PM

Hi,

 

Changing the properties of the cell template will not immediately affect the user interface (UI) of the column's existing cells. These changes are only apparent after the column is regenerated (for example, by sorting the column or through a call to the DataGridView.InvalidateColumn method). (From DataGridViewCheckBoxColumn.CellTemplate Property msdn document)

 

For unbound column, you’d better set the value in DataGridView_DefaultValueNeed event.

        void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)

        {

            e.Row.Cells["checkColumn"].Value = true;

        }

 

Or set the DefaultCellStyle.NullValue.

chkcolumn.DefaultCellStyle.NullValue = true;

Then you can get the true value by

dataGridView1.CurrentRow.Cells["checkColumn"].FormattedValue property.

 

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Sunday, October 04, 2009 12:53 PM

You can use google to search for other answers

Custom Search

More Threads

• Windows Forms Data Controls and Databinding FAQ
• Leaving extra margin blank space below the last row in the grid
• Can DataGridViewComboBoxCell be able to edit by user (like a normal Combobox)?
• Undo Changes in Property Grid
• combobox with the option of setting null
• alert a user "To Save changes or ignore changes" when editing records in VB 2005
• Master Detail grid with typed dataset.
• [SOLVED]dataGridView - RowHeader slection problem
• connectivity of database with outlook express..
• Cell Copy/Row Copy