obulisundaram G

obulisundaram G

  • NA
  • 3
  • 3.4k

Rows cannot be programmatically added to datagridview's rows

Oct 30 2013 5:11 AM
Hi,

I am using datagridview, In that I need to add some file's  name collection. I stored customer name in mysql. I just used button click event to add customer names. Its works good but now it showing error like "Rows cannot be programmatically added to datagridview's rows collection when the control is databound."
Here is my code:

 private void btnadd_Click(object sender, EventArgs e)
        {
            try
            {                
                if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                {
                    string foldername = folderBrowserDialog1.SelectedPath;
                    foreach (string st in Directory.GetFiles(foldername))
                    {             

                        string filenme = st.Substring(st.LastIndexOf('\\') + 1);
                        pth = st;
                        dataGridView1.Rows.Add(filenme);

                    }
                }
            }
            catch(Exception er)
            {
                MessageBox.Show(er.Message + "\n" + er.StackTrace);
            }
        }


Following code is used to store in mysql:


 private void btnsave_Click(object sender, EventArgs e)
        {
            MySqlConnection con;
            con = new MySqlConnection(strpth);
            try
            {
                con.Open();
                MySqlCommand cmd;
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    cmd = con.CreateCommand();

                    string col1 = dataGridView1.Rows[i].Cells[0].Value.ToString();
                    string str = col1;
                    string col2 = dataGridView1.Rows[i].Cells[1].Value.ToString();
                    string col3 = dataGridView1.Rows[i].Cells[2].Value.ToString();
                    string col4 = dataGridView1.Rows[i].Cells[3].Value.ToString();
                    string col5 = pth;
                    cmd.CommandText = "Insert into searchsong values('" + col1 + "','" + col2 + "','" + col3 + "','" + col4 + "','" + col5 + "')";
                    cmd.Parameters.AddWithValue("@File_Name", col1);
                    cmd.Parameters.AddWithValue("@Actor", col2);
                    cmd.Parameters.AddWithValue("@Music_Director", col3);
                    cmd.Parameters.AddWithValue("@Year", col4);
                    cmd.Parameters.AddWithValue("@Path", col5);
                    cmd.ExecuteNonQuery();
                    txtbxfilename.AutoCompleteMode = AutoCompleteMode.Suggest;
                    txtbxfilename.AutoCompleteSource = AutoCompleteSource.CustomSource;
                    txtbxfilename.AutoCompleteCustomSource = namecollection;


                }
            }
            catch (Exception ed)
            {
                MessageBox.Show(ed.Message + "\n" + ed.StackTrace);
            }
            finally
            {
                 con.Close();
               
            }
        }

Thanks,