Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is by design; rows in a GridView are not editable by default. </p> <p>There's two ways you might address this: </p> <h2>1. Add an Edit link</h2> <p>In your GridView tag, add <code>AutoGenerateEditButton="True"</code>. When your GridView renders in the browser, you should now find a hyperlink labelled 'Edit'. If you click it, the fields in your GridView will become editable, and the Edit link will become two links, one to save your changes to the database and the other to discard them. Using this method, all the plumbing to wire up changes in the GridView to the database can be done for you, depending on how you're doing the databinding. This example uses a SqlDataSource control.<br> <a href="http://philippursglove.com/stackoverflow/checkboxgridview1.png" rel="nofollow noreferrer">alt text http://philippursglove.com/stackoverflow/checkboxgridview1.png</a> <a href="http://philippursglove.com/stackoverflow/checkboxgridview2.png" rel="nofollow noreferrer">alt text http://philippursglove.com/stackoverflow/checkboxgridview2.png</a></p> <h2>2. Add a TemplateField with a CheckBox inside it</h2> <p>Inside the <code>&lt;columns&gt;</code> tag, you can add TemplateFields that you set the databinding up for yourself e.g.</p> <pre><code>&lt;asp:TemplateField HeaderText="Discontinued"&gt; &lt;ItemTemplate&gt; &lt;asp:CheckBox runat="server" ID="DiscontinuedCheckBox" Checked='&lt;%# Eval("Discontinued") %&gt;' AutoPostback="true" OnCheckedChanged="DiscontinuedCheckBox_CheckedChanged" /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; </code></pre> <p><a href="http://philippursglove.com/stackoverflow/checkboxgridview3.png" rel="nofollow noreferrer">alt text http://philippursglove.com/stackoverflow/checkboxgridview3.png</a></p> <p>This checkbox will be enabled, but you need to do the work yourself to reflect any changes back to the database. This is straightforward as long as you can get a database key, as you'll need to run an <code>UPDATE</code> statement at some point and you want to run it on the right row! Here's two ways you could do this:</p> <p>In your Gridview tag, add <code>DataKeyNames="MyDatabasePrimaryKey"</code>. Then in your <code>CheckedChanged</code> event handler, you need to find out which row you are in and look that up in the <code>DataKeys</code> array.</p> <pre><code>protected void DiscontinuedCheckBox_CheckedChanged(object sender, EventArgs e) { CheckBox DiscontinuedCheckBox; SqlConnection conn; SqlCommand cmd; int productId; GridViewRow selectedRow; // Cast the sender object to a CheckBox DiscontinuedCheckBox = (CheckBox)sender; // We can find the row we clicked the checkbox in by walking up the control tree selectedRow = (GridViewRow)DiscontinuedCheckBox.Parent.Parent; // GridViewRow has a DataItemIndex property which we can use to look up the DataKeys array productId = (int)ProductGridView.DataKeys[selectedRow.DataItemIndex].Value; using (conn = new SqlConnection(ProductDataSource.ConnectionString)) { cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandType = CommandType.Text; if (DiscontinuedCheckBox.Checked) { cmd.CommandText = "UPDATE Products SET Discontinued = 1 WHERE ProductId = " + ProductId.ToString(); } else { cmd.CommandText = "UPDATE Products SET Discontinued = 0 WHERE ProductId = " + ProductId.ToString(); } conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } } </code></pre> <p>Or, you could add the key in a HiddenField control:</p> <pre><code>&lt;asp:TemplateField HeaderText="Discontinued"&gt; &lt;ItemTemplate&gt; &lt;asp:hiddenfield runat="server" id="ProductIdHiddenField" Value='&lt;%# Eval("ProductID") %&gt;' /&gt; &lt;asp:CheckBox runat="server" ID="DiscontinuedCheckBox" Checked='&lt;%# Eval("Discontinued") %&gt;' AutoPostback="true" OnCheckedChanged="DiscontinuedCheckBox_CheckedChanged" /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; </code></pre> <p>Code:</p> <pre><code>protected void DiscontinuedCheckBox_CheckedChanged(object sender, EventArgs e) { CheckBox DiscontinuedCheckBox; HiddenField ProductIdHiddenField; DiscontinuedCheckBox = (CheckBox)sender; ProductIdHiddenField = (HiddenField)DiscontinuedCheckBox.Parent.FindControl("ProductIdHiddenField"); using (conn = new SqlConnection(ProductDataSource.ConnectionString)) { ... if (DiscontinuedCheckBox.Checked) { cmd.CommandText = "UPDATE Products SET Discontinued = 1 WHERE ProductId = " + ProductIdHiddenField.Value; } ... } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload