动态显示ListView列中的字符串 - 不起作用

时间:2016-09-21 09:50:25

标签: c# winforms listview

我的目标如下: The user enters the stuff as shown in this picture. The 3 items (source file, destination directory and if file exists) are saved in a string array. User clicks OK.

After clicking OK the previous window is closed and the user is taken to the main form shown in this picture. The previously mentioned source file and the destination directory are shown in the table.

我使用以下代码显示输入的文件&表中的目录:

    private void okButton_Clicked(object sender, EventArgs e) 
    {
        //saving user's input
        userInput = new string[3];
        userInput[0] = sourceFileTextBox.Text;
        userInput[1] = destinationDirComboBox.SelectedItem.ToString();
        userInput[2] = ifFileExistsComboBox.SelectedItem.ToString();

        //creating a new ListView object - the object is derived from the ListView Class 
       and has absolutely nothing in the constructor or anywhere
       Classes.ListViewDerivative lvd = new Classes.ListViewDerivative();
       ListViewItem item1 = new ListViewItem(userInput[0]);
       item1.SubItems.Add(userInput[1]);
       lvd.Items.AddRange(new ListViewItem[] { item1 });
       this.DialogResult = DialogResult.OK;
       this.Hide();
    }

它不起作用。无论我把这段代码放在哪里,表都是空的 - 我试图将这个改编的代码放在ListViewDerivative构造函数中,ListViewDerivative类和editFileEntry(第一张图片)类中的函数。右侧文本保存在数组中,但未显示在表中。请帮忙!

1 个答案:

答案 0 :(得分:0)

不使用lvd变量。您必须将'lvd'控件添加到主视图中。您可以使用Designer,在“FilePickerDialog”的对话框结果中,您可以将结果分配给数据网格。

MainView的一些伪代码

void    Config_Clicked()
{
   ConfigDlg dlg = new ConfigDlg();
   if(dlg.ShowDialog() == OK)
   {
       this.myListView1.Items.Add(dlg.userInput[0]);
   }
}