title: How do I programmatically add a button to a gridview and assign it to a specific code-behind function?
body: <p>In runtime I'm creating a DataTable and using nested for-loops to populate the table. This table I later assign as DataSource to a gridview and on RowDataBound I assign the value of each cell. I want to know how I can give each cell a button and assign that button to a codebehind function. I'll have 12 buttons and each one will contain a different value. I would prefer if they all call the same function with some kind of event that stores the cell-specific value. </p>
  
  <p>This is the code where the Table gets created:</p>
  
  <pre><code>protected void GridViewDice_RowDataBound(object sender, GridViewRowEventArgs e)
  {
  
  
      DataTable diceTable = _gm.GetDice(_gameId);
      for (int i = 0; i &lt; GameRules.ColumnsOfDice; i++)
      {
          if(e.Row.RowIndex &gt; -1)
          {
              /*This is where I'd like to add the button*/
              //e.Row.Cells[i].Controls.Add(new Button);
              //e.Row.Cells[i].Controls[0].Text = specific value from below
  
              //This is where the specific value gets input
              e.Row.Cells[i].Text = diceTable.Rows[e.Row.RowIndex][i].ToString();
          }
  
      }
  }
  </code></pre>
  
  <p>I would like to handle the buttonclick with something like this:</p>
  
  <pre><code>protected void DiceButton_Click(int column, int row, int value)
  {
      //Do whatever
  }
  </code></pre>
  
  <p>Any suggestions?</p>