add delete record checkbox to mxdatagrid


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

LVL 3
tnapolitanoAsked:
Who is Participating?
 
osiris247Commented:
You are trying to bind the checkbox to a database field called 'Delete', which i am guessing you dont have in your initial databind.

Are you trying to perform a single delete when a checkbox is checked? If so i recommend using a button, using a checkbox in this context i would expect to be able to select/deselect mulitple rows before commiting the delete.

Steve
0
 
osiris247Commented:
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
0
 
tnapolitanoAuthor Commented:
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
0
 
tnapolitanoAuthor Commented:
I'll give the button a try and get back to you.
0
 
tnapolitanoAuthor Commented:
Yeah, a linkbutton was the way to go. Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.