2013-01-20 42 views
0

当用户在我的网站上使用GridView进行搜索时,我希望有一个checkbox列,他们会点击这一列,将列STATUS中的值更改为U。我有一段时间寻找可行的代码,所以我希望你们可以提供帮助。我是一个完整的NOOB,如果你知道答案,请说明性。GridView中的复选框更新

Checkbox按钮的代码 - 当前我搜索时它返回说它不能将字符串转换为boolean

   <asp:TemplateField HeaderText="Successful Contact?"> 
       <EditItemTemplate> 
        <asp:CheckBox ID="CheckBox1" runat="server" checked='<%#Bind("status")%>' AutoPostBack="true" /> 
       </EditItemTemplate> 
       <ItemTemplate> 
       <asp:CheckBox ID="CheckBox1" runat="server" checked='<%#Bind("status")%>' 
        Enabled="False" /></ItemTemplate> 
      </asp:TemplateField>  

这是VB代码

Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) 
    For Each GridViewRow In GridView1.Rows 

     Dim row As GridViewRow = GridView1.Rows(e.RowIndex) 
     Dim value As Boolean 
     value = DirectCast(GridView1.Rows(e.RowIndex).Cells(0).FindControl("CheckBox1"), CheckBox).Checked 
     Dim connectionString As String = ConfigurationManager.ConnectionStrings("Data Source=server;Initial Catalog=i3FCC01;Integrated Security=True;").ConnectionString 
     Dim com_sql As New SqlClient.SqlCommand 
     Dim insertSql As String 
     insertSql = "Update CallQueueFCC Set Status='U' where Id = @id" 
     Using myConnection As New SqlConnection(connectionString) 
      myConnection.Open() 
      Dim myCommand As New SqlCommand(insertSql, myConnection) 
      myCommand.Parameters.AddWithValue("@status", value) 
      myCommand.ExecuteNonQuery() 
      myConnection.Close() 
     End Using 
    Next 
    GridView1.EditIndex = -1 
    DataBind() 
End Sub 

回答

0

检查的属性,这个布尔值要绑定的状态值复选框。

但是,状态具有字符串值,因为您将字符串绑定到布尔值,它会引发错误。

0

和前面提到的Kiran一样,status有一个字符串值,所以你需要在行绑定事件中做一些消防。

标记

<asp:TemplateField HeaderText="Successful Contact?"> 
      <EditItemTemplate> 
       <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" /> 
      </EditItemTemplate> 
      <ItemTemplate> 
      <asp:CheckBox ID="CheckBox1" runat="server" 
       Enabled="False" /></ItemTemplate> 
     </asp:TemplateField> 

代码背后

Protected Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound 

    If e.Row.RowType = DataControlRowType.DataRow Then 
    'Evaluate the state of the check box, true when status =U , and false otherwise 
    Dim itemstatus As Boolean=If(DataBinder.Eval(e.Row.DataItem, "Status"), True, False) 
    Dim CheckBoxItemTemp As CheckBox=CType(e.Row.FindControl("CheckBox1"), CheckBox) 
    CheckBoxItemTemp.Checked=itemstatus 
    End if 


    If e.Row.RowState & DataControlRowState.Edit Then 
'same as above but now for edit item template 
    Dim editstatus As Boolean=If(DataBinder.Eval(e.Row.DataItem, "Status"), True, False) 
    Dim CheckBoxEditTemp As CheckBox=CType(e.Row.FindControl("CheckBox1"), CheckBox) 
    CheckBoxEditTemp.Checked=editstatus 
    End if 

End Sub 
+0

谢谢里面,但现在我和以前一样混乱。 Visual Studio不喜欢e.RowIndex,但我也不确定该代码如何执行任何操作,因为我没有看到它触发任何sql update命令将SQL值更新为U.将我的头发拉出来,但是我感谢你的帮助。 – user1993989

+0

我已经更新了我的答案。我的不好。它应该是e.Row来代替e.RowIndex。代码实际上做了一些事情。它清除了你正在对字符串进行布尔转换的错误。您仍然必须挂钩你在网格行更新事件中的更新逻辑,你已经完成了。让我知道如果这仍然不清楚。 –