Problems with inserted datagridview checkbox column

  • Thread starter Thread starter Debra has a question
  • Start date Start date
D

Debra has a question

Guest
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:

private void InitializeDataGridView()

{

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

DataGridViewCheckBoxColumn chkColumn = new DataGridViewCheckBoxColumn();

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 = new Font("Arial", 18, FontStyle.Bold);

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

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

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

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

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

foreach (DataGridViewColumn dc in 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 doesnt 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 dont 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 doesnt 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

Continue reading...
 
Back
Top