你可以这样做。
这是按设计的;GridView中的行文不因违约而被篡改。
你可以采取两种方式解决这一问题:
- 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.
(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)
这一检查箱将得以安装,但你需要做工作,以反映数据库的任何变化。 只要你能够找到一个数据库钥匙,就直截了当,因为你需要操作一个<代码>。 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
希望这将有助于你们。