locked
How can I delete a single record in a datagrid? RRS feed

  • Question

  • What I'm trying to do is, select a record in my datagrid and from a value I get issue a sql delete command to remove that single record.  

    I have found a post on the forums that instruct me to use one of these syntax's below to get that value but I don't seem to have the .selectedrows, or .selectedcells properties available for my datagrid control.

    string cellText =dataGrid.SelectedRows(0).Cells(1).Text()

    or

    String cellText = dataGrid.SelectedCells[0].Value().ToString()

     

    Keep in mind, my datagrid is working fine.  I'm able to add records, refresh and delete all at this time.

     


    Friday, May 21, 2010 11:48 AM

All replies

  • You can use DataGrid.IsSelected() or DataGrid.CurrentRowIndex or Datagrid.CurrentCell

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Friday, May 21, 2010 4:46 PM
  • This works just great 

    dim rownum as interger

    rownum = MpgdataDataGrid.CurrentCell

    Now how can I use this either SQL or another method to delete this selected record?

    What i was actually trying to do was return the value of a individual field so i could delete that record with that unique value.  If theres another way to accomplish this Im open for new techniques also.

     

    Thanks

     

     

    • Edited by Rjl66 Saturday, May 22, 2010 12:52 PM make more clear
    Friday, May 21, 2010 9:27 PM
  • That depends on your data source. One important thing to understand with data grid is what it contains no data but just shows whatever in the data source.  Since grid has no data you need to work with data source to add/change/delete data.

    For example if your data is in SQL and you're using DataSet then you need to delete row from the DataSet and the use TableAdapter to submit changes to the SQL. Note it is not related to the DataGrid in any way.


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Sunday, May 23, 2010 5:22 PM
  • That depends on your data source. One important thing to understand with data grid is what it contains no data but just shows whatever in the data source.  Since grid has no data you need to work with data source to add/change/delete data.

    For example if your data is in SQL and you're using DataSet then you need to delete row from the DataSet and the use TableAdapter to submit changes to the SQL. Note it is not related to the DataGrid in any way.


    This posting is provided "AS IS" with no warranties, and confers no rights.

    Ilya,

    Can you please provide me with an example of how to accomplish this?

    Just something to get me started.  I'm sure i can find more online.

    Thanks

     

     

    Monday, May 24, 2010 12:32 PM
  • I can not as I don't what are you using as data source.

    You should be able to find it on your own. While searching make sure to remove "grid" or "data grid" from your search query and include whatever data source you're using. E.g. not "delete record data grid" but "delete record DataTable" if you're using DataSet/DataTable.


    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by Rjl66 Monday, May 24, 2010 4:17 PM
    • Unmarked as answer by Rjl66 Tuesday, May 25, 2010 2:19 AM
    Monday, May 24, 2010 4:01 PM
  • Getting very close but still not quite there.  The code below will delete the record from the datagrid but not from the table.  As soon as I add another record they all show up again.

    Any ideas?

    gridrow = dataDataGrid.CurrentRowIndex
    DataSet1.Tables("data").Rows(gridrow).Delete()
    ataSet1.Tables("data").AcceptChanges()
    Monday, May 24, 2010 4:17 PM