如何将项目列表添加到多列DataGridView?

时间:2020-07-21 13:44:10

标签: c# winforms datagridview

在我的程序中,我将来自两个TextBox控件的输入存储在两个单独的列表中,这些列表将用于填充DataGridView。

除了将文本框值存储在列表中之外,我实际上尚未添加任何代码:

List<string> lTypes = new List<string>();
List<int> lAmounts = new List<int>();

private void btnAddItemL_Click(object sender, EventArgs e)
{
    lTypes.Add(txtItemL.Text);
    lAmounts.Add(int.Parse(txtAmountL.Text));
}

我有两个单独的列,分别是“类型”(由lTypes填充)和“金额”(由lAmounts填充)。

如何使列表分别填充各自的列?

2 个答案:

答案 0 :(得分:1)

可能的情况:

  • 两个TextBox控件用于输入一些数据,一个string值和一个int值。
  • 数据应存储并显示在DataGridView中。
    • 尚不清楚是否涉及其他控件,共享相同数据或可以对其进行修改:我们假设其他控件可能现在或将来都愿意参与。
  • 数据可能需要在某个时候进行序列化或永久存储。
  • 数据也可以在不同的用户界面中显示。

现在,您使用两个List<T>作为存储空间(其中T在一种情况下为string,在另一种情况下为int)来显示在DataGridView中列出,当然在两个不同的列中。

此设置不能完全满足要求:按列和行组织的同质数据源可以更好地服务DataGridView。

►当然要注意,我们可以使用DataGridView控件本身输入和编辑数据。有时是实用的,有时则不是。评估。无论如何,用作数据源的对象的集合都需要支持它。

我们可以使用与所需数据类型匹配的属性来构建一个用于存储数据的类对象。
而且,在处理数据的UI表示时,最好使用Framework提供的DataBinding功能:它使所有内容更加线性化。

在WinForms中,BindingListBindingSource对象提供了许多现成的功能,因为数据和数据绑定会更改事件,自动货币管理等,从而可以同步所有UI元素。需要共享数据源。

我们只需要构建一个可以描述我们的数据的对象,生成这些数据对象的List(使用BindingList),然后使用BindingSource将所有UI元素绑定到List。 BindingSource可用作公开DataSource属性的控件(作为DataGridView)和使用DataBindings的控件的数据源。

基本单位对象

public class StorageObject
{
    public StorageObject() { }
    public StorageObject(string type, int amount)
    {
        this.StorageType = type;
        this.Amount = amount;
    }

    public string StorageType { get; set; }
    public int Amount { get; set; }
}

让我们构建一个 Manager 对象,该对象公开我们需要设置数据绑定的实体:

  • 该类对象处理一个BindingList<StorageObject>和一个BindingSource,链接到BindingList,该BindingSource提供控件绑定共享和呈现数据所需的数据绑定,以及添加新对象或编辑/更新现有对象。
  • 该类还公开了一些方法,这些方法允许以受控的方式在数据源中添加或删除数据单元(此处简化以演示该功能)。

MyStorage类在存储单元和需要使用每个单元保存的数据的UI元素之间提供一层。

使用此类对象,我们可以设置所需的数据绑定:

  • 将DataGridView的数据源设置为BindingSource。
  • 创建两个绑定以将两个TextBox控件绑定到同一源(在动画Current面板的右侧看到的那些)。
  • 再添加两个(未绑定)TextBox控件以输入数据(在动画底部看到的那些)。
public partial class SomeForm : Form
{
    private MyStorage myStorage = null;

    public SomeForm() {
        InitializeComponent();
        myStorage = new MyStorage();

        myDataGridView.DataSource = myStorage.StorageSource; 
        txtCurrentType.DataBindings.Add("Text", myStorage.StorageSource, "StorageType", false, DataSourceUpdateMode.OnPropertyChanged);
        txtCurrentAmount.DataBindings.Add("Text", myStorage.StorageSource, "Amount", true, DataSourceUpdateMode.OnPropertyChanged);
    }
}

要使用可视示例中所示的按钮(在动画中, StorageObject {{ 1}} 是底部的两个文本框控件,靠近 txtType 按钮):

txtAmount

要使用按钮从集合中删除对象,请执行以下操作:

Add...

您可以将 private void btnAddStorageObject_Click(object sender, EventArgs e) { if (int.TryParse(txtAmount.Text, out int amount) && txtType.TextLength > 0) { myStorage.Add(txtType.Text, amount); } } 对象传递给另一个Form,并以在这种情况下有意义的任何方式呈现,编辑或以其他方式使用其指向的数据:
private void btnRemoveCurrent_Click(object sender, EventArgs e) { // Remove the object that is Current in the BindingSource myStorage.Remove(myStorage.StorageSource.Current as StorageObject); // Or, pick one or the other if (int.TryParse(txtCurrentAmount.Text, out int amount)) { myStorage.Remove(txtCurrentType.Text, amount); } } 是一个表单,如可视示例所示。向该表单中添加一个接受MyStorage作为参数的构造函数)

InputDialog

仅此而已。如果需要绑定其他控件,则只需使用 BindingSource (BindingSource)。我们还可以将此对象引用传递给其他类(一个Form也是一个类)。

所描述操作的可视示例

WinForms DataBindings

private void btnUseDialog_Click(object sender, EventArgs e) { var inputDialog = new InputDialog(myStorage.StorageSource); inputDialog.ShowDialog(); } 数据管理器类对象

myStorage.StorageSource

答案 1 :(得分:1)

根据您的描述,您想要将列表项添加到多列dataGridView。

您可以尝试以下代码来解决此问题。

   public partial class Form1 : Form
    {
        //Create a Commodity class, including lType, lAmount properties
        public class Commodity 
        {
            public string lType { get; set; }
            public int lAmount { get; set; }           
        }
        public Form1()
        {
            InitializeComponent();
            
        }
        
        List<Commodity> list = new List<Commodity>();//Create a List<Commodity> collection
        private void btnAddItemL_Click(object sender, EventArgs e)
        {
            Commodity co = new Commodity(); //Create an object of type Commodity
            co.lType = txtItemL.Text; //Assign values to the property of the object
            co.lAmount = Convert.ToInt32(txtAmountL.Text);
            list.Add(co);//Add the object to list
            /* When the data source of the DataGridview is the list, the data will not be updated when the list is operated and then bound. Using BindingList can solve this problem well.*/
            var bindingList = new BindingList<Commodity>(list);   //List<T> is converted to BindingList<T>
            var source = new BindingSource(bindingList, null);    //Create a data source of type BindingSource with the specified BindingList<T>            
            dataGridView1.DataSource = source;//bind your dataGridView to the BindingSource   

        }
    }

结果:

enter image description here