Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Checkbox as selected in window applictaion datagridview control
 

Checkbox as selected in window applictaion datagridview control

hi,

Have bind datatable to datagridview control and later have added one more new column as datagridviewcheckbokcolumn. Needed to make sure the checkbox are select as default and later as user changes select/deselect the state should be changed accordingly.

Tried using TrueValue and select without any expected output..:(

Please can anyone advise me how to do this. Its been require bit urgently.
Developer
Sri2080  Wednesday, September 30, 2009 2:55 AM
Do you have a field for that newly created checkbox columns?

If you have , did you assign the field name to the DataPropertyName field of your column?

If you have the following code may be a sample for you;

            DataTable dt = new DataTable();
            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("IsChecked", typeof(bool));
            dt.Rows.Add(1, "a", true);
            dt.Rows.Add(2, "b", true);
            dt.Rows.Add(3, "c", true);

            dgv.AutoGenerateColumns = false;
            DataGridViewCheckBoxColumn cbl = new DataGridViewCheckBoxColumn(false);
            cbl.DataPropertyName = "IsChecked";//the field on your datasource
            cbl.HeaderText = "Is Checked";
            dgv.Columns.Add(cbl);

            dgv.DataSource = dt;
Tamer Oz  Wednesday, September 30, 2009 5:17 AM
Hi,

 

For unbound column, you’d better set the default 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

 

The following example shows binding datagridview to a datatable, add checkboxColumn to the datagridview.

        private void Form1_Load(object sender, EventArgs e)

        {

            DataTable dt01 = new DataTable("dt01");

            dt01.Columns.Add("col01");

            dt01.Columns.Add("col02");

            dt01.Columns.Add("col03");

 

            dt01.Rows.Add("aa", "11", "a1");

            dt01.Rows.Add("aa", "11", "a1");

            dt01.Rows.Add("aa", "11", "a1");

 

            dataGridView1.DataSource = dt01;

 

            DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();

            chk.Name = "Check";

            chk.HeaderText = "Test";

 

            chk.DefaultCellStyle.NullValue = true;

 

            dataGridView1.Columns.Add(chk);

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            dataGridView1.CurrentRow.Cells["Check"].Value = false;

        }

 

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  Monday, October 05, 2009 8:44 AM
hi,

Have bind datatable to datagridview control and later have added one more new column as datagridviewcheckbokcolumn. Needed to make sure the checkbox are select as default and later as user changes select/deselect the state should be changed accordingly.

Tried using TrueValue and select without any expected output..:(

Please can anyone advise me how to do this. Its been require bit urgently.
Developer
Sri2080  Wednesday, September 30, 2009 2:51 AM
Do you have a field for that newly created checkbox columns?

If you have , did you assign the field name to the DataPropertyName field of your column?

If you have the following code may be a sample for you;

            DataTable dt = new DataTable();
            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("IsChecked", typeof(bool));
            dt.Rows.Add(1, "a", true);
            dt.Rows.Add(2, "b", true);
            dt.Rows.Add(3, "c", true);

            dgv.AutoGenerateColumns = false;
            DataGridViewCheckBoxColumn cbl = new DataGridViewCheckBoxColumn(false);
            cbl.DataPropertyName = "IsChecked";//the field on your datasource
            cbl.HeaderText = "Is Checked";
            dgv.Columns.Add(cbl);

            dgv.DataSource = dt;
Tamer Oz  Wednesday, September 30, 2009 5:17 AM
Hi,

 

For unbound column, you’d better set the default 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

 

The following example shows binding datagridview to a datatable, add checkboxColumn to the datagridview.

        private void Form1_Load(object sender, EventArgs e)

        {

            DataTable dt01 = new DataTable("dt01");

            dt01.Columns.Add("col01");

            dt01.Columns.Add("col02");

            dt01.Columns.Add("col03");

 

            dt01.Rows.Add("aa", "11", "a1");

            dt01.Rows.Add("aa", "11", "a1");

            dt01.Rows.Add("aa", "11", "a1");

 

            dataGridView1.DataSource = dt01;

 

            DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();

            chk.Name = "Check";

            chk.HeaderText = "Test";

 

            chk.DefaultCellStyle.NullValue = true;

 

            dataGridView1.Columns.Add(chk);

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            dataGridView1.CurrentRow.Cells["Check"].Value = false;

        }

 

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  Monday, October 05, 2009 8:44 AM

You can use google to search for other answers

Custom Search

More Threads

• Problem connecting to existing typed datatable
• Validating DataGridViewCell
• Databindings Advanced Properties Design Window bug.
• Geting a reference to new record
• Create a datagridview with treeview features using VB.net
• Problem in Populating datagridview with designed columns
• DataGridView Column as Combobox bound to ArrayList
• Grab current record when not using databinding
• VS Exception, DataSource, VS Crash
• CREATING AND USING DATAVIEW OBJECTS