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

发布于 2024-12-12 07:20:47 字数 127 浏览 0 评论 0 原文

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

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

や莫失莫忘 2024-12-19 07:20:47

你可以这样尝试..

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

您可以通过两种方法解决此问题:

  1. 添加编辑链接
    在 GridView 标记中,添加 AutoGenerateEditButton="True"。当 GridView 在浏览器中呈现时,您现在应该会找到一个标记为“编辑”的超链接。如果单击它,GridView 中的字段将变得可编辑,并且“编辑”链接将变成两个链接,一个用于将更改保存到数据库,另一个用于放弃它们。使用此方法,可以为您完成将 GridView 中的更改连接到数据库的所有操作,具体取决于您如何进行数据绑定。此示例使用 SqlDataSource 控件。
    替代文本
    (来源:philippursglove.com

alt text

  1. 添加一个 TemplateField,其中包含一个复选框
    标记内,您可以添加您自己设置数据绑定的 TemplateFields,例如


    <项目模板>
    " AutoPostback="true" OnCheckedChanged="DiscontinuedCheckBox_CheckedChanged" />

替代文字
(来源:philippursglove.com

此复选框将被启用,但您需要自己完成工作才能将任何更改反映回数据库。只要您可以获得数据库密钥,这很简单,因为您需要在某个时刻运行 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 控件中添加键:


<项目模板>
>
" AutoPostback="true" OnCheckedChanged="DiscontinuedCheckBox_CheckedChanged" />

     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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文