Khaleel Hmoz
Khaleel Hmoz

Reputation: 995

How to Make Boolean Column Editable (asp.net VB gridView filled by DataTable that has Boolean Column) ?

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 ?

Upvotes: 0

Views: 3611

Answers (1)

Glory Raj
Glory Raj

Reputation: 17701

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:

  1. Add an Edit link
    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.
    alt text
    (source: philippursglove.com)

alt text

  1. 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>

alt text
(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 UPDATE statement 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 your CheckedChanged event handler, you need to find out which row you are in and look that up in the DataKeys array.

   Protected Sub DiscontinuedCheckBox_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
    Dim DiscontinuedCheckBox As CheckBox
    Dim conn As SqlConnection
    Dim cmd As SqlCommand
    Dim productId As Integer
    Dim selectedRow As GridViewRow
    ' Cast the sender object to a CheckBox
    DiscontinuedCheckBox = CType(sender,CheckBox)
    ' We can find the row we clicked the checkbox in by walking up the control tree
    selectedRow = CType(DiscontinuedCheckBox.Parent.Parent,GridViewRow)
    ' GridViewRow has a DataItemIndex property which we can use to look up the DataKeys array
    productId = CType(ProductGridView.DataKeys(selectedRow.DataItemIndex).Value,Integer)
    conn = New SqlConnection(ProductDataSource.ConnectionString)
    cmd = New SqlCommand
    cmd.Connection = conn
    cmd.CommandType = CommandType.Text
    If DiscontinuedCheckBox.Checked Then
        cmd.CommandText = ("UPDATE Products SET Discontinued = 1 WHERE ProductId = " + ProductId.ToString)
    Else
        cmd.CommandText = ("UPDATE Products SET Discontinued = 0 WHERE ProductId = " + ProductId.ToString)
    End If
    conn.Open
    cmd.ExecuteNonQuery
    conn.Close
End Sub

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>

     Protected Sub DiscontinuedCheckBox_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
    Dim DiscontinuedCheckBox As CheckBox
    Dim ProductIdHiddenField As HiddenField
    DiscontinuedCheckBox = CType(sender,CheckBox)
    ProductIdHiddenField = CType(DiscontinuedCheckBox.Parent.FindControl("ProductIdHiddenField"),HiddenField)
    conn = New SqlConnection(ProductDataSource.ConnectionString)
 ..................
    If DiscontinuedCheckBox.Checked Then
        cmd.CommandText = ("UPDATE Products SET Discontinued = 1 WHERE ProductId = " + ProductIdHiddenField.Value)
    End If
...............
End Sub

i hope it will helps you....

Upvotes: 1

Related Questions