在我的程序中,我将来自两个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
填充)。
如何使列表分别填充各自的列?
答案 0 :(得分:1)
可能的情况:
string
值和一个int
值。现在,您使用两个List<T>
作为存储空间(其中T
在一种情况下为string
,在另一种情况下为int
)来显示在DataGridView中列出,当然在两个不同的列中。
此设置不能完全满足要求:按列和行组织的同质数据源可以更好地服务DataGridView。
►当然要注意,我们可以使用DataGridView控件本身输入和编辑数据。有时是实用的,有时则不是。评估。无论如何,用作数据源的对象的集合都需要支持它。
我们可以使用与所需数据类型匹配的属性来构建一个用于存储数据的类对象。
而且,在处理数据的UI表示时,最好使用Framework提供的DataBinding功能:它使所有内容更加线性化。
在WinForms中,BindingList和BindingSource对象提供了许多现成的功能,因为数据和数据绑定会更改事件,自动货币管理等,从而可以同步所有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元素之间提供一层。
使用此类对象,我们可以设置所需的数据绑定:
Current
面板的右侧看到的那些)。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也是一个类)。
所描述操作的可视示例:
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
}
}
结果: