简体   繁体   English

如何将组合框的选定索引添加到列表视图中的列

[英]How to add selected index of a combobox to a column in a listview

I have found variations of this, but nothing that fixed the problem. 我发现了这种变化,但是没有解决问题的方法。 So here it goes: 所以就这样:

I am using Winforms. 我正在使用Winforms。 I have a combobox and a listview with 3 columns. 我有一个组合框和3列的列表视图。 The user can either select an existing value from the combobox or add their own value through a textbox and a button. 用户可以从组合框中选择一个现有值,也可以通过文本框和按钮添加自己的值。 This all works okay. 一切正常。

Here is where I run into problems: I want the user to be able to select a value, in this case a category, from the combobox and display that in the listview under the "Category" column. 这是我遇到问题的地方:我希望用户能够从组合框中选择一个值(在本例中为类别),并将其显示在“类别”(Category)列下的列表视图中。

I am in the process of teaching myself C#, this is day 2 so bear with me please. 我正在教自己C#,这是第二天,所以请多多包涵。

My listview code: 我的列表视图代码:

    private void Form1_Load(object sender, EventArgs e)
    {
        //Calls the function that adds properties to the listview
        PopulateListView();
        btnEditDesiredEnd.BackColor = Color.LightGray;
        btnDeposit.BackColor = Color.LightGray;

    }

    public void PopulateListView()
    {
        //Listview Properties:
        //Listview Details 
        lstView.View = View.Details;
        //Allow user to edit labels
        lstView.LabelEdit = true;
        //Allow user to change column order
        lstView.AllowColumnReorder = true;
        //Display checkboxes
        //lstView.CheckBoxes = true;
        //Display gridlines
        lstView.GridLines = true;
        //Allows user to select entire row
        lstView.FullRowSelect = true;


        //Create columns, width of -2 indicates auto-size
        lstView.Columns.Add("Transaction", 70, HorizontalAlignment.Center);
        lstView.Columns.Add("Category", 130, HorizontalAlignment.Center);
        lstView.Columns.Add("Description", -2, HorizontalAlignment.Left);

        //Add listview as a control
        this.Controls.Add(lstView);
    }

And here is the button that should add all the user inputs to the listview. 这是应该将所有用户输入添加到列表视图的按钮。 The only line that does not work is the last line. 唯一无效的行是最后一行。

It gives me the following error: Cannot assign 'Add' because it is a 'method group' . 它给我以下错误: 无法分配“添加”,因为它是一个“方法组”

private void btnDeposit_Click(object sender, EventArgs e)
    {
        lstView.View = View.Details;

        ListViewItem lvwItem = lstView.Items.Add(txtDeposit.Text);
        lvwItem.SubItems.Add(txtWithdraw.Text);
        lvwItem.SubItems.Add(txtDescription.Text);

        txtDeposit.Clear();
        txtWithdraw.Clear();
        txtDescription.Clear();

        btnDeposit.Enabled = false;
        btnDeposit.BackColor = Color.LightGray;

        lstView.Items.Add = cboCategory.SelectedIndex;

    }

EDIT: Here is the working code.. (cboCategory is the combobox) 编辑:这是工作代码。(cboCategory是组合框)

private void btnDeposit_Click(object sender, EventArgs e)
    {
        lstView.View = View.Details;
        ListViewItem lvwItem = lstView.Items.Add(txtDeposit.Text);
        if (cboCategory.SelectedItem != null)
        {
            lvwItem.SubItems.Add(cboCategory.SelectedItem.ToString());
        }
        lvwItem.SubItems.Add(txtDescription.Text);

The .Add is a method so it needs to be called like a method. .Add是一个方法,因此需要像一个方法一样被调用。

lstView.Items.Add("Adding this string to the list view");

However I don't know what cboCategory is so I cant advise on what youre trying to add. 但是我不知道cboCategory是什么,所以我不能建议您尝试添加什么。

As stated by Sriram above this line is completely wrong, well actually we don't know what your cboCategory holds either. 正如Sriram所说的,这条线是完全错误的,实际上我们也不知道您的cboCategory拥有什么。

lstView.Items.Add = cboCategory.SelectedIndex;

Sure you are not supposed to do a: 确定您不应该这样做:

lstView.Items.Add(lvwItem);

And/Or... 和/或...

var selectedValue = lstView.Items.FirstOrDefault( c=>whateversearchparameters here);
if( selectedValue!=null )
      SelectAndStuffHere();

Bit hard to tell without knowing more :) 有点不知道就很难说:)

But hope it helps, 但希望能有所帮助,

Cheers 干杯

Stian 了Stian

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

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