4
Answers

How to edit dataset bound datagridview?

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)
2
Amit Gupta

Amit Gupta

NA 16.5k 25.7k 7y
Hey
 
I guess that the same has been answered by me in some past months, isn't it?
 
Well, if your gridview is binded by some datatable, you CANNOT use gridview.Rows.Add(..) function.
 
You have to add data to your datatable rather than gridview. 
Accepted
0
Mayank Jani

Mayank Jani

NA 154 3.8k 7y
Hii Amit,
Your reply is enough to solve my problem!!! :)
 
I just try something and got it done. after your reply I consider to do it again with datatable.
 
i was doing...
{
dgvFruits.ColumnCount = 1;
dgvFruits.Columns[0].Width = 300;

DataTable dt = dgvFruits.DataSource as DataTable;
dt.Rows.Add(cmbFruits.SelectedText);
 
then i changed it to... (just experiments)
{
DataTable dt = dgvFruits.DataSource as DataTable;
dt.Rows.Add(cmbFruits.SelectedText);
 
dgvFruits.Columns[0].Width = 300;
 
and now I can add more rows..
 
 
Thank you so much.
 
P.S. Can you please check another problem regarding to CheckedListBox it is still unanswered. May your reply make it solved!!
 
Thanks again...
 
Mayank Jani.
0
Amit Gupta

Amit Gupta

NA 16.5k 25.7k 7y
Please post your relevant code with expected output, so that I can look at your issue
0
Mayank Jani

Mayank Jani

NA 154 3.8k 7y
Hiiii Amit,
 
Nice to see you dear,
yes you are right, I have seen that and other options on net but not get my answer.  Would you please teach me how to do that, if possible? I want to do it with gridview only as my application is designed that way. I hope you understood my query. my problem got arise after retrieve a data (record) from database. when trying to add one or more row to update/rectify the record, i get this error.
 
and, could you please check my another query regarding to CheckedListBox?
 
Thank You. :)
 
Mayank Jani