Results 1 to 4 of 4

Thread: DataGridView UncheckAdd conflicts with delete row code

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Resolved DataGridView UncheckAdd conflicts with delete row code

    Hello! I hate having to bother you all with questions, I usually ask when I do not know how to word it in Google Search Engine so yeah... Anyways, I had this issue that when you click the starting row that automatically fills with datagridview, it crashes the program due to my code. Anyways, I was wanting that to go away anyways so I went into the Datagridview properties and unchecked Enable Adding. Now, I suspect it is messing up my index variable, which index is integer Because when I click the whole row that I manually populated with enable adding unchecked and click delete, it crashes the program with error: 'Index was out of range. Must be non-negative and less than the size of the collection.'
    Which shouldn't be happening right? Here is my code for it all:

    Code:
    Dim index As Integer
    Delete Row Button Code:
    Code:
            DataGridView1.Rows.Remove(DataGridView1.SelectedRows(index))
    I do not understand why this is happening, it works when the Enable Adding is checked. Is it because the program visuals shows populating but in the data, the row isn't populating but only does visually? That is what I suspect is happening.
    Last edited by Dragnorian; May 31st, 2017 at 04:33 PM.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Re: DataGridView UncheckAdd conflicts with delete row code

    UPDATE: Worked but then added dialog result msgbox confirmation menu. Back to same old issue of 'Index was out of range. Must be non-negative and less than the size of the collection.'
    Last edited by Dragnorian; May 31st, 2017 at 04:20 PM.

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: DataGridView UncheckAdd conflicts with delete row code

    If that is all the related code, there is no need to use the variable index, simply using 0 would do the same thing.

    As to why it failed (and then worked), that is probably due to the fact you are using .SelectedRows , and on the failed attempt the first of the row(s) you had selected at the time was presumably the "new" row (before it was technically added), and that can't be removed/selected.

    The actual error message means that the value you were passing to .SelectedRows() wasn't valid, and assuming it was 0, that means there were no rows selected.
    Last edited by si_the_geek; May 31st, 2017 at 04:27 PM.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Re: DataGridView UncheckAdd conflicts with delete row code

    Hmmmm, alright. That works too. I just realized that the issue could have been that I have a line of code in the cell click event that says that if user clicks on any rows then it selects that entire row. When you delete a row, it autoselects a new cell but not the entire row so that may have where the whole mess started but never realized. Now I need to go back and try to make it where it autoselects an entire new row. Thank you as well.

Tags for this Thread

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