Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Windows Forms ListView get selected row values

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();

    }
like image 916
n3bi0s Avatar asked Jan 20 '26 09:01

n3bi0s


1 Answers

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.

like image 110
Rob Avatar answered Jan 21 '26 21:01

Rob