Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. Datagrid problems - deleting rows

Datagrid problems - deleting rows

Scheduled Pinned Locked Moved Visual Basic
helpcomquestionlearning
4 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    oakleaf
    wrote on last edited by
    #1

    I have been having this issue for some time now. Some may remember seeing my posts over the course of the last few months. Issue: Everything seems to work with the exception of deleting rows from the datagrid. Err - You can delete rows if you start from the topmost record and work your way down. ?? I have posted the full code for one of the offending forms here Ideas/advice needed and appreciated. If more information is needed, please let me know.

    L 1 Reply Last reply
    0
    • O oakleaf

      I have been having this issue for some time now. Some may remember seeing my posts over the course of the last few months. Issue: Everything seems to work with the exception of deleting rows from the datagrid. Err - You can delete rows if you start from the topmost record and work your way down. ?? I have posted the full code for one of the offending forms here Ideas/advice needed and appreciated. If more information is needed, please let me know.

      L Offline
      L Offline
      lavanya_satheesh
      wrote on last edited by
      #2

      u can delete rows from datagrid try this: If dataset.Tables("SearchResult").Rows.Count > 0 Then dataset.Tables("SearchResult").Rows.RemoveAt(dgrid.CurrentRowIndex) dgrid.Refresh() Else MsgBox("No Data To Delete") End If when u populate the grid with data declare the dataset with general scope. this worked for me. i was also going mad for months. good luck.:)

      O 1 Reply Last reply
      0
      • L lavanya_satheesh

        u can delete rows from datagrid try this: If dataset.Tables("SearchResult").Rows.Count > 0 Then dataset.Tables("SearchResult").Rows.RemoveAt(dgrid.CurrentRowIndex) dgrid.Refresh() Else MsgBox("No Data To Delete") End If when u populate the grid with data declare the dataset with general scope. this worked for me. i was also going mad for months. good luck.:)

        O Offline
        O Offline
        oakleaf
        wrote on last edited by
        #3

        I think that the datagrid in question is a bit too complex for this to work. I tried what you had posted in the DataGrid1_KeyDown event... If e.KeyCode = Keys.Delete Then..... but I still immediately receive: ************** Exception Text ************** System.NullReferenceException: Object reference not set to an instance of an object. at System.Windows.Forms.DataGrid.ResetSelection() at System.Windows.Forms.DataGrid.ResetUIState() at System.Windows.Forms.DataGrid.SetDataGridRows(DataGridRow[] newRows, Int32 newRowsLength) at System.Windows.Forms.DataGrid.DeleteDataGridRows(Int32 deletedRows) at System.Windows.Forms.DataGrid.DeleteRows(DataGridRow[] localGridRows) at System.Windows.Forms.DataGrid.ProcessGridKey(KeyEventArgs ke) at System.Windows.Forms.DataGrid.ProcessDialogKey(Keys keyData) at System.Windows.Forms.Control.ProcessDialogKey(Keys keyData) at System.Windows.Forms.TextBoxBase.ProcessDialogKey(Keys keyData) at System.Windows.Forms.Control.PreProcessMessage(Message& msg) at System.Windows.Forms.ThreadContext.System.Windows.Forms.UnsafeNativeMethods+IMsoComponent.FPreTranslateMessage(MSG& msg)

        L 1 Reply Last reply
        0
        • O oakleaf

          I think that the datagrid in question is a bit too complex for this to work. I tried what you had posted in the DataGrid1_KeyDown event... If e.KeyCode = Keys.Delete Then..... but I still immediately receive: ************** Exception Text ************** System.NullReferenceException: Object reference not set to an instance of an object. at System.Windows.Forms.DataGrid.ResetSelection() at System.Windows.Forms.DataGrid.ResetUIState() at System.Windows.Forms.DataGrid.SetDataGridRows(DataGridRow[] newRows, Int32 newRowsLength) at System.Windows.Forms.DataGrid.DeleteDataGridRows(Int32 deletedRows) at System.Windows.Forms.DataGrid.DeleteRows(DataGridRow[] localGridRows) at System.Windows.Forms.DataGrid.ProcessGridKey(KeyEventArgs ke) at System.Windows.Forms.DataGrid.ProcessDialogKey(Keys keyData) at System.Windows.Forms.Control.ProcessDialogKey(Keys keyData) at System.Windows.Forms.TextBoxBase.ProcessDialogKey(Keys keyData) at System.Windows.Forms.Control.PreProcessMessage(Message& msg) at System.Windows.Forms.ThreadContext.System.Windows.Forms.UnsafeNativeMethods+IMsoComponent.FPreTranslateMessage(MSG& msg)

          L Offline
          L Offline
          lavanya_satheesh
          wrote on last edited by
          #4

          I am posting the full code that worked for me. it is written for a button_click event. if u want it for key_press please wait for a few days. i will ask around and tell u. the code that worked for me is: Imports System.Data Imports System.Data.SqlClient Imports System.Data.SqlDbType Public Class Form5 Inherits System.Windows.Forms.Form . . . . Dim dsdata2 As New DataSet Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim sqlcon2 As New SqlConnection(constr) Dim sqladp2 As New SqlDataAdapter Dim cmdstr1 As New SqlCommand Dim strq1 As String sqlcon2.Open() strq1 = "select * from Doctor" Try sqladp2.SelectCommand = cmdstr1 sqladp2.SelectCommand.CommandText = strq1 sqladp2.SelectCommand.Connection = sqlcon2 dsdata2.Clear() sqladp2.Fill(dsdata2, "SearchResult") If dsdata2.Tables("SearchResult").Rows.Count > 0 Then DataGrid1.DataSource = dsdata2 DataGrid1.DataMember = "SearchResult" DataGrid1.Refresh() End If cmdstr1.Dispose() sqlcon2.Close() sqlcon2.Dispose() sqladp2.Dispose() Catch empexp As SqlException MessageBox.Show(empexp.Message) End Try End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If dsdata2.Tables("SearchResult").Rows.Count > 0 Then dsdata2.Tables("SearchResult").Rows.RemoveAt(DataGrid1.CurrentRowIndex) DataGrid1.Refresh() Else MsgBox("No Data To Delete") End If End Sub End Class Hope this will help u.:)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups