Getting values of multiple columns on the same row of a ListView

c# forms winforms listview

362 просмотра

1 ответ

60 Репутация автора

I have a ListView table and I want the user to be able to select a value in the table and click edit which will open another form for them to edit. What is the best way to pass variables back and fourth and re-populate tables?

Here is what I have so far:

private void editSelected_Click(object sender, EventArgs e)
        for (int i = 0; i < basket.Items.Count; i++)
            if (basket.Items[i].Selected)
                string product = //get product string from table
                string quantity = //get quantity string from table
                string price = //get price string from table

enter image description here

enter image description here

Автор: kieron oates Источник Размещён: 10.04.2017 12:46

Ответы (1)

1 плюс

3325 Репутация автора


Like this, To avoid exceptions you should set MultiSelect to false,

listView1.MultiSelect = false;

Form1 (Your main form which contains ListView),

public ListView _ListView {
     get { return this.listView1; }
private void button1_Click(object sender, EventArgs e){
    Form2 frm = new Form2(this);
    frm.productName = listView1.Items[listView1.SelectedItems[0].Index].SubItems[0].Text; // set defined variable value (for example, index of subitems 0 represents ProductName)

And Edit Form's code behind looks like;

private Form1 _OpenerForm;
public string productName = "";
public Form2(Form1 OpenerForm)
   _OpenerForm = OpenerForm;
private void Form2_Load(object sender, EventArgs e)
    textBox1.Text = productName;

private void submitButton_Click(object sender, EventArgs e){
    _OpenerForm._ListView.Items[_OpenerForm._ListView.SelectedItems[0].Index].SubItems[0].Text = textBox1.Text;

Hope helps,

Автор: Berkay Размещён: 10.04.2017 12:58
Вопросы из категории :