How to update table from Gridview

0

I have the following gridView with a column of type CheckBox, what I'm looking for is to load in the GridView the data of a table, once the data is loaded, select with the CheckBox the fields that require changing the status and once the fields make the UPDATE in the table from a button.

   <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
        <Columns>
              <asp:TemplateField>
            <HeaderTemplate>
                <asp:CheckBox ID="chkHeader" runat="server" />
            </HeaderTemplate>
            <ItemTemplate>
                <asp:CheckBox ID="chkRow" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
             <asp:BoundField DataField="id" HeaderText="id"  />
            <asp:BoundField DataField="matricula" HeaderText="matricula"  />              
        </Columns>
    </asp:GridView>

I'm trying to do it in the following way but I do not get it

  Protected Sub Button1_Click(sender As Object, e As EventArgs)
    For Each row As GridViewRow In GridView1.Rows
        If TryCast(row.FindControl("chkRow"), CheckBox).Checked Then
            Dim constr As String = ConfigurationManager.ConnectionStrings("EJEMPLOConnectionString").ConnectionString
            Using con As New SqlConnection(constr)
               Using cmd As New SqlCommand("UPDATE matricula SET estado = activo where id=@id")
    cmd.Parameters.AddWithValue("@id",Convert.ToInt32(row.Cells[2].Text))
                con.Open()
                cmd.ExecuteNonQuery()

            End Using
            End Using
        End If
    Next
End Sub
    
asked by yulfredy rodriguez 26.07.2017 в 01:20
source

0 answers