After Filling a DataTable in GridView’s DataSource . A column with check box Type appears but it created as read only column
and I can’t enable it or make it editable… even i tried .readonly = false
and still can’t be edited
can any one help please ?
After Filling a DataTable in GridView’s DataSource . A column with check box Type
Share
you can try like this..
This is by design; rows in a GridView are not editable by default.
There’s two ways you might address this:
In your GridView tag, add
AutoGenerateEditButton="True". 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.(source: philippursglove.com)
Add a TemplateField with a CheckBox inside it
Inside the
<columns>tag, you can add TemplateFields that you set the databinding up for yourself e.g.<asp:TemplateField HeaderText="Discontinued"><ItemTemplate>
<asp:CheckBox runat="server" ID="DiscontinuedCheckBox" Checked="<%# Eval("Discontinued") %>" AutoPostback="true" OnCheckedChanged="DiscontinuedCheckBox_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
(source: philippursglove.com)
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
UPDATEstatement at some point and you want to run it on the right row! Here’s two ways you could do this:In your Gridview tag, add
DataKeyNames="MyDatabasePrimaryKey". Then in yourCheckedChangedevent handler, you need to find out which row you are in and look that up in theDataKeysarray.Or, you could add the key in a HiddenField control:
<asp:TemplateField HeaderText="Discontinued"><ItemTemplate>
<asp:hiddenfield runat="server" id="ProductIdHiddenField" Value='<%# Eval("ProductID") %>' />
<asp:CheckBox runat="server" ID="DiscontinuedCheckBox" Checked="<%# Eval("Discontinued") %>" AutoPostback="true" OnCheckedChanged="DiscontinuedCheckBox_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
i hope it will helps you….