Page 1 of 2 12 LastLast
Results 1 to 40 of 63

Thread: Error deleting datagrid row

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219

    Question Error deleting datagrid row

    I'm getting an error of There is no row at position 6. when I tried to delete row six from the datagrid. In fact, I tried all the rows in the datagrid and still receiving the same error message. Here's what tried before:
    Dim dgRowNumber As Integer = dgComputer.CurrentRowIndex

    Try
    DSetProgram.Program.Rows(dgRowNumber).Delete()
    Catch exError As Exception
    MessageBox.Show(exError.Message)
    End Try
    DAdptProgram.Update(DSetProgram.Tables("Program"))
    DSetProgram.Tables("Program").AcceptChanges()
    But still it gives me the error that there is no row at the position of the variable "dgRowNumber". So I tried this:
    dsUsrProfile.Tables("strTableName").Rows(dgRowNumber).Delete()
    DAprofile.Update(dsUsrProfile.Tables("strTableName"))
    dsUsrProfile.Tables("strTableName").AcceptChanges()
    If (dsUsrProfile.HasChanges) Then
    MsgBox("Changes has been committed")
    End If
    But the same error happened. However, I have a total of 6 rows in the table called "Program". In addition, when the program runs, the variable "dgRowNumber" do have an actual number that corresponds to the datagrid. If this variable would to contain some number that is not relating to the datagrid or out of the range then yes, I understand but it's not. It is within the range of the rows in the datagrid and database table.

    ljCharlie

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    VB Code:
    1. Dim dgRowNumber As Integer = dgComputer.CurrentRowIndex [b]-1[/b]

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219

    Question

    Thanks for suggestion; however, that is not the problem. I tried your suggestion and it's still not working. The error is still the same. The number after the word "position" corresponds to whatever row I select in the datagrid but it just gives me an error. Like I said, it's not like the row number is out of range. It is still within the range.

    ljCharlie

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219
    I figured out a part of the problem is when I test the code on another dataset and dataadapter using the same datagrid; however, I don't know how to solve it or detect it. Here's what I found out.

    In this statement:Dim dgRowNumber As Integer = dgComputer.CurrentRowIndex

    The code suppose to find the row that I select to be deleted and it did show the correct row number I want to delete. In this statement: DSetProgram.Program.Rows(dgRowNumber).Delete() , the dgRowNumber still show the correct row number to be deleted. However, when the code finish executing, the record that was deleted wasn't the one that I specify to be deleted. I tried this a couple of times and it seems to be that the deletion was random or it appears to be random despite what row I specify to be delete!

    So my question is, how/why this happen and how do I fix the problem?

    ljCharlie

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219
    Regarding the Random deletion; well, it's not true. It depends on the arrow of the header in each of the columns in the datagrid pointed at. If the arrow pointed up, then the deletion starts at the bottom. If the arrow pointed down, then the deletion starts at the top regardless of what row I specify to be deleted.

    Any clue why this is happening and how do I fix this?


    Many thanks for the help so far.

    ljCharlie

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Did you set any property to sort data on the Grid ? or do you know if the data on the Grid is sorted same way as in source database ?

    If the above is true , then you need to validate selected row not by index number but using real data .

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219

    Question

    I think you're right. I need to validate my selected row by using real data. The question is I don't know how to do that. Mind showing me how or pointing me to an article that will show me how to do that?

    Thank you,

    ljCharlie

  8. #8
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Sure , I'll give it a shot .

  9. #9
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Sorry for taking long time . It's here . VB.NET 2003 , If you don't have it , get the converter under my signature .
    Attached Files Attached Files

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219
    Thanks for helping me out. Unfornately, I wasn't able to unzip that file. When I tried to unzip the file, the program gives me an error of "Bad header". I don't know what that means. Anywa, will you zip the project into .zip instead of .rar?

    Thank you,

    ljCharlie

  11. #11
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Instead , you could downloaded Winrar and unpack it rather than waiting again . . I like winrar because of that I always keep it installed .

    OK . It's winziped pack .
    Attached Files Attached Files

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219
    Again, thank you very much giving up your time to help me.

    If I would to do it the way your sample showed me then that means I'm not using ADO.NET at all correct? I thought ADO.NET supose to make datagrid editing a lot easier. This is the manual way though. Because I'm using design mode to create my dataset and dataadapter, is it possible for me to just call one of those already generated queries to delete the row I want? I remember that there is a way; however, I don't remember how to call the delete SQL query instead of writing from scratch, since I'm jusing a dataset that was creaed at design time.

    ljCharlie

  13. #13
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    No problem

    ADO.NET is the underlying structure for the databases , tables , columns ..etc . So , it's completely isolated from DataGrid or any other control . So as soon you declare any ADO.NET obj (connection , table , column..etc) , then you're using ADO.NET .

    For me , I hate those database controls . I like to code them .

    For the deleting issue , check the demo I posted above . It should show you how to use SQL Statement "DELETE" to delete a record .

    Sorry if I'm still missing the point you're dragging my attention to .

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219
    I can do a manual delete with a SQL query. I thought it would be much easier just to specify which row to delete in the datagrid and the program will automatically delete it for me. Perhaps the DataGrid is not that useful then.

    Well, thanks for all the help.

    ljCharlie

  15. #15
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I don't know if you are doing something supernatural and this DataGrid is no way good for your current proj . From what I saw , it still works good . Why not just specifying a unique word or number and let it handled by this SQL string . I did it in a way , it builds the string easily avoiding error generations . I'm trying to force you to do that but what's the real problem now ?

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219
    Guess what? I got something weard going on here. Somehow it works if I created a textbox and just enter a row number into this textbox and leave everything the same; however, instead of using the CurrentRowIndex, I'm using the number from the textbox to make my deletion and it seems to work. Now, I have not test this on when I sort the datagrid. But my question is, why is this working?

    ljCharlie

  17. #17
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Dublin (Ireland)
    Posts
    304
    If you are using the current row index then set your data row as follows

    dim currow as integer = MyDatagrid.CurrentRowIndex

    dim mydatarow as datarow

    mydatarow = mydataset.tables.ite("MyDataset").Rows.RemoveAt(currow)

    that should do the trick!

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219
    There is a blue underline beneath the line when I tried this:

    mydatarow = mydataset.tables.ite("MyDataset").Rows.RemoveAt(currow)

    What is ite? I tried without that and it still gives me the blue underline. I tried putting the word "Item" and still giving the same thing. for the "MyDataSet", I put my database Table name and still same blue underline. I tried putting the name of my DataSet in and the same thing happend.

    ljCharlie

  19. #19
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Maybe he means something like this :

    VB Code:
    1. dr = ds.Tables("tablename").Rows.RemoveAt("indexnumber")

  20. #20
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Dublin (Ireland)
    Posts
    304
    yes the ite was a mistype, you will also need to Dim your dataset as in

    Dim MyDataset as new dataset, put it before the dim MyDatarow

  21. #21
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Remove the quotes surrounding the indexnumber .

  22. #22

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219
    So it should something like this:
    Dim MyNewDataSet As New DataSet

    Dim currow as integer = MyDatagrid.CurrentRowIndex

    Dim mydatarow as DataRow

    mydatarow = mydataset.tables(MyNewDataSet ).Rows.RemoveAt(currow)
    ljCharlie

  23. #23
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Dublin (Ireland)
    Posts
    304
    No Pirate I didn't mean




    dr = ds.Tables("tablename").Rows.RemoveAt("indexnumber")

    since the parameter for RemoveAt has to be an integer and should match, the row number you are on the datagrid, usually the one you selected by clicking aand saving the row number for another event to remove

  24. #24
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Without double quotes .
    VB Code:
    1. dr = ds.Tables("tablename").Rows.RemoveAt(indexnumber)
    but are you sure that this record is the same one as in the Grid?

    SQL string I think is the good way to go with . It's your choice ...

  25. #25

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219
    If what I'm looking for works then I want to try that route first. As far as SQL string, I know how to do that...the only problem I have right now is that I'm too far in the project that I don't want to start all over by writing every SQL string in my project just to accomodate the delete routine.

    ljCharlie

  26. #26
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Dublin (Ireland)
    Posts
    304
    Yes thats right but just remember I have left out all the code to populate that datagrid first as written it would be ridiculous as you have nothing to remove.

    Basic order here, create dataset and populate
    Bind datagrid to dataset

    You must already have done this in your application I feel sure

    Now you have a datagrid populated with data and you want to remove one item

    The basic technique I use, is to react to the click event on the datagrid and save the currentrow index in an integer variable

    Then have what is effectively a remove button

    When they click that, first of all I check that the saved current row index is selected and if so use the remove at method to remove it from the displayed items in the datagrid

    In the meantime if you could look at my thread called the The downside of Application.Enablestyles and offer any insights, boy would I be grateful!!!

    Your problem is covered in the VB.Net help although I know that VS 2003 is shippped without help and they tell you install MSDN help, yes all 2gb of it, I rely on the help I got with VS 2002 or MSDN itself and finally here.

  27. #27

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219
    RichardAtherton, thanks for the help. However, I have one more question before I implement the code. Why do I need to declare a New DataSet variable to put into this mydatarow = mydataset.tables(MyNewDataSet ).Rows.RemoveAt(currow). I already created a dataset and dataadapter at design time. So what's the purpose declaring the MyNewDataSet again?

    ljCharlie

  28. #28
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Dublin (Ireland)
    Posts
    304
    maybe that was a crossover from my last post, no of course you don't have to create a new dataset,Ijust wrote some code that would not have any syntax errors.

    I did assume that you already had a populated dataset and simply wanted to manipluate it.

  29. #29

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219

    Question

    I tried and still not working. I mean there is a blue underline when I wrote this line:

    mydatarow = mydataset.tables("myTable" ).Rows.RemoveAt(currow)

    ljCharlie

  30. #30
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Dublin (Ireland)
    Posts
    304
    Ok here is some actual working code! with no blue lines, of course anything not Dim has been dim elsewhere and is known to this routine

    FindResult.item is a shared class, with a variable for storing and retrieivng an integer value representing the current row index

    If FindResult.Item = -1 Then Exit Sub
    Me.Cursor = Cursors.WaitCursor
    dr5 = ds5.Tables.Item("RentAdjustmentsList").Rows(FindResult.Item)
    TotalAdj = TotalAdj - dr5("Amount")
    DecTxtBox3.Text = Format(TotalAdj, "##,##0.00")
    ds5.Tables.Item("RentAdjustmentsList").Rows.RemoveAt(FindResult.Item)
    Me.Cursor = Cursors.Default
    Button5.Visible = False
    NumTxtBox1.Focus()

  31. #31

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219
    dr5 is a DataRow, correct? And FindResult.Item contains the CurrentRowIndex as an Integer, correct?

    ljCharlie

  32. #32
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Dublin (Ireland)
    Posts
    304
    yes dr5 is a datarow and findresult.item hold the currentrowindex

  33. #33

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219
    First of all, any time when I do this:
    dr5 = ds5.Tables.Item("RentAdjustmentsList").Rows(FindResult.Item) , with the datarow variable, I get a blue underline. Secondly, the RemoveAt dosn't work. It's not deleting the row in the datagrid. It only delete when I when use the word "Delete", but again, it's not deleting the selected row.

    ljCharlie

  34. #34
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Dublin (Ireland)
    Posts
    304
    ok I give up don't know what is wrong, all I know is the code works for me

  35. #35

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219
    Many thanks for the effor though. I was going to attach my project to this thread; however, looks like this site only allow 100K and my zip file is over 300K.

    Well, thanks for trying.

    ljCharlie

  36. #36
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Remove the bin and obj folders . Zip up the proj , if still too big , then post it in two separate posts (proj + database)

  37. #37

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219
    Here it is. I break it to two zip files.

    ljCharlie
    Attached Files Attached Files

  38. #38

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219

    Question

    Here is the second file. Put the .mdb file inside the bin and the Icons folder under the Computer.

    ljCharlie
    Attached Files Attached Files

  39. #39
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    OK , I downloaded the proj . Promise to look into it tomorrow .I'm dead tired by now .

  40. #40

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219
    Sorry for taking too much of your time. I'm greatly appreciate the time you spend on helping me out.

    Thanks again.

    ljCharlie

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width