【问题标题】:Setting maximum number of rows in DataGridView?在 DataGridView 中设置最大行数?
【发布时间】:2018-02-19 16:20:13
【问题描述】:

我有一个程序,一旦用户单击按钮,就会向 DataGridView 添加行。我需要将行数限制为最多 10 行。

这是我的代码:

public partial class Form1 : Form 
{
    private Int32 MaxRows { get; set; }

    public Form1()
    {
        MaxRows = 10;
        InitializeComponent();

        dataGridView1.UserAddedRow += dataGridView1_RowCountChanged;
        dataGridView1.UserDeletedRow += dataGridView1_RowCountChanged;
    }

    private void dataGridView1_RowCountChanged(object sender, EventArgs e)
    {
        CheckRowCount();
    }

    private void CheckRowCount()
    {
        if (dataGridView1.Rows != null && dataGridView1.Rows.Count > MaxRows)
        {
            dataGridView1.AllowUserToAddRows = false;
        }
        else if (!dataGridView1.AllowUserToAddRows)
        {
            dataGridView1.AllowUserToAddRows = true;
        }
    }

    public void button1_Click(object sender, EventArgs e)
    {
        this.dataGridView1.Rows.Add("This is a row.");
    }

}

我从此处发布的另一个问题中获得了代码(似乎找不到链接),但代码不起作用,我能够在我的 DataGridView 中创建超过 11 行。发生这种情况的任何原因?

【问题讨论】:

    标签: c# visual-studio oop visual-studio-2013 datagridview


    【解决方案1】:

    CheckRowCount() 可以简化。您只需要在达到最大限制时禁用AllowUserToAddRows

    private void CheckRowCount()
        {
            // The data grid view's default behavior is such that it creates an additional row up front.
            // e.g. when you add 1st row, it creates 2nd row automatically.
            // If you use Count < MaxRows, the user won't be able to add the 10th row.
            if (dataGridView1.Rows.Count <= MaxRows)
            {
                dataGridView1.AllowUserToAddRows = true;
            }
            else
            {
                dataGridView1.AllowUserToAddRows = false;
            }
        }
    

    在单击按钮时添加新行时,请确保当前行数小于MaxRows。您还应该调用CheckRowCount 方法以确保相应地设置AllowUserToAddRows。修改按钮点击处理程序如下:

    private void button1_Click(object sender, EventArgs e)
        {
            // If the rows count is less than max rows, add a new one.
            if (dataGridView1.Rows.Count < MaxRows)
            {
                this.dataGridView1.Rows.Add("This is a row.");
    
                // Check the row count again.
                CheckRowCount();
            }
        }
    

    【讨论】:

      【解决方案2】:

      也许这种方式更容易?

      private Int32 MaxRows { get; set; }
      
      public Form1()
      {
          MaxRows = 10;
          InitializeComponent();
      }
      
      public void button1_Click(object sender, EventArgs e)
      {
          if (dataGridView1.Rows.Count <= MaxRows)
          this.dataGridView1.Rows.Add("This is a row.");
      }
      

      此外,AllowUserToAddRows 属性为:“如果向用户显示添加行选项,则为 true;否则为 false。默认值为 true”与从后面的代码添加行无关。它指的是在您的应用运行时通过单击数据网格将新行添加到网格中的选项。

      【讨论】:

      • 如果您需要一些无法添加更多行的视觉反馈,您应该只在数据网格有 10 行时禁用按钮。
      猜你喜欢
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-23
      • 1970-01-01
      • 1970-01-01
      • 2012-01-02
      • 1970-01-01
      相关资源
      最近更新 更多