Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Problems with inserted datagridview checkbox column

$
0
0

I have a few problems with a checkbox column added to a datagridview. The datagridview is bound by a binding source whose datasource is a datatable. I also add in the code a checkbox column:

privatevoid InitializeDataGridView()

        {

            dgvInvoices.ColumnHeadersDefaultCellStyle.Font =newFont("Arial", 10,FontStyle.Bold);

           DataGridViewCheckBoxColumn chkColumn = newDataGridViewCheckBoxColumn();

            chkColumn.FlatStyle =FlatStyle.Standard;

            dgvInvoices.Columns.Insert(0, chkColumn);

            dgvInvoices.DataSource = bs;

            cmbInvoicingCriteria.Text ="(All)";

           DataGridViewBinding.CreateColumn(dgvInvoices, "Date", "Date", DataGridViewContentAlignment.MiddleLeft);

           DataGridViewBinding.CreateColumn(dgvInvoices, "Invoice", "Invoice",DataGridViewContentAlignment.MiddleLeft);

           DataGridViewBinding.CreateColumn(dgvInvoices, "Customer", "Customer");

           DataGridViewBinding.CreateColumn(dgvInvoices, "Name", "Name");

           DataGridViewBinding.CreateColumn(dgvInvoices, "Count", "Count");

           DataGridViewBinding.CreateColumn(dgvInvoices, "Total", "Total");

           DataGridViewBinding.CreateColumn(dgvInvoices, "Driver", "Driver");

           DataGridViewBinding.CreateColumn(dgvInvoices, "Status", "Status");

           DataGridViewBinding.CreateColumn(dgvInvoices, "ARHID", "ARHID");

           DataGridViewBinding.CreateColumn(dgvInvoices, "AR", "AR");

            dgvInvoices.Columns["ARHID"].Visible =false;

            dgvInvoices.Columns["AR"].Visible =false;

           this.dgvInvoices.Columns[0].DefaultCellStyle.Font =newFont("Arial", 18,FontStyle.Bold);

           this.dgvInvoices.Columns["Date"].DefaultCellStyle.Font =newFont("Arial", 10,FontStyle.Bold);

           this.dgvInvoices.Columns[2].DefaultCellStyle.Font =newFont("Arial", 10,FontStyle.Bold);

           this.dgvInvoices.Columns["Customer"].DefaultCellStyle.Font =newFont("Arial", 10,FontStyle.Bold);

           this.dgvInvoices.Columns["Name"].DefaultCellStyle.Font =newFont("Arial", 10,FontStyle.Bold);

           this.dgvInvoices.Columns["Count"].DefaultCellStyle.Font =newFont("Arial", 10,FontStyle.Bold);

           foreach (DataGridViewColumn dcin dgvInvoices.Columns)

            {

                dc.ReadOnly =true;

               if (dc.HeaderText.ToLower() == "count" || dc.HeaderText.ToLower() == "name" || dc.Index == 0)

                {

                   this.dgvInvoices.Columns[dc.Index].AutoSizeMode =DataGridViewAutoSizeColumnMode.AllCellsExceptHeader;

                }

               else

                {

                   this.dgvInvoices.Columns[dc.Index].AutoSizeMode =DataGridViewAutoSizeColumnMode.Fill;

                }

            }

            dgvInvoices.DefaultCellStyle.Alignment =DataGridViewContentAlignment.MiddleCenter;

            dgvInvoices.Columns[0].ReadOnly =false;

            dgvInvoices.Columns[0].Width = 25;

            dgvInvoices.Columns[0].SortMode =DataGridViewColumnSortMode.Automatic;

        }

The problems I have here are

1. The first checkbox doesn't check off (it used to work when the width was 25 but then I added the code ontop to make the autosize mode of this columns to allcellscellsexcept header it stopped working. I need that though because if I don't make the autosizemode = all cells except header then when I make the form narrow the checkbox column disappears.) I was wondering why this is happening, and how to fix it.

2. Also this column is not sortable. I added to my code  dgvInvoices.Columns[0].SortMode = DataGridViewColumnSortMode.Automatic; but it doesn't seem to work.

3. Whenever I sort another column the checked off checkboxes lose their focus. I understand that can be a problem with a bound datagridview and one unbound column, but how can I fix it?


Debra has a question


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>