C#DataGridView方法行。ADD工作不正确 - 它在上一个上添加行,而不是最后

发布于 2025-01-31 07:47:33 字数 1054 浏览 2 评论 0原文

我对datagridview.rows.add方法有问题。我想添加一条新行,但在最后一行,而不是以前。我不知道为什么在这种情况下该方法添加不起作用。我的代码:

        dataGridView1.RowCount = 3;
        dataGridView1.ColumnCount = 4;

        dataGridView1.Rows[0].Cells[0].Value = 1;
        dataGridView1.Rows[0].Cells[1].Value = 2;
        dataGridView1.Rows[0].Cells[2].Value = 3;
        dataGridView1.Rows[0].Cells[3].Value = 4;

        dataGridView1.Rows[1].Cells[0].Value = 5;
        dataGridView1.Rows[1].Cells[1].Value = 6;
        dataGridView1.Rows[1].Cells[2].Value = 7;
        dataGridView1.Rows[1].Cells[3].Value = 8;

        dataGridView1.Rows[2].Cells[0].Value = 9;
        dataGridView1.Rows[2].Cells[1].Value = 10;
        dataGridView1.Rows[2].Cells[2].Value = 11;
        dataGridView1.Rows[2].Cells[3].Value = 12;


        dataGridView1.Rows.Add(13,14,15,16);

和结果:

“在此处输入图像描述”

我会为任何帮助或建议而感到兴奋!

I have a problem with datagridview.Rows.Add method. I want to add a new line but in the last line, not in previous. I don't know why the method Add doesn't work in this case. My code:

        dataGridView1.RowCount = 3;
        dataGridView1.ColumnCount = 4;

        dataGridView1.Rows[0].Cells[0].Value = 1;
        dataGridView1.Rows[0].Cells[1].Value = 2;
        dataGridView1.Rows[0].Cells[2].Value = 3;
        dataGridView1.Rows[0].Cells[3].Value = 4;

        dataGridView1.Rows[1].Cells[0].Value = 5;
        dataGridView1.Rows[1].Cells[1].Value = 6;
        dataGridView1.Rows[1].Cells[2].Value = 7;
        dataGridView1.Rows[1].Cells[3].Value = 8;

        dataGridView1.Rows[2].Cells[0].Value = 9;
        dataGridView1.Rows[2].Cells[1].Value = 10;
        dataGridView1.Rows[2].Cells[2].Value = 11;
        dataGridView1.Rows[2].Cells[3].Value = 12;


        dataGridView1.Rows.Add(13,14,15,16);

And result:

enter image description here

I will be thrilled for any help or advice!

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

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

发布评论

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

评论(2

撞了怀 2025-02-07 07:47:33

一个更好的想法是不要设置rowcount何时进入新行。因此,如果可以继续阅读。

在下面的示例中,没有断言要验证row.cell.cells [3] .Value是一个INT,但在第二个块中显示了如何正确检查。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        dataGridView1.ColumnCount = 4;

        for (int index = 0; index < dataGridView1.Columns.Count; index++)
        {
            dataGridView1.Columns[index].HeaderText = $"Column {index +1}";
        }

        dataGridView1.Rows.Add(1, 2, 3, 4);

    }

    private void button1_Click(object sender, EventArgs e)
    {

        if (dataGridView1.Rows.Count == 1)
        {
            dataGridView1.Rows.Add(1, 2, 3, 4);
        }

        for (int index = 0; index < 4; index++)
        {
            var row = dataGridView1.Rows.Cast<DataGridViewRow>()
                .LastOrDefault(gridRow => !gridRow.IsNewRow);

            var lastValue = Convert.ToInt32(row.Cells[3].Value) + 1;

            dataGridView1.Rows.Add(
                lastValue, 
                lastValue += 1, 
                lastValue += 1, 
                lastValue += 1);
        }

    }
}

servert单元格值可以表示int

for (int index = 0; index < 4; index++)
{
    var row = dataGridView1.Rows.Cast<DataGridViewRow>()
        .LastOrDefault(gridRow => !gridRow.IsNewRow);

    if (int.TryParse(row.Cells[3].Value.ToString(), out var lastValue))
    {
        lastValue  += 1;

        dataGridView1.Rows.Add(
            lastValue,
            lastValue += 1,
            lastValue += 1,
            lastValue += 1);
    }

}

A better idea is not to set RowCount when you will be entering new rows. So if open to a alternate read on.

In the example below there is no assertion to validate row.Cells[3].Value is an int but in the 2nd block shows how to check correctly.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        dataGridView1.ColumnCount = 4;

        for (int index = 0; index < dataGridView1.Columns.Count; index++)
        {
            dataGridView1.Columns[index].HeaderText = 
quot;Column {index +1}";
        }

        dataGridView1.Rows.Add(1, 2, 3, 4);

    }

    private void button1_Click(object sender, EventArgs e)
    {

        if (dataGridView1.Rows.Count == 1)
        {
            dataGridView1.Rows.Add(1, 2, 3, 4);
        }

        for (int index = 0; index < 4; index++)
        {
            var row = dataGridView1.Rows.Cast<DataGridViewRow>()
                .LastOrDefault(gridRow => !gridRow.IsNewRow);

            var lastValue = Convert.ToInt32(row.Cells[3].Value) + 1;

            dataGridView1.Rows.Add(
                lastValue, 
                lastValue += 1, 
                lastValue += 1, 
                lastValue += 1);
        }

    }
}

Assert cell value can represent an int

for (int index = 0; index < 4; index++)
{
    var row = dataGridView1.Rows.Cast<DataGridViewRow>()
        .LastOrDefault(gridRow => !gridRow.IsNewRow);

    if (int.TryParse(row.Cells[3].Value.ToString(), out var lastValue))
    {
        lastValue  += 1;

        dataGridView1.Rows.Add(
            lastValue,
            lastValue += 1,
            lastValue += 1,
            lastValue += 1);
    }

}

enter image description here

梦途 2025-02-07 07:47:33

这是在网格视图中插入一条线的正确方法:

dataGridView1.Rows.Add(13,14,15,16);

标记为 *的最后一行实际上是新线路的占位符,最好不要直接插入它

This is the correct way to insert a line in a grid view:

dataGridView1.Rows.Add(13,14,15,16);

The last line marked with * is in fact a placeholder for new lines and it is better not to insert it directly
tempe place to make new row

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