Click here to Skip to main content
15,561,685 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm working on a school assignment where I import data into a DataGridView in WinForm from a CSV file. This all works perfect, however, now I want to assign a value to a button (In column 5, 6 and 8) These can all vary depending on what the value in the same row, in column 3 is. If the value from column 3 is "Input" on the same row, in column 5, the button needs to be "Input", the rest of the cells in that row after column 5 need to be readonly textboxes.

If the value from column 3 is "Output" on the same row, in column 5, the button needs to be "On", in column 6 it needs to be "Off" and in column 8 it needs to be "On #s"

I've tried:

Looping through all the cells, foreach loops, looping through column 3, but I'm not sure how I can change the value of button in the same row. (They also need to have a different function when you click them, but I think that is easier when I understand how this works)

Here is a preview of the CSV file that needs to be used.

This is the code I've written so far (thats working):
private void btnBevestig_Click(object sender, EventArgs e)
        dataGridView1.Sort(dataGridView1.Columns[3], ListSortDirection.Ascending);
        dataGridView1.Columns[0].Visible = false;
        dataGridView1.Columns[1].ReadOnly = true;
        dataGridView1.Columns[1].Width = 250;
        dataGridView1.Columns[2].Visible = false;
        dataGridView1.Columns[3].ReadOnly = true;
        dataGridView1.Columns[4].Visible = false;

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

        DataGridViewButtonColumn btnOff = new DataGridViewButtonColumn();
        btnOff.HeaderText = "";
        btnOff.Text = "";
        btnOff.Name = "colInput";
        btnOff.UseColumnTextForButtonValue = true;

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

        DataGridViewButtonColumn btnOnSec = new DataGridViewButtonColumn();
        btnOnSec.HeaderText = "";
        btnOnSec.Text = "";
        btnOnSec.Name = "colInput";
        btnOnSec.UseColumnTextForButtonValue = true;

        btnOntgrendel.Enabled = true;
        comboBoxProfiel.Enabled = false;
        btnBevestig.Enabled = false;
        btnKiesProfiel.Enabled = false;
        btnNieuwProfiel.Enabled = false;
    catch (Exception error)

What I have tried:

I've tried doing loops, everything worked fine up until the point that I have to edit the button's text.
Updated 15-Feb-21 21:10pm

1 solution

As to me, you wanted to add calculated column. See: DataColumn.Expression Property (System.Data) | Microsoft Docs[^]

private void CalcColumns()
    DataTable table = new DataTable ();

    // Create the first column.
    DataColumn priceColumn = new DataColumn();
    priceColumn.DataType = System.Type.GetType("System.Decimal");
    priceColumn.ColumnName = "price";
    priceColumn.DefaultValue = 50;

    // Create the second, calculated, column.
    DataColumn taxColumn = new DataColumn();
    taxColumn.DataType = System.Type.GetType("System.Decimal");
    taxColumn.ColumnName = "tax";
    taxColumn.Expression = "price * 0.0862";

    // Create third column, calculated column 
    DataColumn totalColumn = new DataColumn();
    totalColumn.DataType = System.Type.GetType("System.Decimal");
    totalColumn.ColumnName = "total";
    totalColumn.Expression = "price + tax";

    // Add columns to DataTable.

    DataRow row = table.NewRow();
    DataView view = new DataView(table);
    dataGrid1.DataSource = view;
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900