循环遍历特定列,然后将值分配给按钮

Looping through a specific column and then assigning a value to a button

我正在做一项学校作业,我将数据从 CSV 文件导入 WinForm 中的 DataGridView。这一切都很完美,但是,现在我想为按钮分配一个值(在第 5、6 和 8 列中)这些都可能因第 3 列中同一行中的值而异。如果第 3 列的值是同一行的“输入”,则在第 5 列中,按钮需要是“输入”,该行中第 5 列之后的其余单元格需要是只读文本框。

如果第 3 列的值在同一行中为“输出”,则在第 5 列中,按钮需要为“打开”,在第 6 列中需要为“关闭”,在第 8 列中需要为是“#s”

我试过:

遍历所有单元格,foreach 循环,遍历第 3 列,但我不确定如何更改同一行中按钮的值。 (当你点击它们时,它们也需要有不同的功能,但我认为当我理解它是如何工作的时候会更容易)

Here为需要使用的CSV文件预览

这是我到目前为止编写的代码(有效):

private void btnBevestig_Click(object sender, EventArgs e)
{
    try
    {
        LaadCSVToDataGrid(openFile.FileName);
        dataGridView1.Sort(dataGridView1.Columns[3], ListSortDirection.Ascending);
        dataGridView1.Columns[0].Visible = false;
        dataGridView1.Columns[1].ReadOnly = true;
        dataGridView1.Columns[2].Visible = false;
        dataGridView1.Columns[3].ReadOnly = true;
        dataGridView1.Columns[4].Visible = false;

        DataGridViewButtonColumn btnInput = new DataGridViewButtonColumn();
        dataGridView1.Columns.Add(btnInput);
        btnInput.HeaderText = "Functie";
        btnInput.Text = "Input";
        btnInput.Name = "colInput";
        btnInput.UseColumnTextForButtonValue = true;

        DataGridViewButtonColumn btnOff = new DataGridViewButtonColumn();
        dataGridView1.Columns.Add(btnOff);
        btnOff.HeaderText = "";
        btnOff.Text = "Off";
        btnOff.Name = "colInput";
        btnOff.UseColumnTextForButtonValue = true;

        DataGridViewTextBoxColumn txtAantalSeconden = new DataGridViewTextBoxColumn();
        dataGridView1.Columns.Add(txtAantalSeconden);
        txtAantalSeconden.HeaderText = "";
        txtAantalSeconden.Name = "txtAantalSec";
        txtAantalSeconden.ReadOnly = false;

        DataGridViewButtonColumn btnOnSec = new DataGridViewButtonColumn();
        dataGridView1.Columns.Add(btnOnSec);
        btnOnSec.HeaderText = "";
        btnOnSec.Text = "Aantal seconden";
        btnOnSec.Name = "colInput";
        btnOnSec.UseColumnTextForButtonValue = true;

        btnOntgrendel.Enabled = true;
        comboBoxProfiel.Enabled = false;
        btnBevestig.Enabled = false;
        btnKiesProfiel.Enabled = false;
        btnNieuwProfiel.Enabled = false;

    }
    catch (Exception error)
    {
        MessageBox.Show(
            error.Message,
            "Fout",
            MessageBoxButtons.OK,
            MessageBoxIcon.Warning
            );
    }
}

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    Console.WriteLine(e.ColumnIndex);
    Console.WriteLine(e.RowIndex);
    if (e.ColumnIndex == 3 && e.RowIndex >= 0)
    {
        if (dataGridView1[e.ColumnIndex, e.RowIndex].ToString() == "Input")
        {
            dataGridView1[5, e.RowIndex].Value = "Input";
            Console.WriteLine(e.ColumnIndex);
            Console.WriteLine(e.RowIndex);
        }
    }
}

由于填充数据后添加了额外的列,因此需要循环并根据Input单元格的值设置目标单元格的值。

private void UpdateCells()
{
    foreach (var r in dataGridView1.Rows.OfType<DataGridViewRow>()
        .Where(x => !x.IsNewRow))
    {
        var v = r.Cells[3].Value?.ToString();

        if (v == "Input")
        {
            r.Cells[5].Value = "Input";
            // other changes ...
        }
        else
        {
            r.Cells[5].Value = "On";
            // Other changes ...
        }    
    }
}

设置网格后调用此方法:

private void btnBevestig_Click(object sender, EventArgs e)
{
    try
    {
        LaadCSVToDataGrid(openFile.FileName);
        dataGridView1.Sort(dataGridView1.Columns[3], ListSortDirection.Ascending);
        dataGridView1.Columns[0].Visible = false;
        btnInput.UseColumnTextForButtonValue = false;
        // ...

        UpdateCells()
    }
    catch { ... }
}

如果代码在运行时更改了只读第三列的值,则还要处理 CellValueChanged 事件以更新目标单元格:

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 3 && e.RowIndex >= 0 && !dataGridView1.Rows[e.RowIndex].IsNewRow)
    {
        var v = dataGridView1[e.ColumnIndex, e.RowIndex].Value?.ToString();

        if (v == "Input")
        {
            dataGridView1[5, e.RowIndex].Value = "Input";
            // other changes ...
        }
        else
        {
            dataGridView1[5, e.RowIndex].Value = "On";
            // Other changes ...
        }
    }
}