winForms + DataGridView 绑定到 List(视觉工作室 2005、C# 2.0)

发布于 2024-12-26 03:27:36 字数 2215 浏览 0 评论 0原文

我有以下代码,但 DatGridView 显示空行。我找不到解决方案。我已将 AutoGenerateColumns 设置为 false。如果我将其设置为 true,它会创建行,但不允许我将此属性设置为 true (客户希望)。

我做错了什么?这是代码:

public partial class Form1 : Form
{
    private List<AStruct> _aCollectionList;
    private BindingList<AStruct> _aCollectionBindingList;

    public struct AStruct
    {
        public string ACode
        {
            get { return _aCode; }
            set { _aCode = value; }
        }

        public string AName
        {
            get { return _aName; }
            set { _aName = value; }
        }

        private string _aCode;
        private string _aName;
    }

    public Form1()
    {
        InitializeComponent();
        this.dataGridView1.AutoGenerateColumns = false;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        _aCollectionList = new List<AStruct>();
        FillCollectionStruct(true);

        dataGridView1.DataSource = _aList;
        dataGridView1.Refresh();            
    }

    private void button2_Click(object sender, EventArgs e)
    {            
        _aCollectionBindingList = new BindingList<AStruct>();
        FillCollectionStruct(false);
        dataGridView1.DataSource = _aBindingList;
        dataGridView1.Refresh();         
    }

    private void FillCollectionStruct(bool listBool)
    {
        AStruct aStruct = new AStruct();

        for (int i = 0; i < 10; i++)
        {
            aStruct.ACode = i.ToString();
            aStruct.AName = i.ToString();

            if (listBool)
                _aCollectionList.Add(aStruct);
            else
                _aCollectionBindingList.Add(aStruct);
        }
    }
}

在 Form1.Designer 中,我确实创建了以下列:

// 
// aCodeColumn
// 
this.colorCodeColumn.DataPropertyName = "Code";
this.colorCodeColumn.HeaderText = "a code";
this.colorCodeColumn.Name = "aCodeColumn";
// 
// aNameColumn
// 
this.colorNameColumn.DataPropertyName = "Name";
this.colorNameColumn.HeaderText = "a name";
this.colorNameColumn.Name = "aNameColumn";

I have the following code but the DatGridView is showing me empty rows. I can't find the solution. I have set the AutoGenerateColumns to false. If I set it to true, it creates the rows, but I'm not allowed to set this property to true (client wish).

What am I doing wrong? Here's the code:

public partial class Form1 : Form
{
    private List<AStruct> _aCollectionList;
    private BindingList<AStruct> _aCollectionBindingList;

    public struct AStruct
    {
        public string ACode
        {
            get { return _aCode; }
            set { _aCode = value; }
        }

        public string AName
        {
            get { return _aName; }
            set { _aName = value; }
        }

        private string _aCode;
        private string _aName;
    }

    public Form1()
    {
        InitializeComponent();
        this.dataGridView1.AutoGenerateColumns = false;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        _aCollectionList = new List<AStruct>();
        FillCollectionStruct(true);

        dataGridView1.DataSource = _aList;
        dataGridView1.Refresh();            
    }

    private void button2_Click(object sender, EventArgs e)
    {            
        _aCollectionBindingList = new BindingList<AStruct>();
        FillCollectionStruct(false);
        dataGridView1.DataSource = _aBindingList;
        dataGridView1.Refresh();         
    }

    private void FillCollectionStruct(bool listBool)
    {
        AStruct aStruct = new AStruct();

        for (int i = 0; i < 10; i++)
        {
            aStruct.ACode = i.ToString();
            aStruct.AName = i.ToString();

            if (listBool)
                _aCollectionList.Add(aStruct);
            else
                _aCollectionBindingList.Add(aStruct);
        }
    }
}

In the Form1.Designer I do make the following columns:

// 
// aCodeColumn
// 
this.colorCodeColumn.DataPropertyName = "Code";
this.colorCodeColumn.HeaderText = "a code";
this.colorCodeColumn.Name = "aCodeColumn";
// 
// aNameColumn
// 
this.colorNameColumn.DataPropertyName = "Name";
this.colorNameColumn.HeaderText = "a name";
this.colorNameColumn.Name = "aNameColumn";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南七夏 2025-01-02 03:27:36

难道你不能将绑定从: 更改

// aCodeColumn
// 
this.colorCodeColumn.DataPropertyName = "Code";
this.colorCodeColumn.HeaderText = "a code";
this.colorCodeColumn.Name = "aCodeColumn";
// 
// aNameColumn
// 
this.colorNameColumn.DataPropertyName = "Name";
this.colorNameColumn.HeaderText = "a name";
this.colorNameColumn.Name = "aNameColumn";

为:

// aCodeColumn
// 
this.colorCodeColumn.DataPropertyName = "ACode";
this.colorCodeColumn.HeaderText = "a code";
this.colorCodeColumn.Name = "aCodeColumn";
// 
// aNameColumn
// 
this.colorNameColumn.DataPropertyName = "AName";
this.colorNameColumn.HeaderText = "a name";
this.colorNameColumn.Name = "aNameColumn";

这不应该起作用吗?

由于 DataPropertyName 是绑定到属性的名称,因此名称必须相同,否则编译器无法知道什么放在哪里。

Can't you just change the bindings from:

// aCodeColumn
// 
this.colorCodeColumn.DataPropertyName = "Code";
this.colorCodeColumn.HeaderText = "a code";
this.colorCodeColumn.Name = "aCodeColumn";
// 
// aNameColumn
// 
this.colorNameColumn.DataPropertyName = "Name";
this.colorNameColumn.HeaderText = "a name";
this.colorNameColumn.Name = "aNameColumn";

To:

// aCodeColumn
// 
this.colorCodeColumn.DataPropertyName = "ACode";
this.colorCodeColumn.HeaderText = "a code";
this.colorCodeColumn.Name = "aCodeColumn";
// 
// aNameColumn
// 
this.colorNameColumn.DataPropertyName = "AName";
this.colorNameColumn.HeaderText = "a name";
this.colorNameColumn.Name = "aNameColumn";

Shouldn't that work?

Since the DataPropertyName is what binds to your properties the names have to be the same, else there's no way for the compiler to know what goes where.

清风夜微凉 2025-01-02 03:27:36

如果我理解你的问题,你可以使用这样的东西。dataGridView1.Refresh();为了重新填充它,并在刷新行之前放置 this.dataGridView1.DataSource = _aCollectionBindingList;

If I understand your problem write you could use sth like this.dataGridView1.Refresh(); in order to repopulate it and before the line you refresh put this.dataGridView1.DataSource = _aCollectionBindingList;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文