title: asp.net select row in grid set
body: <p>I have a gridview that I want to read, delete, edit and update, I have all the methods set up correctly. I need to know how to enable the grid view so I can select a row to then invoke the delete. Here is my code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Data;
  
  namespace ConsumeDSetService
  {
  public partial class _Default : System.Web.UI.Page
  {
      public static DataSet ds;
  
      protected void Page_Load(object sender, EventArgs e)
      {
  
      }
  
      protected void Button1_Click(object sender, EventArgs e)
      {
          localhost.Service1 myws = new localhost.Service1();
          ds = myws.GetDataSet();
          GridView1.DataSource = ds;
          GridView1.DataBind();
      }
  
      protected void Button2_Click(object sender, EventArgs e)
      {
          //updates database in web service with contents of dataset
          localhost.Service1 myws = new localhost.Service1();
          Label7.Text = myws.ModifyDatabase(ds);
      }
  
      protected void Button3_Click(object sender, EventArgs e)
      {
          //reads student record after row in grid view has been selected
          int i = GridView1.SelectedIndex;
          TextBox1.Text = System.Convert.ToString(ds.Tables["Students"].Rows[i]["RollNumber"]);
          TextBox2.Text = System.Convert.ToString(ds.Tables["Students"].Rows[i]["FirstName"]);
          TextBox3.Text = System.Convert.ToString(ds.Tables["Students"].Rows[i]["Surname"]);
          TextBox4.Text = System.Convert.ToString(ds.Tables["Students"].Rows[i]["Course"]);
          TextBox5.Text = System.Convert.ToString(ds.Tables["Students"].Rows[i]["Level"]);
          TextBox6.Text = System.Convert.ToString(ds.Tables["Students"].Rows[i]["Address"]);
      }
  
      protected void Button4_Click(object sender, EventArgs e)
      {
          //inserts new row into database
          DataRow dr = ds.Tables["Students"].NewRow();
          dr["RollNumber"] = TextBox1.Text;
          dr["FirstName"] = TextBox2.Text;
          dr["Surname"] = TextBox3.Text;
          dr["Course"] = TextBox4.Text;
          dr["Level"] = TextBox5.Text;
          dr["Address"] = TextBox6.Text;
          ds.Tables["Students"].Rows.Add(dr);
          GridView1.DataSource = ds;
          GridView1.DataBind();
      }
  
      protected void Button5_Click(object sender, EventArgs e)
      {
          //edits data row in set
          int i = GridView1.SelectedIndex;
          ds.Tables["Students"].Rows[i]["RollNumber"] = TextBox1.Text;
          ds.Tables["Students"].Rows[i]["FirstName"] = TextBox2.Text;
          ds.Tables["Students"].Rows[i]["Surname"] = TextBox3.Text;
          ds.Tables["Students"].Rows[i]["Course"] = TextBox4.Text;
          ds.Tables["Students"].Rows[i]["Level"] = TextBox5.Text;
          ds.Tables["Students"].Rows[i]["Address"] = TextBox6.Text;
          GridView1.DataSource = ds;
          GridView1.DataBind();
      }
  
      protected void Button6_Click(object sender, EventArgs e)
      {
          //deletes row in set
          int i = GridView1.SelectedIndex;
          ds.Tables["Students"].Rows[i].Delete();
          GridView1.DataSource = ds;
          GridView1.DataBind();
      }
  }
  }
  </code></pre>