title: programmatically add a button column
body: <p>I am trying to add a <code>ButtonField</code> to a <code>GridView</code> programmatically.</p>
  
  <p>So in aspx it looks like this:</p>
  
  <pre><code>&lt;asp:ButtonField ButtonType="Image" 
                   CommandName="buttonClicked" 
                   ImageUrl="~/checkdailyinventory.bmp" /&gt;
  </code></pre>
  
  <p>This is what I have tried in C# to replicate.</p>
  
  <pre><code> GridView genGridView = new GridView();
  
          //Where the xml gets bonded to the data grind
          XmlDataSource xds = new XmlDataSource();
          xds.Data = xml;
          xds.DataBind();
          xds.EnableCaching = false;
  
  
          genGridView.DataSource = xds;
          genGridView.DataBind();
  
  
          // formating is done here
  
          ButtonField test3 = new ButtonField();
          test3.ButtonType = ButtonType.Image;
          test3.CommandName = "buttonClicked";
          test3.ImageUrl = "~/checkdailyinventory.bmp";
          genGridView.Columns.Add(test3);
  </code></pre>
  
  <p>This does not create any new columns
  Any help would be apperciated.</p>
  
  <h1>UPDATE Progress</h1>
  
  <p>I was able to create the columns, but they are the first column, not the last.  To do this I had to put the button creation and add the column before the data binding.</p>
  
  <pre><code>GridView genGridView = new GridView();
  
          //Where the xml gets bonded to the data grind
          XmlDataSource xds = new XmlDataSource();
          xds.Data = xml;
          xds.DataBind();
          xds.EnableCaching = false;
  
          //Set the rowdatabound before binding.  This will allow the correct function to be called.
          genGridView.RowDataBound += new GridViewRowEventHandler(inventoryGridView_RowDataBound);
          genGridView.RowCommand += new GridViewCommandEventHandler(inventoryGridView_RowCommand);
  
          ButtonField test3 = new ButtonField();
          test3.ButtonType = ButtonType.Image;
          test3.CommandName = "buttonClicked";
          test3.ImageUrl = "checkdailyinventory.bmp";
  
          genGridView.Columns.Add(test3);
          genGridView.DataSource = xds;
          genGridView.DataBind();
  </code></pre>
  
  <p>Also the button fails to actually do anything even though I have all the variables set correctly, but one step at a time I guess.</p>
  
  <p>Minor Edit:</p>
  
  <p>I think I figure out why the buttons aren't working.  In the html it should look like this:</p>
  
  <pre><code>&lt;td&gt;&lt;input type="image" src="checkdailyinventory.bmp" onclick="javascript:__doPostBack('inventoryGridView','buttonClicked$2')" style="border-width:0px;" /&gt;&lt;/..&gt;
  </code></pre>
  
  <p>while it actually looks like this:</p>
  
  <pre><code>&lt;td&gt;&lt;input type="image" src="checkdailyinventory.bmp" onclick="javascript:__doPostBack('ctl03','buttonClicked$2')" style="border-width:0px;" /&gt;&lt;/..&gt;
  </code></pre>
  
  <p>So I need to figure out how to replace <code>ct103</code> with <code>inventoryGridView</code></p>