如何使布尔列可编辑(由具有布尔列的DataTable填充的asp.net VB gridView)?

时间:2011-10-27 08:25:13

标签: asp.net vb.net gridview datatable

在GridView的DataSource中填充DataTable之后。出现带有复选框类型的列,但它创建为只读列 我无法启用它或使其可编辑...即使我尝试过.readonly = false 仍然无法编辑 请帮忙吗?

1 个答案:

答案 0 :(得分:1)

你可以这样试试..

这是设计上的;默认情况下,GridView中的行不可编辑。

有两种方法可以解决这个问题:

  1. 添加编辑链接
    在GridView代码中,添加AutoGenerateEditButton="True"。当您的GridView在浏览器中呈现时,您现在应该找到标记为“编辑”的超链接。如果单击它,GridView中的字段将变为可编辑,编辑链接将变为两个链接,一个用于保存对数据库的更改,另一个用于丢弃它们。使用此方法,可以为您完成将GridView中的更改连接到数据库的所有管道,具体取决于您执行数据绑定的方式。此示例使用SqlDataSource控件 alt text http://philippursglove.com/stackoverflow/checkboxgridview1.png alt text http://philippursglove.com/stackoverflow/checkboxgridview2.png

  2. 添加一个带有CheckBox的TemplateField 在<columns>标记内,您可以添加自己设置数据绑定的TemplateFields,例如

    <asp:TemplateField HeaderText="Discontinued">
    <ItemTemplate>
    <asp:CheckBox runat="server" ID="DiscontinuedCheckBox" Checked="<%# Eval("Discontinued") %>" AutoPostback="true" OnCheckedChanged="DiscontinuedCheckBox_CheckedChanged" />
    </ItemTemplate>
    </asp:TemplateField>

  3. alt text http://philippursglove.com/stackoverflow/checkboxgridview3.png

    此复选框将启用,但您需要自己完成工作以将任何更改反映回数据库。只要您可以获取数据库密钥,这很简单,因为您需要在某个时刻运行UPDATE语句,并且您希望在右侧行运行它!这有两种方法可以做到这一点:

    在Gridview代码中,添加DataKeyNames="MyDatabasePrimaryKey"。然后在CheckedChanged事件处理程序中,您需要找出您所在的行并在DataKeys数组中查找。

       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
    

    或者,您可以在HiddenField控件中添加密钥:

    <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
    

    我希望它会帮助你......