protected void Button1_Click(object sender, EventArgs e)
    {
       
        foreach (GridViewRow gr in GV.Rows)
        {
            int id = Convert.ToInt32(gr.Cells[5].Text);
            product pro = new product();
            pro.id = id;
            CheckBox ck=(CheckBox )gr.Cells [6].FindControl ("itemchck");
            if (ck.Checked)
            {
                                 product.Delete(pro);
            }
        }
        BindGridView();   
    }
以上你们的都不能用啊 。,我这样取出来了,可是如果我的gridview里把iD隐藏了,怎么得到呢也就是说,int id =如何获取

解决方案 »

  1.   

    gridview中visible=false就取不到值了,除非把gridview的DataKeyNames属性设置为要取的字段名
      

  2.   

    把datakey设成ID
    取的时候gv.datakeys[i]
      

  3.   

    在DataKeyNames里设置ID
    用Gv.DataKeys[e.RowIndex].Value取
      

  4.   

    放checkbox的value里面,、不要放 gr.Cells[5] 里面。,
    我的2个方法都可以啊。、
      

  5.   


    绑定时 给 CheckBox  添加一个属性
            void GridViewCheckBox_DataBinding(object sender, EventArgs e)
            {
                CheckBox checkBox = (CheckBox)sender;
                GridViewRow container = (GridViewRow)checkBox.NamingContainer;
                myType dataobj = (myType)container.DataItem;
                checkBox.InputAttributes.Add("ID", myType.getId().ToString());
            }取时用此属性
        public List<string> GetSelectRowID()
        {
            List<string> allDeleteID = new List<string>();
            foreach (GridViewRow Item in GridViewData.Rows)
            {
                CheckBox checkBox = (CheckBox)Item.FindControl("GridViewCheckBox");            if (checkBox != null)
                {
                    if (checkBox.Checked)
                    {
                        allDeleteID.Add(checkBox.InputAttributes["ID"].ToString());
                    }
                }
            }        return allDeleteID;
        }
      

  6.   

    这有这么复杂吗?以Northwind的Products表为例,给GridView加个CheckBox模板列。点击Button1时显示被选择的id, 而且把ID列隐藏。
    复制、粘贴试一下吧。
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>无标题页</title>
        <style type="text/css">
            .hide { display: none; }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                DataKeyNames="ProductID" DataSourceID="SqlDataSource1">
                <Columns>
                    <asp:BoundField DataField="ProductID" HeaderText="ProductID" 
                        InsertVisible="False" ReadOnly="True" SortExpression="ProductID" >
                        <ControlStyle CssClass="hide" />
                        <FooterStyle CssClass="hide" />
                        <HeaderStyle CssClass="hide" />
                        <ItemStyle CssClass="hide" />
                    </asp:BoundField>
                    <asp:BoundField DataField="ProductName" HeaderText="ProductName" 
                        SortExpression="ProductName" />
                    <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" 
                        SortExpression="UnitPrice" />
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
                SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice] FROM [Products]">
            </asp:SqlDataSource>
            
        </div>
        </form>
    </body>
    </html>protected void Button1_Click(object sender, EventArgs e)
        {
            System.Collections.Generic.List<int> ids = new System.Collections.Generic.List<int>();
            foreach (GridViewRow gvr in GridView1.Rows)
            {
                CheckBox cb = (CheckBox)gvr.FindControl("CheckBox1");
                if (cb.Checked)
                    ids.Add(Convert.ToInt32(GridView1.DataKeys[gvr.RowIndex].Value));
            }        Literal msg = new Literal();
            string s = string.Empty;
            foreach (int id in ids)
            {
                s += id.ToString() + ",";
            }
            msg.Text = "<script>alert('" + s + "');</script>";
            this.Controls.Add(msg);
        }
      

  7.   

    索引超出范围。必须为非负值并小于集合大小。
    参数名: index 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。
    参数名: index源错误: 
    行 140:        foreach (GridViewRow gr in GV.Rows)
    行 141:        {
    行 142:            int id = Convert.ToInt32(GV.DataKeys [gr.RowIndex ].Value );
    行 143:            product pro = new product();
    行 144:            pro.id = id;
     源文件: d:\Backup\test\CartView.aspx.cs    行: 142 堆栈跟踪: 
    [ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。
    参数名: index] 
      

  8.   

    <asp:BoundField DataField="ProductID" HeaderText="ProductID" 
                        InsertVisible="False" ReadOnly="True" SortExpression="ProductID" >
                        <ControlStyle CssClass="hide" />
                        <FooterStyle CssClass="hide" />
                        <HeaderStyle CssClass="hide" />
                        <ItemStyle CssClass="hide" />
                    </asp:BoundField>这个为什么我的出不来呢。报错
      

  9.   

    装了Northwind了吧?真的复制粘贴运行过吗?<style type="text/css">
            .hide { display: none; }
    </style>
    这一段加了吗?我是劝你真的复制粘贴,然后用Northwind试一下。然后再对比。
      

  10.   

    AutoGenerateColumns="False" 
    DataKeyNames="ProductID"等等这些设置未设置可能都会引发你说的错误。所以还是劝你先Northwind一下:-),然后再说别的。