English 中文(简体)
• 如何让Bolean Column Editable(asp.net VB gridView填满的有数据可查的,这已使Loulean Column )?
原标题: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 ?

最佳回答

你可以这样做。

这是按设计的;GridView中的行文不因违约而被篡改。

你可以采取两种方式解决这一问题:

  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

  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)

这一检查箱将得以安装,但你需要做工作,以反映数据库的任何变化。 只要你能够找到一个数据库钥匙,就直截了当,因为你需要操作一个<代码>。 UPDATE statement at some point and You wish toîd it on the right row! 在这方面,你可以采取两种方式:

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

或者,你可以补充隐藏实地控制的关键:

<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

希望这将有助于你们。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签