4
Answers

How to edit dataset bound datagridview?

Photo of Mayank Jani

Mayank Jani

7y
270
1
Dear Members,
 
Greetings of the day...
 
you might found the subject familier and thought that the question has been answered on this site and on the net many times. but please read this at least once because, before posting my problem on this site, I have searched more than enough on net and on this site too. but not get any satisfactory answer.
 
so, I get an error message while try to add a row in a datagridview that, 'Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.'
 
first please note that this is a Windows Application and I use MS Access as database.
 
after fetching the gridview data from database, I want to add some options to the datagridview from a combobox... at the time when I select the option from combobox, I get this error.
 
this is my code...
 
private void cmbFruits_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (btnSave.Text == "&Save")
{
dgvFruits.ColumnCount = 1;
dgvFruits.Columns[0].Width = 300;

dgvFruits.Rows.Add(cmbFruits.Text);

dgvFruits.Rows[0].Cells[0].Selected = false;
}
else
{

//THE ERROR OCCURS HERE.
dgvFruits.Rows.Add(cmbFruits.Text);

}
 
You are requested to advice for C# WinApp only and for MS Access.
 
Thank You
 
Mayank Jani

Answers (4)

0
Photo of Vishal Gilbile
NA 13.9k 2.9m 12y
Hello Friend,
                  Following link may help you solve your problem.

http://www.dotnetperls.com/ienumerable

With Regards,
Vishal Gilbile
0
Photo of Vulpes
NA 98.3k 1.5m 12y

IEnumerable or its generic equivalent IEnumerable<T>, is just an interface which all enumerable collections (arrays, lists etc) should implement.

Although you can't create instances of these interfaces, you can have variables of these interface types which can accept references to any object which implements them.

These interfaces are particularly important because they enable you to use the 'foreach' statement to iterate through an enumerable collection and to use the LINQ extension methods on them as well.

So, in your example, 'sorted' contains a reference to an object which implements IEnumerable<int>. In other words, it's a sequence of integers.

This can therefore be passed to your Display method which takes a parameter of that type and the sequence can then be iterated through using foreach.