3

I have a ListView populated by a SQL db and want to be able to edit the values once I select a row and click the edit button.

When I click the "edit" button the values from the selected row should be set in the textboxes, but I get this error "Object reference not set to an instance of an object." Why isn't this working?

private void btnEdit_Click(object sender, EventArgs e)
    {
        this.txtid.Text = lvBrands.SelectedItems["id"].Text.ToString();  
        this.txtName.Text = lvBrands.SelectedItems["name"].Text.ToString();

    }
6
  • Did you look at the values of all the objects referenced in the method? (at least one of them is null) Commented Mar 23, 2013 at 15:38
  • yes.. bith of them are null... but why ? I have also tried to use [0] index instead of the column name... stil doesn't work Commented Mar 23, 2013 at 15:40
  • i think you need to attach the code that initiates your lvBrands if any chance of a solution EDIT: Actually i think if you just put a breakpoint in and make sure that the exact text in selected items is id and name you might find your answer Commented Mar 23, 2013 at 15:43
  • if your ListView is in report mode (i.e. it looks like a grid) then you will need the SubItems property. lvBrands.SelectedItems gets you each items in the list view - SubItems gets you the columns. So lvBrands.SelectedItems[0].SubItems[0] is the second column value Commented Mar 23, 2013 at 15:44
  • @Rob thats it :) that was the solution :) thank you... how do i mark your answer as the solution ? Commented Mar 24, 2013 at 23:20

1 Answer 1

3

if your ListView is in report mode (i.e. it looks like a grid) then you will need the SubItems property. lvBrands.SelectedItems gets you each items in the list view - SubItems gets you the columns. So lvBrands.SelectedItems[0].SubItems[0] is the second column value.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.