简体   繁体   English

更新ListView上的SelectedItem

[英]Update SelectedItem on ListView

I have a ListView that displays multiple rows of ListViewItems. 我有一个ListView,它显示ListViewItems的多行。 The user is able to edit the row by clicking the edit button which opens up another form displaying the selected row and the data within it. 用户可以通过单击“编辑”按钮来编辑该行,这将打开另一个显示所选行及其中数据的表单。 The problem I am having is that I cannot seem to update the ListViewItem on the parent form when I press the update button. 我遇到的问题是,当我按下更新按钮时,似乎无法更新父窗体上的ListViewItem。 The code I am using keeps throwing the exception message "An unhandled exception of type 'System.NullReferenceException' occurred in ToDoList.exe". 我正在使用的代码不断抛出异常消息“ ToDoList.exe中发生了'System.NullReferenceException类型的未处理异常”。 I have tried different approaches to updating the selected item but cannot seem to get a working code. 我尝试了不同的方法来更新所选项目,但似乎无法获得有效的代码。

This is the code I am using on the form that displays the selected row, button1 is the "Update Row" button that should update the listView. 这是我在显示所选行的窗体上使用的代码,button1是“ Update Row”按钮,应更新listView。

private void button1_Click(object sender, EventArgs e)
    {
        Form1 form1 = (Form1)this.Owner;

        int i = 0;
        ListViewItem item = form1.listView1.SelectedItems[i];
        textBox1.Text = item.SubItems[0].Text;
        richTextBox1.Text = item.SubItems[1].Text;
        comboBox1.Text = item.SubItems[2].Text;
        dateTimePicker1.Text = item.SubItems[3].Text;

        this.Close();
    }

To make a form owned by another form, assign its Owner property a reference to the form that will be the owner. 若要使一个表单归另一个表单所有,请将其Owner属性分配给将作为所有者的表单的引用。

https://msdn.microsoft.com/en-us/library/system.windows.forms.form.owner%28v=vs.110%29.aspx https://msdn.microsoft.com/zh-CN/library/system.windows.forms.form.owner%28v=vs.110%29.aspx

You need to set the Owner property first 您需要先设置Owner属性

Finally managed to fix the problem. 终于设法解决了这个问题。 There were two issues with my code. 我的代码有两个问题。 One was with the way I was opening up the child Form3 and not referring to Form1 as a class and the other was with the code I was using to edit a row not functioning correctly. 一种是与我打开子Form3而不是将Form1称为类的方式有关,另一种与我用来编辑行无法正常工作的代码有关。

Changed it to this and came to the solution; 将其更改为此并找到解决方案;

Form1 code - method of opening Form3 with the data from the selected row, originally used the code: Form1代码-使用选定行中的数据打开Form3的方法,最初使用的代码是:

private void button2_Click(object sender, EventArgs e)
    {
        if (listView1.SelectedItems.Count == 0) return;

        Form3 form3 = new Form3();

        int i = 0;
        ListViewItem item = listView1.SelectedItems[i];
        string title = item.SubItems[0].Text;
        string description = item.SubItems[1].Text;
        string priority = item.SubItems[2].Text;
        string datedue = item.SubItems[3].Text;
        form3.textBox1.Text = title.ToString();
        form3.richTextBox1.Text = description.ToString();
        form3.comboBox1.Text = priority.ToString();
        form3.dateTimePicker1.Text = datedue.ToString();

        form3.Show();  
    }

But changed it to this: 但是将其更改为:

private void button2_Click(object sender, EventArgs e)
    {
        if (listView1.SelectedItems.Count == 0) return;

        using (Form3 form3 = new Form3())
        {
            int i = 0;
            ListViewItem item = listView1.SelectedItems[i];
            string title = item.SubItems[0].Text;
            string description = item.SubItems[1].Text;
            string priority = item.SubItems[2].Text;
            string datedue = item.SubItems[3].Text;
            form3.textBox1.Text = title.ToString();
            form3.richTextBox1.Text = description.ToString();
            form3.comboBox1.Text = priority.ToString();
            form3.dateTimePicker1.Text = datedue.ToString();

            form3.ShowDialog(this);
        }
    }

And finally changed the code in Form3 to this: 最后将Form3中的代码更改为此:

private void button1_Click(object sender, EventArgs e)
    {
        Form1 form1 = (Form1)this.Owner;

        int i = 0;
        ListViewItem item = form1.listView1.SelectedItems[i];
        item.SubItems[0].Text = textBox1.Text;
        item.SubItems[1].Text = richTextBox1.Text;
        item.SubItems[2].Text = comboBox1.Text;
        item.SubItems[3].Text = dateTimePicker1.Text;

        this.Close();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM