There is a DataGridView (c # winforms vs2010 net4.0) to which a line is added (one by one):

private void btn_AddRow_Click(object sender, EventArgs e) { int index = dGV.Rows.Count; index++; dGV.Rows.Add(); int nRowIndex = dGV.Rows.Count - 1; string[] ListGroups = listBox1.Items.OfType<string>().ToArray(); DataGridViewComboBoxCell Col1 = (DataGridViewComboBoxCell)dGV.Rows[nRowIndex].Cells[0]; Col1.DataSource = ListGroups; } 

The ListGroups list is bound to column1 (Col1), which is formed on the basis of the listBox1 list. In column1, a value is selected. Next, I change (code btn_Groups_Click) the contents of the listBox1 list (for example, delete the value of the string that is selected in column1). As a result, an error occurs.

  private void btn_Groups_Click(object sender, EventArgs e) { Form4 frm4 = new Form4(); if (frm4.ShowDialog(this) == DialogResult.OK) { string[] ListGroups = frm4.ListBox2.Items.OfType<string>().ToArray(); listBox1.Items.Clear(); listBox1.Items.AddRange(ListGroups); DataGridViewComboBoxColumn column1 = (DataGridViewComboBoxColumn)dGV.Columns[0]; column1.DataSource = null; column1.DataSource = ListGroups; } frm4.Close(); frm4.Dispose(); } 

Can you please tell me how, in this case, delete (possibly with a user notification that such a value is already missing) from column 1 there is already a missing value and rebind to the modified listBox1?

  • What kind of error is happening? I tried to reproduce your code - it works strangely, but there is no error. - Alexander Petrov

1 answer 1

This is not the answer, because the question is not enough data for it. I hope we will gradually come to him.

For now, I’ll run a small code review.

In the first method, you have an unused index variable. It is created, incremented, but not used further. Can I throw it out?

The DataGridView.Rows.Add() method returns the index of the added row. It is logical to use it:

 int index = dGV.Rows.Add() - 1; 

Naming He is terrible. See Naming Guidelines

ListGroups - variables are referred to as camelCase: listGroups .
Col1 is the same DataGridViewComboBoxCell - a cell, not a column. I would call a cell .
listBox1 - what does this number 1 mean? Nothing. Give a talking title, something like listBoxGroups or whatever you have in this list box.

btn_AddRow - normal name: "Add a row", you can leave.
btn_Groups - it's not entirely clear what exactly the button does: shows (show), edits (edit), deletes (delete / remove)? The name should reflect this.

Form4 , frm4 - likewise, what does 4 mean? Judging by the code, there is work with some groups. FormGroups or GroupEditingForm (group editing form), not?

column1 - I would call it either just a column , or add an essence: columnGroup , etc.


In some places you use data binding: DataSource = ... , in other places you work directly with the control: Items.OfType , Items.AddRange .
It would be better to start a collection in which data is stored. And to use this collection both for data binding to GUI controls, and for transfer to other methods / forms.
Deleting / adding elements is also done in this collection - thanks to the data binding, changes will be automatically displayed in the GUI.

  • Thank you very much for your comments. Please tell me, now I add data (from the mdb file) to the DataGridView cells using the DataSource, how to use collections and bind them to DataGridView cells? You can give links where to read or examples. - olga