2013-04-08 57 views
0

我有一个asp.net表单,使用网格视图显示来自Oracle数据库的用户详细信息。我甚至管理使用以下模板来获得在网格视图中的复选框场..如何使用gridview中的复选框?

<asp:TemplateField> 
    <ItemTemplate> 
     <asp:CheckBox runat="server" ID="chck"/> 
    </ItemTemplate> 
    </asp:TemplateField> 

我希望管理员能使用的复选框以选择多个条目,并执行某些动作他们。我试过使用下面的删除操作:

for (int i = 0; i < GridView1.Rows.Count; i++) 
     { 
      if (Convert.ToBoolean(GridView1.Rows[i].Cells[1].Value == true)) 
       GridView1.Rows.RemoveAt[i]; 
     } 

但是,这会显示一个错误。显然,每一行上的每个复选框都必须有自己唯一的索引。我如何得到它? 任何形式的帮助将不胜感激。感谢名单:)

+1

什么是错误?你为什么要将'GridView1.Rows [i] .Cells [1] .Value'作为'boolean'进行比较?你确定吗? – 2013-04-08 11:07:17

回答

0

你需要使用的FindControl

for (int i = 0; i < GridView1.Rows.Count; i++) 
{ 
    CheckBox cb1 = (CheckBox)GridView1.Rows[i].FindControls("chck"); 
     if(cb1.Checked) 
      GridView1.Rows.RemoveAt[i]; 
} 
+0

@sonergonul感谢您的编辑。 – 2013-04-08 11:16:34

0

使用查找控制得到复选框来找到你的GridView行内部控制:

for (int i = 0; i < GridView1.Rows.Count; i++) 
    { 

     CheckBox chck = (CheckBox)GridView1.Rows[i].FindControl("chck"); 

     if (chck != null) 
     { 
     if (chck.Checked) 
     { 
      GridView1.Rows.RemoveAt[i]; 
     } 
     } 
    } 
+0

我已经添加了gridview部分的复选框,谢谢。我面临的唯一问题是,如何检索已检查行的主键并使用它们删除记录? – Chopsy 2013-04-09 12:13:47

+0

您可以添加一个绑定了主键值的标签,并以与复选框相同的方式获取该标签值。 – 2013-04-10 04:20:12

0

这是ASPX文件:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    BackColor="White" BorderColor="#336699" BorderStyle="Solid" BorderWidth="1px" 
    CellPadding="0" CellSpacing="0" DataKeyNames="CategoryID" Font-Size="10" 
    Font-Names="Arial" GridLines="Vertical" Width="40%"> 

      <Columns>    
       <asp:TemplateField> 
        <ItemTemplate> 
         <asp:CheckBox ID="chkStatus" runat="server" 
          AutoPostBack="true" OnCheckedChanged="chkStatus_OnCheckedChanged" 
          Checked='<%# Convert.ToBoolean(Eval("Approved")) %>' 
          Text='<%# Eval("Approved").ToString().Equals("True") ? " Approved " : " Not Approved " %>' /> 
        </ItemTemplate>     
       </asp:TemplateField> 

       <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />     
       <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" /> 
      </Columns> 

    <HeaderStyle BackColor="#336699" ForeColor="White" Height="20" /> 

</asp:GridView> 

这是CS文件:

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!Page.IsPostBack) 
    { 
    LoadData(); 
    } 
} 

private void LoadData() 
{ 
    string constr = @"Server=.\SQLEXPRESS;Database=TestDB;uid=waqas;pwd=sql;"; 
    string query = @"SELECT CategoryID, CategoryName, Approved FROM Categories"; 

    SqlDataAdapter da = new SqlDataAdapter(query, constr); 
    DataTable table = new DataTable(); 
    da.Fill(table); 

    GridView1.DataSource = table; 
    GridView1.DataBind(); 
} 

public void chkStatus_OnCheckedChanged(object sender, EventArgs e) 
{ 
    CheckBox chkStatus = (CheckBox)sender; 
    GridViewRow row = (GridViewRow)chkStatus.NamingContainer; 


    string cid = row.Cells[1].Text; 
    bool status = chkStatus.Checked; 


    string constr = @"Server=.\SQLEXPRESS;Database=TestDB;uid=waqas;pwd=sql;"; 
    string query = "UPDATE Categories SET Approved = @Approved WHERE CategoryID = @CategoryID"; 

    SqlConnection con = new SqlConnection(constr); 
    SqlCommand com = new SqlCommand(query, con); 


    com.Parameters.Add("@Approved", SqlDbType.Bit).Value = status; 
    com.Parameters.Add("@CategoryID", SqlDbType.Int).Value = cid; 


    con.Open(); 
    com.ExecuteNonQuery(); 
    con.Close(); 

    LoadData(); 
}