I'm new to Visual Studio and fixing a small bug in an application.
The combo box which exist is editable with DropDown eventhough it is has databinding. Because If I deleted a value, it would not save the change, I made it into a DropDownList; however, now I do not have the option of using null.
I have searched around and have been reading that I can insert an item before databinding.
I have the bits and peices of the codes from different functions for this combo box. Not sure where to exactly make the change.
if someone could point me in the right direction, that'd be great.
private System.Windows.Forms.ComboBox cmbSecCSR;
//----------------
this.cmbSecCSR = new System.Windows.Forms.ComboBox();
//---------------------
// cmbSecCSR
//
this.cmbSecCSR.AccessibleRole = System.Windows.Forms.AccessibleRole.TitleBar;
this.cmbSecCSR.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
this.cmbSecCSR.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
this.cmbSecCSR.DataSource = this.csrBindingSource2;
this.cmbSecCSR.DisplayMember = "Name";
this.cmbSecCSR.FormattingEnabled = true;
this.cmbSecCSR.Location = new System.Drawing.Point(112, 26);
this.cmbSecCSR.Margin = new System.Windows.Forms.Padding(0);
this.cmbSecCSR.MaxDropDownItems = 10;
this.cmbSecCSR.Name = "cmbSecCSR";
this.cmbSecCSR.Size = new System.Drawing.Size(184, 21);
this.cmbSecCSR.TabIndex = 2;
this.cmbSecCSR.ValueMember = "Username";
this.cmbSecCSR.TextChanged += new System.EventHandler(this.comboBox_TextChanged);
this.cmbSecCSR.Enter += new System.EventHandler(this.cmbBox_Entered);
//
// csrBindingSource2
//
this.csrBindingSource2.DataMember = "CSR";
this.csrBindingSource2.DataSource = this.productionDS;
//-------------------------
//loadUnboundData();
cmbSecCSR.DataBindings.Add("SelectedValue", productionMasterBindingSource, "CSR2", true, DataSourceUpdateMode.OnPropertyChanged);
My Problem is similar to this: http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/fa277629-a65b-4799-9400-364f6f771739/
which is why I decided to change it to DropDownList;however, I don't know how to add the NULL Value in the DropDownList as it is bounded.
Related
I am trying to display my List in a ListView using a GridView. I am using dynamic binding as my list view changes. it's gridview for other purposes.
I have already used similar code twice and had no issues. I have checked every variable using a debugger and executed the code step by step and everything seemed normal.
// the code of the function
public void loadPairsIntoListView(object sender, RunWorkerCompletedEventArgs args)
{
XmlComparator xComp = XmlComparator.getInstance();
List<ComparePair> listOfPairs = xComp.getListOfPairs();
multiCompareListOfCompared.ItemsSource = listOfPairs;
multiCompareModelLabel.Content = listOfPairs[0].model.trueName + " " + listOfPairs[0].model.envName.ToUpper();
GridView myGridView = new GridView();
myGridView.ColumnHeaderToolTip = "Objet recap";
GridViewColumn gvc1 = new GridViewColumn();
gvc1.DisplayMemberBinding = new Binding("compared.trueName");
gvc1.Header = "nom";
gvc1.Width = 100;
myGridView.Columns.Add(gvc1);
GridViewColumn gvc2 = new GridViewColumn();
gvc2.DisplayMemberBinding = new Binding("compared.envName");
gvc2.Header = "environnement";
gvc2.Width = 100;
myGridView.Columns.Add(gvc2);
GridViewColumn gvc3 = new GridViewColumn();
gvc3.DisplayMemberBinding = new Binding("anomalies.Count");
gvc3.Header = "Ecarts";
gvc3.Width = 100;
myGridView.Columns.Add(gvc3);
multiCompareListOfCompared.View = myGridView;
Log.S();
}
// the class i'm trying to bind
public class ComparePair
{
public XmlFile model;
public XmlFile compared;
public List<int> anomalies;
private const int diffOnlyMarge = 10;
/// Methods ...
}
//
I am getting an output where the List is actually binded to the ListView (i can click on and use every row) but the rows are actually empty. I am getting all the three columns with their names but their rows has no "visual content" while still has the object bind to it.
What i am expecting is to see the actual values i have bind to each column.
I hope i have been clear enough. Tell me if you need more precision.
I want to get grid view data in the StudentRegistrationForm. I have tried many times, but I am unable do this, and I don't understand. Can you tell me how I can get GridView values in my StudentRegistrationForm?
If I comment combo boxes code, then other values are put in StudentRegistrationForm, but I want to show all data in StudentRegistrationForm.
St_Registration obj = new St_Registration();
int St_Id = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
obj.txt_name.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
obj.txt_fname.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
obj.txt_sur_name.Text =dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
obj.txt_email.Text = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
obj.dob_date.Text = dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString();
obj.date_of_registration.Text=dataGridView1.Rows[e.RowIndex].Cells[6].Value.ToString();
obj.txt_address.Text = dataGridView1.Rows[e.RowIndex].Cells[7].Value.ToString();
obj.rbtn_male.Checked = true;
obj.rbtn_female.Checked = false;
if (dataGridView1.Rows[e.RowIndex].Cells[8].Value.ToString() == "Female")
{
obj.rbtn_male.Checked = false;
obj.rbtn_female.Checked = true;
}
obj.txt_contact.Text = dataGridView1.Rows[e.RowIndex].Cells[9].Value.ToString();
obj.cmb_country.Text=dataGridView1.Rows[e.RowIndex].Cells[10].Value.ToString();
obj.cmb_province.Text = dataGridView1.Rows[e.RowIndex].Cells[11].Value.ToString();
obj.cmb_city.Text = dataGridView1.Rows[e.RowIndex].Cells[12].Value.ToString();
obj.txt_pin.Text=dataGridView1.Rows[e.RowIndex].Cells[13].Value.ToString();
obj.ShowDialog();
I have this form that lets user choose a (Code - Product) item from a comboxbox. input quantity and price and Add it to a list.
Loading the inventories to the form
private List<Inventory> inventories = new Inventory().read_inventory();
Setting the ComboBox with values
private void set_drop_down_inventory()
{
cb_inventory.DisplayMember = "name";
cb_inventory.DataSource = inventories;
cb_inventory.ResetText();
cb_inventory.SelectedIndex = -1;
}
When a user selects a product, it will create a new instance.
private void cb_inventory_SelectionChangeCommitted(object sender, EventArgs e)
{
var selected_inventory = (cb_inventory.SelectedItem as Inventory);
sales_order_detail = new Sales_Order_Detail(selected_inventory, 0);
tx_description.Text = selected_inventory.description;
tx_price.Text = selected_inventory.get_price_str();
}
Once the user adds the item it triggers this code.
private void btn_add_item_Click(object sender, EventArgs e)
{
// Set the inputted data into the instance before adding to the list
sales_order_detail.description = tx_description.Text.ToString();
sales_order_detail.quantity = tx_quantity.Value;
sales_order_detail.price = Convert.ToDecimal(tx_price.Text);
// Adding the instances to a List
sales_order.sales_order_details.Add(sales_order_detail);
// Sets the Datagrid to provide the data+
initialize_datagrid(sales_order_detail);
}
This is how i initialize the datagrid because i need to manually display the columns -- this is where i am not sure what to do - i believe i do not need to manually add a new row every time a user adds an item because this datagrid is bounded to the List<>, so whatever instance is added to the List<> it will be added to the grid when i trigger the dgv.Refresh()
private void initialize_datagrid(Sales_Order_Detail sales_order_detail)
{
dgv_sales_order_details.Columns.Clear();
dgv_sales_order_details.DataSource = null;
dgv_sales_order_details.Refresh();
dgv_sales_order_details.AutoGenerateColumns = false;
// Set the datasource to the list where the item is added
dgv_sales_order_details.DataSource = sales_order.sales_order_details;
DataGridViewComboBoxColumn product_code_col = new DataGridViewComboBoxColumn();
DataGridViewColumn description_col = new DataGridViewColumn();
DataGridViewColumn quantity_col = new DataGridViewColumn();
DataGridViewColumn price_col = new DataGridViewColumn();
DataGridViewColumn account_col = new DataGridViewColumn();
DataGridViewComboBoxCell product_cell = new DataGridViewComboBoxCell();
DataGridViewTextBoxCell description_cell = new DataGridViewTextBoxCell();
DataGridViewTextBoxCell amount_cell = new DataGridViewTextBoxCell();
product_cell.DisplayMember = "name";
// They have the same Datasource as the combobox above.
product_cell.DataSource = inventories;
product_code_col.CellTemplate = product_cell;
product_code_col.DataPropertyName = nameof(sales_order_detail.inventory.name); //This binds the value to your column
product_code_col.HeaderText = "Code";
product_code_col.Name = "name";
description_col.CellTemplate = description_cell;
description_col.DataPropertyName = nameof(sales_order_detail.description);
description_col.HeaderText = "Description";
description_col.Name = "description";
quantity_col.CellTemplate = amount_cell;
quantity_col.DataPropertyName = nameof(sales_order_detail.quantity);
quantity_col.HeaderText = "Quantity";
quantity_col.Name = "quantity";
quantity_col.DefaultCellStyle.Format = "0.00";
quantity_col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
price_col.CellTemplate = amount_cell;
price_col.DataPropertyName = nameof(sales_order_detail.price);
price_col.HeaderText = "Price";
price_col.Name = "price";
price_col.DefaultCellStyle.Format = "0.00";
price_col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
dgv_sales_order_details.Columns.Add(product_code_col);
dgv_sales_order_details.Columns.Add(description_col);
dgv_sales_order_details.Columns.Add(quantity_col);
dgv_sales_order_details.Columns.Add(price_col);
dgv_sales_order_details.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
}
This is the result when the item is added but as you can see the combobox column has not displayed value, it only shows the value when i click the combobox column. and when i change the value in the combobox above the list, the value in the combobox column also changes. it seems that they are binded.
My Goal is to be able add a row to the datagrid where the comboboxcolumn displays what i selected and to fix to combobox duplicated selection.
Please comment if it needs more clarification so i could correct it. Thanks!
DataGridViewComboBoxColumn c = new DataGridViewComboBoxColumn();
c.Name = "ComboColumn";
c.DataSource = dataTable;
c.ValueMember = "ID";
c.DisplayMember = "Item";
dataGridView1.Columns.Add(c);
To select a particular value you set the Value property of a given cell.
dataGridView1.Rows[rowIndexYouWant].Cells["ComboColumn"].Value = 1;
Note that the type here is important! IF you say you get a System.FormatException. This can be caused by setting the wrong type to the value.
When you set the value to 1 you are assigning an int - if for some reason you have strings in the ID column you will get the System.FormatException exception you are seeing.
If the types differ you need to either update the DataTable definition or set the value to a string:
dataGridView1.Rows[rowIndexYouWant].Cells["ComboColumn"].Value = "1";
for adding rows you might need
dataGridView1.Rows.Add();
int z=0;
for (int a = 0; a < dataGridView1.comlumncount; a++)
{
dataGridView1.Rows[z].Cells[a].Value = "yourvalue";
z++;
}
for your reference check this Link you might get your problem solved
I've managed to solve it, this is my solution. This is best solution i've come up so far. Please comment if you have any correction. so we could improve it. I hope this will help others too.
Created a DataGridView Handler so i could reuse it in the other forms and add more conditions for it be flexible.
namespace Project.Classes
{
public static class DGV_Handler
{
public static DataGridViewComboBoxColumn CreateInventoryComboBox()
{
DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
// This lets the combo box display the data selected
// I set the datasource with new instance because if i use the Datasource used in the combobox in the item selection. the combobox in the grid and that combox will be binded. if i change one combobox the other one follows.
combo.DataSource = new Inventory().read_inventory();
combo.DataPropertyName = "inventory_id";
combo.DisplayMember = "name";
combo.ValueMember = "inventory_id";
combo.Name = "inventory_id";
combo.HeaderText = "Code";
combo.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
return combo;
}
public static DataGridViewComboBoxColumn CreateGLAccountComboBox()
{
DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
combo.DataSource = new Account().read();
combo.DataPropertyName = "gl_account_sales";
combo.DisplayMember = "account_name";
combo.ValueMember = "account_id";
combo.Name = "account_id";
combo.HeaderText = "Account";
combo.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
return combo;
}
public static DataGridViewTextBoxColumn CreateTextBox(string dataproperty, string headertext, string name, bool is_numbers)
{
DataGridViewTextBoxColumn textbox = new DataGridViewTextBoxColumn();
textbox.DataPropertyName = dataproperty;
textbox.HeaderText = headertext;
textbox.Name = name;
if (is_numbers)
{
textbox.DefaultCellStyle.Format = "0.00";
textbox.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
}
return textbox;
}
}
}
When the form is loaded i initialize the datagrid like this.
private void initialize_datagrid()
{
dgv_sales_order_details.Columns.Clear();
dgv_sales_order_details.Refresh();
dgv_sales_order_details.AutoGenerateColumns = false;
dgv_sales_order_details.DataSource = bindingSource1;
dgv_sales_order_details.Columns.Add(DGV_Handler.CreateInventoryComboBox());
dgv_sales_order_details.Columns.Add(DGV_Handler.CreateTextBox("description","Description", "description", false));
dgv_sales_order_details.Columns.Add(DGV_Handler.CreateTextBox("quantity","Quantity","quantity", true));
dgv_sales_order_details.Columns.Add(DGV_Handler.CreateTextBox("price", "Price", "price", true));
dgv_sales_order_details.Columns.Add(DGV_Handler.CreateGLAccountComboBox());
dgv_sales_order_details.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dgv_sales_order_details.RowHeadersVisible = false;
dgv_sales_order_details.EditMode = DataGridViewEditMode.EditOnEnter;
}
Code when adding a new row
private void btn_add_item_Click(object sender, EventArgs e)
{
if(validate_selection())
{
// Set the properties to be included in the DGV Column
var selected_row = (cb_inventory.SelectedValue as Inventory);
var selected_gl_account = (cb_gl_account.SelectedValue as Account);
string description = tx_description.Text;
decimal quantity = tx_quantity.Value;
decimal price = Convert.ToDecimal(tx_price.Text);
int gl_account_id = selected_gl_account.account_id;
// When something new is added to the bindingsource, the DGV will be refresh
bindingSource1.Add(new Sales_Order_Detail(selected_row, description, quantity, price, 0, gl_account_id));
clear_item_selection();
}
}
Result
I'm having problems with BindingContext in a .NET application. I need to populate 6 comboboxes with the same datasource but the first 3 comboboxes must be indepedent from the last 3 comboboxes.
I coded the following:
combo_Bancos_cheque.DataSource = bancos;
combo_Bancos_cheque.DisplayMember = "Nombre";
combo_Bancos_cheque.ValueMember = "IDBanco";
combo_ctas_cheque.DataSource = bancos;
combo_ctas_cheque.DisplayMember = "NoCuenta";
combo_clabe_cheque.DataSource = bancos;
combo_clabe_cheque.DisplayMember = "CLABE";
combo_Bancos_dep.BindingContext = new BindingContext();
combo_Bancos_dep.DataSource = bancos;
combo_Bancos_dep.DisplayMember = "Nombre";
combo_Bancos_dep.ValueMember = "IDBanco";
combo_ctas_dep.DataSource = bancos;
combo_ctas_dep.DisplayMember = "NoCuenta";
combo_clabe_dep.DataSource = bancos;
combo_clabe_dep.DisplayMember = "CLABE";
The first 3 comboboxes work fine, when combo_Bancos_cheque changes combo_ctas_cheque and combo_clabe_cheque also change which is the expected behaviour. Then I create a new BindingContext to unbind 4,5 & 6 combobox which also use the same datasource.
The problem here is: when combo_Bancos_cheque value changes, combo_ctas_dep & combo_clabe_dep also change but I don't want this, I need these comboboxes to change only when combo_Bancos_dep changes.
I'm new to BindingContexts, what am I missing?
This is a quick guess, but don't you need to point combo boxes 5 and 6 to the same binding context as combo 4 (combo_Bancos_dep) ?
What if you create the new binding context into a variable, and then set it to CBs 4,5, and 6 ?
EDIT:
I just checked to confirm, and the above is correct. You were really close, you just needed to set your new BindingContext to combo_ctas_dep and combo_clabe_dep.
Here is what you posted with the change:
combo_Bancos_cheque.DataSource = bancos;
combo_Bancos_cheque.DisplayMember = "Nombre";
combo_Bancos_cheque.ValueMember = "IDBanco";
combo_ctas_cheque.DataSource = bancos;
combo_ctas_cheque.DisplayMember = "NoCuenta";
combo_clabe_cheque.DataSource = bancos;
combo_clabe_cheque.DisplayMember = "CLABE";
BindingContext oBC = new System.Windows.Forms.BindingContext();
combo_Bancos_dep.BindingContext = oBC;
combo_Bancos_dep.DataSource = bancos;
combo_Bancos_dep.DisplayMember = "Nombre";
combo_Bancos_dep.ValueMember = "IDBanco";
combo_ctas_dep.BindingContext = oBC;
combo_ctas_dep.DataSource = bancos;
combo_ctas_dep.DisplayMember = "NoCuenta";
combo_clabe_dep.BindingContext = oBC;
combo_clabe_dep.DataSource = bancos;
combo_clabe_dep.DisplayMember = "CLABE";
From what I can infer, I believe the behavior is expected - you are binding the same source to the comboboxes.
I believe this statement:
combo_Bancos_dep.BindingContext = new BindingContext();
does not do what you wish it to do - that is bind to a new source.
If "bancos" is a DataTable, you can remove the above line and just do a Copy on "bancos" to create a new DataTable and use that to bind your other combos:
DataTable copyOfbancos = bancos.Copy();
combo_Bancos_dep.DataSource = copyOfbancos;
combo_Bancos_dep.DisplayMember = "Nombre";
combo_Bancos_dep.ValueMember = "IDBanco";
I've got a datagrid control bound to BindingList of objects. One of the Properties of the object class is boolean. I have customized that column in the datagridview to be a checkbox type.
Things work correctly when the datagridview loads for the rows that have values brought in from the binding list. However, when the "new entry" line is painted, a System.FormatException is thrown on the checkbox cell.
Exact Error message (relevant portion):
The following exception occurred in the DataGridView:
System.FormatException: Value '' cannot be converted to type 'Boolean'. at System.Windows.Forms.Formatter.FormatObjects....
My searching indicated that this can occur when the true, false, and indeterminate values of the checkbox column are not set.
URL Referencing similar problem to mine:
http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/c29427ff-18be-4fb0-a0a7-d1940e1cd817
However, I have set these values (shown in code below). Beyond this, i can't find any other information relevant to my problem. I'm fairly sure the problem is localized to the use of the checkbox, since, when i change the column type to a simple textbox, i get no exception errors, simply a column of true / false with the "new entry" line showing no value.
DataGridView code:
//
// dataGridView1
//
this.dataGridView1.AllowUserToResizeRows = false;
dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Control;
dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
dataGridViewCellStyle1.NullValue = null;
dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
this.dataGridView1.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1;
this.dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
this.dataGridView1.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.DisplayedCells;
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.columnDescription,
this.columnExpedite,
this.columnId,
this.columnQuantity,
this.columnEntryDate,
this.columnUpdateDate});
this.dataGridView1.Location = new System.Drawing.Point(3, 5);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
this.dataGridView1.Size = new System.Drawing.Size(1015, 241);
this.dataGridView1.TabIndex = 0;
//
// columnDescription
//
this.columnDescription.DataPropertyName = "Description";
this.columnDescription.FillWeight = 200F;
this.columnDescription.HeaderText = "Description";
this.columnDescription.Name = "columnDescription";
//
// columnExpedite
//
this.columnExpedite.DataPropertyName = "Expedite";
this.columnExpedite.FalseValue = "false";
this.columnExpedite.HeaderText = "Expedited";
this.columnExpedite.Name = "columnExpedite";
this.columnExpedite.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.columnExpedite.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
this.columnExpedite.TrueValue = "true";
this.columnExpedite.IndeterminateValue = "false";
//
// columnId
//
this.columnId.DataPropertyName = "Id";
this.columnId.HeaderText = "Id";
this.columnId.Name = "columnId";
this.columnId.Visible = false;
//
// columnQuantity
//
this.columnQuantity.DataPropertyName = "Quantity";
this.columnQuantity.HeaderText = "Quantity";
this.columnQuantity.Name = "columnQuantity";
//
// columnEntryDate
//
this.columnEntryDate.DataPropertyName = "EntryDateTime";
dataGridViewCellStyle2.Format = "g";
dataGridViewCellStyle2.NullValue = null;
this.columnEntryDate.DefaultCellStyle = dataGridViewCellStyle2;
this.columnEntryDate.HeaderText = "Entry Date/Time";
this.columnEntryDate.Name = "columnEntryDate";
this.columnEntryDate.ReadOnly = true;
this.columnEntryDate.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.columnEntryDate.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
//
// columnUpdateDate
//
this.columnUpdateDate.DataPropertyName = "UpdateDateTime";
this.columnUpdateDate.HeaderText = "Last Update Date/Time";
this.columnUpdateDate.Name = "columnUpdateDate";
this.columnUpdateDate.ReadOnly = true;
this.columnUpdateDate.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.columnUpdateDate.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
Edit Added:
A couple additional things i've tried:
Attempted using the DefaultValueNeeded event. default value loads when i "touch" the new entry line, but exception fired prior to that, when "new entry" line is actually painting.
Attempted to use
dataGridView1.Columns["columnExpedite"].DefaultCellStyle.NullValue = "false";
with same results.
How do i resolve this exception?
I may be reading this wrong, but this sounds like an order-of-operations issue. I don't see the code where you add the item to the list so I'm not 100% sure on his, but I would guess that you're adding the new object to the list and then modifying the values
You need to ensure that the boolean value is not null. If you're adding a new item,. you will need to populate the value of the object being added before you add it to he list. One possible way to ensure this is to set a default value of either false or true, or set this in the constructor.
Edit - Added
I didn't test this myself, but I'm hoping it will work for you.
Can you hook into the DefaultValuesNeeded event of the DataGridView? That event should fire before the exception is thrown, which should resolve the issue...
Yes.
Try this code:
private void dataGridViewEpizode_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
{
try
{
e.Row.Cells[22].Value = false;
}
catch (Exception ex)
{
mainForm.staticvar.logger.Write(ex);
}
}