Link to home
Start Free TrialLog in
Avatar of tnapolitano
tnapolitano

asked on

add delete record checkbox to mxdatagrid


I'm stumped on how to add a "delete record" checkbox to a mxdatagrid. Any help is appreciated.

Avatar of osiris247
osiris247
Flag of United States of America image

You can add a checkbox column not bound to any fields, a delete button then loop through the gridviewrows and delete the records where the checkbox is checked.

something like....
foreach (GridViewRow row in gridView1.Rows) {
  CheckBox chk = (CheckBox)row.FindControl("chkBox1");
  if (chk.Checked) {
    //delete the record
  }
}

hope this helps.
Steve
Avatar of tnapolitano
tnapolitano

ASKER

So here's what I did:

                <Fields>
                    <wmx:TemplateField HeaderText="Delete">
                        <ItemTemplate>
                            <asp:checkbox id="active" runat="server" checked='<%# DataBinder.Eval(Container, "DataItem.Delete") %>' />
                        </ItemTemplate>
                    </wmx:TemplateField>
                    <wmx:BoundField DataField="Role" HeaderText="Role"></wmx:BoundField>
                    <wmx:BoundField DataField="Level" HeaderText="Level"></wmx:BoundField>
                    <wmx:BoundField DataField="Category" HeaderText="Category"></wmx:BoundField>
                    <wmx:BoundField DataField="Material" HeaderText="Material"></wmx:BoundField>
                    <wmx:BoundField DataField="Location" HeaderText="Location"></wmx:BoundField>
                    <wmx:BoundField DataField="Section" HeaderText="Section"></wmx:BoundField>
                </Fields>
            </wmx:MxDataGrid>

Which gave me a checkbox column in my mxdatagrid. However when I try to compile/launch the page, I get:

'System.Data.DataRowView' does not contain a property with the name Delete."

How do I link the Checkbox to my function "delRecord"

 Function delRecord(ByVal uID As Integer) As Integer
        Dim connectionString As String = "server='(local)'; trusted_connection=true; database='Deputization'"
        Dim dbConnection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(connectionString)

        Dim queryString As String = "DELETE FROM [dp_material] WHERE ([dp_material].[uID] = @uID)"
        Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand
        dbCommand.CommandText = queryString
        dbCommand.Connection = dbConnection

        Dim dbParam_uID As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
        dbParam_uID.ParameterName = "@uID"
        dbParam_uID.Value = uID
        dbParam_uID.DbType = System.Data.DbType.Int32
        dbCommand.Parameters.Add(dbParam_uID)

        Dim rowsAffected As Integer = 0
        dbConnection.Open
        Try
            rowsAffected = dbCommand.ExecuteNonQuery
        Finally
            dbConnection.Close
        End Try

        Return rowsAffected
    End Function
ASKER CERTIFIED SOLUTION
Avatar of osiris247
osiris247
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'll give the button a try and get back to you.
Yeah, a linkbutton was the way to go. Thanks.