Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi guys,

 

I have a datagrid with the delete command in the template. What I want is, whenever I click delete, I should be able to delete that particular row. My code is like below.

 

Private Sub DataGrid1_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.DeleteCommand

Dim strConn As String = "Data Source=isdc01;Initial Catalog=Internet_SMS;user id=sa;pwd=;"

Dim DeleteCmd As String = "DELETE from COntact_List Where id = @System_ID"

Dim MyConn As New SqlConnection(strConn)

Dim Cmd As New SqlCommand(DeleteCmd, MyConn)

Cmd.Parameters.Add(New SqlParameter("@System_ID", DataGrid1.DataKeys(CInt(e.Item.ItemIndex))))

MyConn.Open()

Cmd.ExecuteNonQuery()

MyConn.Close()

BindData()

End Sub

 

Thanks in advanced,

Edora Theo

Posted

Sorry, Forgot,

 

When I try to run, I will get this error

 

Cmd.Parameters.Add(New SqlParameter("@System_ID", DataGrid1.DataKeys(CInt(e.Item.ItemIndex))))

 

WIth the error command -

 

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

 

Can anyone help me to find our what is the error about?

Thanks

Posted

Well,

 

I have change my code like below

Private Sub DataGrid1_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.DeleteCommand

Dim System_ID As String = CType(e.Item.FindControl("Delete"), LinkButton).Text

Dim myTable As DataTable = CType(Session("Contact_ListTable"), DataTable)

Dim myRows As DataRowCollection = myTable.Rows

Dim thisRow As DataRow = myRows.Find(System_ID)

If Not (thisRow Is Nothing) Then myRows.Remove(thisRow)

Session("Contact_ListTable") = myTable

DataGrid1.DataSource = Session("Contact_ListTable")

DataGrid1.DataBind()

End Sub

 

but I still get an error which is

 

Line 56: Dim System_ID As String = CType(e.Item.FindControl("Delete"), LinkButton).Text

 

and the error command is

Object reference not set to an instance of an object.

 

Anyone could help me to figure out which is wrong?

 

Thanks in advanced,

Edora Theo

Posted

Hi guys,

 

Since i cannot get the solution after I tried quite a number of sample, I've decided to change my code. Which is, when i mark the checkbox, and click the button delete outside the datagrid, it will delete the selected rows.

 

Private Sub cmdButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdButton.Click

For Each myDataGridItem In DataGrid2.Items

If chkSelected.Checked Then

(I need some code here to delete the selected row)

 

End If

Next

 

End Sub

 

 

Hoping for help...

Thanks

Posted

Hi guys,

 

Finally I got it, so this is the sample working code!

 

Private Sub cmdButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdButton.Click

Dim a As CheckBox

For Each myDataGridItem In DataGrid2.Items

a = myDataGridItem.FindControl("chkSelection")

' Response.Write(CType(DataGrid2.Items(7).FindControl("chkSelection"), CheckBox).Checked())

If a.Checked Then

m_objSMS.delete(myDataGridItem.Cells(2).Text())

End If

 

Next

End Sub

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...