【问题标题】:Showing more than one row in DataGridView in C#在 C# 中的 DataGridView 中显示多于一行
【发布时间】:2020-12-21 21:16:11
【问题描述】:

为什么我不能在 C# 中的 DataGridView 上显示多于一行?

这是我的代码:

btnAdd_Click 事件:

    QuestionGroup _question = new QuestionGroup(); 
    _question.QGID = QGID; // int
    _question.QGName = QGName; // string
    ToQuestionList(_question);
    MessageBox.Show("Item Added successfully.", Application.ProductName, MessageBoxButtons.OK, 
                     MessageBoxIcon.Information);

ToQuestionList 函数:

    void ToQuestionList(QuestionGroup q)
    {
        Questions.Add(q);
        dataGridView1.DataSource = Questions;
    }

问题类:

    public class QuestionGroup
        {
            public int QGID { get; set; }
            public string QGName { get; set; }
        }

【问题讨论】:

  • 什么是Questions,它在哪里/如何定义?当您将Questions 作为数据源分配给您的gridview 时,它的内容是什么?
  • 在设置为 Questions 之前将 DataSource 设置为 null
  • ``` 问题 ``` 是全局定义的。 ``` #region Resources List Questions = new List(); #endregion ``` Questions 实例列表。当使用填写字段并按“添加”按钮时。
  • 谢谢@Steve。解决了。我拥有你一个人UwU

标签: c# windows winforms datagridview


【解决方案1】:

您的问题是由 DataGridView 查看您要设置为其数据源的对象引起的。它发现它与第一次单击时绑定的对象相同,因此它不会更改其显示。使用相同的对象设置 Datasource 不会强制 DataGridView 查看对象中是否有更多元素。

第一个解决方法可能是:

void ToQuestionList(QuestionGroup q)
{
    Questions.Add(q);
    dataGridView1.DataSource = null;
    dataGridView1.DataSource = Questions;
}

这将强制网格重新绑定所有内容并显示新元素。
但是,使用 BindingSource 实例有更好的方法。

您需要在类级别声明一个像这样的 BindingSource 类型的对象

public class Form1: Form
{
     BindingSource data = new BindingSource();
     List<QuestionGroup> Questions = new List<QuestionGroup>();
     ....

然后在表单加载事件中你应该添加这些行

public void Form_Load(object sender, EventArgs e)
{
     data.DataSource = Questions;
     dataGridView1.DataSource = data;
     ....

最后您可以将 ToQuestionList 更改为(但此时您也可以将其删除)

void ToQuestionList(QuestionGroup q)
{
    bs.Add(q);
}

【讨论】:

    【解决方案2】:

    你是对的,如果你想在 DataGridView 中显示数据,明智的做法是不要直接编辑行和单元格,而是使用 DataSource。

    您使用什么类型的 DataSource 取决于您想对您的数据做什么。

    • 如果您只想显示初始数据,不想更改它,那么您可以使用任何列表/数组,甚至 IEnumerable 或 ICollection 就足够了。
    • 如果要更改要显示的数据,则需要使用实现 IBindingList 的对象。这可确保您的软件对数据所做的更改会显示在 DataGridView 中,并且操作员在 DataGridView 中所做的更改会在源数据中更新。

    对于后一种情况,在BindingList 中显示您的数据就足够了

    // the DataGridView that shows Customers:
    private DataGridView CustomerView => this.customerView;
    
    // The data in the DataGridView:
    private BindingList<Customer> DisplayedCustomers
    {
        get => (BindingList<Customer>)this.CustomerView.DataSource;
        set => this.CustomerView.DataSource = value;
    }
    
    // constructor:
    public MyForm()
    {
        InitializeComponent();   // creates member this.customerView;
    
        // initally show an empty customers collection
        this.DisplayedCustomers = new BindingList<Customer>();
    }
    

    一段时间后(表单加载?按下按钮后?获取客户,并显示数据:

    private void InitCustomerView()
    {
        var customers = this.GetCustomers().ToList();
        this.DisplayedCustomers = new BindingList<Customer> customers;
    }
    

    如果您正确定义了列,这足以显示您的数据,每个客户一行:

    dataGridViewColumnId.DataPropertyName = nameof(Customer.Id);
    dataGridViewColumnName.DataPropertyName = nameof(Customer.Name);
    ...
    

    添加/删除显示的客户:

    private void AddCustomer(Customer customer)
    {
        this.DisplayedCustomers.Add(customer);
    }
    
    private void RemoveCustomer(int customerId)
    {
        int index = this.FindCustomerIndex(customerId);
        this.DisplayedCustomers.RemoveItem(index);
    }
    
    private int FindCustomerIndex(int customerId)
    {
        // TODO: use (Collection<Customer>)this.DisplayedCustomers to find index
        // of customer with Id == customerId        
    }
    

    在操作员发出完成对客户的编辑信号后获取所有客户:

    private void ButtonFinishedEditing_Clicked(object sender, ...)
    {
        ICollection<T> customers = this.DisplayedCustomers();
        this.ProcessEditedCustomers(customers);
    }
    

    结论:

    将您的数据放入BindingList&lt;T&gt;,并将其放入datagridview 的DataSource 中。在每个接受的编辑行之后,BindingList 中的数据都会更新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多