title: how to insert multiple data in grid view
body: <p>i have visual studio 2008 and sql mgmt studio 2008...i have created a winform and I have a combobox which shows the names of all tables created in my sql db, a dgv and a button...
  now i want to insert data in multiple new rows of dgv and save it automatically in my sql db...here is the snipet from my code-</p>
  
  <p><strong>Code</strong>:</p>
  
  <pre><code>    private void InsertInfo()
      {
  
          string table = comboBox1.Text;
          string connectionString = null;
          SqlConnection connection;
          SqlDataAdapter adapter = new SqlDataAdapter();
  
          connectionString = @"Data Source=HP\SQLEXPRESS;database=MK;Integrated Security=true";
  
          connection = new SqlConnection(connectionString);
  
          //if (comboBox1.Text == "lol")
          //{
          //    string insrtQry = "INSERT INTO lol (name, marks) VALUES (@value1, @Value2)";
          //}
          //else
          //{
          //    string insrtQry = "INSERT INTO datejoin (first name,last name,date joined) VALUES (@value1, @value2,@value3)";
  
  
          //}
          foreach (int rowIndex in lstNewRows)
          {
  
  
              string insrtQry = "insert into " + comboBox1.Text + " values(";
  
              foreach (DataGridViewCell cell in dataGridView1.Rows[rowIndex].Cells)
              {
                  insrtQry += "'" + cell.Value.ToString() + "',";
              }
  
              insrtQry = insrtQry.TrimEnd(",".ToCharArray());
  
              insrtQry += ")";
  
  
              try
              {
                  connection.Open();
                  adapter.InsertCommand = new SqlCommand(insrtQry, connection);
                  adapter.InsertCommand.ExecuteNonQuery();
  
  
  
                  MessageBox.Show("Row inserted !! ");
                  connection.Close();
              }
  
              catch (Exception ex)
              {
                  MessageBox.Show(ex.ToString());
              }
          }
      }
  
          private void insert_Click(object sender, EventArgs e)
          {
              InsertInfo();
          }
   private void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
          {
              lstNewRows.Add(e.Row.Index);
          }
  </code></pre>