title: How to add Edit, update and Cancel buttons actions for programatically created gridview using VB.net?
body: <p>I'm, trying to add a grid view to my web page programatically (using VB.net in Visual Studio 2012). im successful in creating a gridview and also added Edit, update and Cancel buttons to my grid. but im struck with adding event commands to these buttons. how to make this buttons work? right know button (edit/Update/Delete) action is not doing anything. below is the code... kindly help. Please help me with the code for Edit, Update and Cancel buttons for gridview (<strong>Programatically added Grid view</strong>).</p>
  
  <pre><code>Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click       
     Dim Gv As GridView = New GridView()
     Dim cf As CommandField = New CommandField()
      Dim df As CommandField = New CommandField()
      Dim ef As CommandField = New CommandField()
      cf.ButtonType = ButtonType.Button
      df.ButtonType = ButtonType.Button
      df.ShowCancelButton = True
      cf.ShowEditButton = True
      ef.ShowDeleteButton = True
      Dim dt As DataTable = Me.GetData("SELECT * FROM Table1")
      Gv.Columns.Add(df)  
      Gv.Columns.Add(cf)
      Gv.Columns.Add(ef)
      Gv.DataSource = dt
      Gv.EditIndex = True
      Gv.DataBind()
      Page.Form.Controls.Add(Gv)
  
      Private Function GetData(query As String) As DataTable
      Dim dt As New DataTable()
      Dim constr As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
      Using con As New SqlConnection(constr)
          Using cmd As New SqlCommand(query)
              Using sda As New SqlDataAdapter()
                  cmd.CommandType = CommandType.Text
                  cmd.Connection = con
                  sda.SelectCommand = cmd
                  sda.Fill(dt)
              End Using
          End Using
          Return dt
      End Using
  End Function 
  </code></pre>