title: Looping through a specific column and then assigning a value to a button
body: <p>I'm working on a school assignment where I import data into a <code>DataGridView</code> in <code>WinForm</code> from a <code>CSV</code> 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 &quot;Input&quot; on the same row, in column 5, the button needs to be &quot;Input&quot;, the rest of the cells in that row after column 5 need to be readonly textboxes.</p>
  <p>If the value from column 3 is &quot;Output&quot; on the same row, in column 5, the button needs to be &quot;On&quot;, in column 6 it needs to be &quot;Off&quot; and in column 8 it needs to be &quot;On #s&quot;</p>
  <p>I've tried:</p>
  <p>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)</p>
  <p><a href="https://i.stack.imgur.com/1ofNI.png" rel="nofollow noreferrer">Here</a> is a preview of the CSV file that needs to be used.</p>
  <p>This is the code I've written so far (thats working):</p>
  <pre class="lang-cs prettyprint-override"><code>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 = &quot;Functie&quot;;
          btnInput.Text = &quot;Input&quot;;
          btnInput.Name = &quot;colInput&quot;;
          btnInput.UseColumnTextForButtonValue = true;
  
          DataGridViewButtonColumn btnOff = new DataGridViewButtonColumn();
          dataGridView1.Columns.Add(btnOff);
          btnOff.HeaderText = &quot;&quot;;
          btnOff.Text = &quot;Off&quot;;
          btnOff.Name = &quot;colInput&quot;;
          btnOff.UseColumnTextForButtonValue = true;
  
          DataGridViewTextBoxColumn txtAantalSeconden = new DataGridViewTextBoxColumn();
          dataGridView1.Columns.Add(txtAantalSeconden);
          txtAantalSeconden.HeaderText = &quot;&quot;;
          txtAantalSeconden.Name = &quot;txtAantalSec&quot;;
          txtAantalSeconden.ReadOnly = false;
  
          DataGridViewButtonColumn btnOnSec = new DataGridViewButtonColumn();
          dataGridView1.Columns.Add(btnOnSec);
          btnOnSec.HeaderText = &quot;&quot;;
          btnOnSec.Text = &quot;Aantal seconden&quot;;
          btnOnSec.Name = &quot;colInput&quot;;
          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,
              &quot;Fout&quot;,
              MessageBoxButtons.OK,
              MessageBoxIcon.Warning
              );
      }
  }
  
  private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
  {
      Console.WriteLine(e.ColumnIndex);
      Console.WriteLine(e.RowIndex);
      if (e.ColumnIndex == 3 &amp;&amp; e.RowIndex &gt;= 0)
      {
          if (dataGridView1[e.ColumnIndex, e.RowIndex].ToString() == &quot;Input&quot;)
          {
              dataGridView1[5, e.RowIndex].Value = &quot;Input&quot;;
              Console.WriteLine(e.ColumnIndex);
              Console.WriteLine(e.RowIndex);
          }
      }
  }
  </code></pre>