title: Delete row from datagridview
body: <p>I having problem on deleting a row of data returned by a search query.
  I wish the user can select whichever row of data and click on the delete button [button1_click]  to delete it from DB. This is a windows form application.</p>
  
  <p>Hope you can advise me. Thanks a lot.
  Below is my code</p>
  
  <pre><code>public partial class Search : Form
  {
      public Search()
      {
          InitializeComponent();
          string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Project\DB_Booking.mdb;";
          DataTable ds = new DataTable();
          using (var cn = new OleDbConnection(strConn))
          {
              cn.Open();
              using (var cmd = new OleDbCommand("SELECT * FROM staff", cn))
              {
                  using (OleDbDataAdapter adp = new OleDbDataAdapter(cmd))
                      adp.Fill(ds);
  
                  comboBox1.DataSource = ds;
                  comboBox1.ValueMember = "sname";
                  comboBox1.SelectedIndex = 0;
  
              }
          }
  
      }
  
  
  
      private void btn_search_bystaffname_Click(object sender, EventArgs e)
      {
          string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Project\DB_Booking.mdb;";
  
          DataTable dt = new DataTable();
  
             using (var cn = new OleDbConnection(strConn))
              {
                  cn.Open();
                  using (var cmd = new OleDbCommand("SELECT * FROM booking WHERE sname = @sname", cn))
                  {
                      //cmd.Parameters.AddWithValue("@bdate", dtp_search_date.Value.Date);
                      cmd.Parameters.AddWithValue("@sname", comboBox1.SelectedValue);
  
                      using (OleDbDataAdapter oda = new OleDbDataAdapter(cmd))
                          oda.Fill(dt);
                      GridView1.DataSource = dt;
  
                      GridView1.Columns[0].HeaderCell.Style.Alignment = DataGridViewContentAlignment.TopCenter;
                      GridView1.Columns[1].HeaderCell.Style.Alignment = DataGridViewContentAlignment.TopCenter;
                      GridView1.Columns[2].HeaderCell.Style.Alignment = DataGridViewContentAlignment.TopCenter;
                      GridView1.Columns[3].HeaderCell.Style.Alignment = DataGridViewContentAlignment.TopCenter;
                      GridView1.Columns[4].HeaderCell.Style.Alignment = DataGridViewContentAlignment.TopCenter;
                      GridView1.Columns[5].HeaderCell.Style.Alignment = DataGridViewContentAlignment.TopCenter;
  
                      GridView1.Columns[0].HeaderText = "Booking ID";
                      GridView1.Columns[1].HeaderText = "Client Name";
                      GridView1.Columns[2].HeaderText = "Booking Date";
                      GridView1.Columns[3].HeaderText = "Booking Time";
                      GridView1.Columns[4].HeaderText = "Client Contact";
                      GridView1.Columns[5].HeaderText = "Staff Name";
  
                      this.GridView1.DefaultCellStyle.Font = new Font("Times New Roman", 12);
                      this.GridView1.DefaultCellStyle.ForeColor = Color.Blue;
                      this.GridView1.DefaultCellStyle.BackColor = Color.Beige;
                      this.GridView1.DefaultCellStyle.SelectionForeColor = Color.Yellow;
                      this.GridView1.DefaultCellStyle.SelectionBackColor = Color.Black;
  
                      this.GridView1.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter;
                      this.GridView1.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter;
                      this.GridView1.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter;
                      this.GridView1.Columns[3].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter;
                      this.GridView1.Columns[4].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter;
                      this.GridView1.Columns[5].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter;
  
                  }
  
              }
          }
  
      private void btn_search_bydate_Click(object sender, EventArgs e)
      {
          string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Project\DB_Booking.mdb;";
  
          DataTable dt = new DataTable();
  
          using (var cn = new OleDbConnection(strConn))
          {
              cn.Open();
              using (var cmd = new OleDbCommand("SELECT * FROM booking WHERE bdate = @bdate", cn))
              {
                  cmd.Parameters.AddWithValue("@bdate", dtp_search_date.Value.Date);
                  cmd.Parameters.AddWithValue("@sname", comboBox1.SelectedValue);
  
                  using (OleDbDataAdapter oda = new OleDbDataAdapter(cmd))
                      oda.Fill(dt);
                  GridView1.DataSource = dt;
  
                  GridView1.Columns[0].HeaderCell.Style.Alignment = DataGridViewContentAlignment.TopCenter;
                  GridView1.Columns[1].HeaderCell.Style.Alignment = DataGridViewContentAlignment.TopCenter;
                  GridView1.Columns[2].HeaderCell.Style.Alignment = DataGridViewContentAlignment.TopCenter;
                  GridView1.Columns[3].HeaderCell.Style.Alignment = DataGridViewContentAlignment.TopCenter;
                  GridView1.Columns[4].HeaderCell.Style.Alignment = DataGridViewContentAlignment.TopCenter;
                  GridView1.Columns[5].HeaderCell.Style.Alignment = DataGridViewContentAlignment.TopCenter;
  
                  GridView1.Columns[0].HeaderText = "Booking ID";
                  GridView1.Columns[1].HeaderText = "Client Name";
                  GridView1.Columns[2].HeaderText = "Booking Date";
                  GridView1.Columns[3].HeaderText = "Booking Time";
                  GridView1.Columns[4].HeaderText = "Client Contact";
                  GridView1.Columns[5].HeaderText = "Staff Name";
  
                  this.GridView1.DefaultCellStyle.Font = new Font("Times New Roman", 12);
                  this.GridView1.DefaultCellStyle.ForeColor = Color.Blue;
                  this.GridView1.DefaultCellStyle.BackColor = Color.Beige;
                  this.GridView1.DefaultCellStyle.SelectionForeColor = Color.Yellow;
                  this.GridView1.DefaultCellStyle.SelectionBackColor = Color.Black;
  
                  this.GridView1.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter;
                  this.GridView1.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter;
                  this.GridView1.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter;
                  this.GridView1.Columns[3].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter;
                  this.GridView1.Columns[4].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter;
                  this.GridView1.Columns[5].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter;
  
              }
  
          }
      }
  
      private void button1_Click(object sender, EventArgs e)
      {
          GridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
          DataGridViewRow row = GridView1.SelectedRows[0];
          GridView1.Rows.Remove(row);
  
      }
  
      }
  
  }
  </code></pre>