string MySelectQuery = "select * from userinfo";
            SqlDataAdapter MyDataAdapter = new SqlDataAdapter(MySelectQuery, Conn.MyConnection);         
            DataSet ds = new DataSet();
            MyDataAdapter.Fill(ds,"UserInfo");
            Conn.Close();            GridViewList.DataSource = ds.Tables["UserInfo"].DefaultView;
            GridViewList.DataBind();
userinfo表中有个U_id字段,
在删除的时候,我想通过U_id来判断要删除的行,
但是我取不到U_id的值,
把Visible="false"设为"true"可以获取,但设为false就不行了.
请教如何解决?
<asp:BoundField HeaderText="ID" Visible="false" DataField="u_id" />
        <asp:HyperLinkField HeaderText="姓名" DataTextField="u_name" DataNavigateUrlFields="u_id" DataNavigateUrlFormatString="viewDetail.aspx?id={0}" Target="&quot;_blank&quot;&quot;" >
            <HeaderStyle Width="100px" />
        </asp:HyperLinkField>
        <asp:BoundField HeaderText="学校" DataField="u_school" >
            <HeaderStyle Width="260px" />
        </asp:BoundField>

解决方案 »

  1.   

    不要用绑定列,用个hidden field就可以了,不用设置visble属性。
      

  2.   

    给GridView添加一个主键属性:
    <asp:GridView ID="GridViewList" runat="server" DataKeyNames="u_id">
    删除的时候根据RowIndex获取该行主键
    GridViewList.DataKeys[RowIndex].Value.ToString()
      

  3.   

    在代碼中作刪除操作之前先把這列的visible屬性設為true,
    刪除后再設為false
      

  4.   

    设置  gridview 得DataKeyNames 为 主键字段名字
      

  5.   

    给你个demo吧!测试通过:<%@ Page Language="C#" AutoEventWireup="true" Debug="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
    <body>
        <form id="form1" runat="server">
        <div>
             <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" datakeynames="ID" >
                <Columns>
                    <asp:TemplateField>
                      <ItemTemplate>
                          <asp:LinkButton ID="select" CommandName="Delete" CommandArgument= <%# DataBinder.Eval (Container.DataItem, "ID") %> OnCommand="Delete_Command" runat="server" Text="删除"> </asp:LinkButton>
                       </ItemTemplate>
                    </asp:TemplateField>               
                    <asp:BoundField ShowHeader="true" DataField="ID" HeaderText="ID" />
                    <asp:BoundField ShowHeader="true" DataField="name" HeaderText="姓名" />
                </Columns>
            </asp:GridView> 
     
        </div>
        </form>
    </body>
    </html>
    </html>
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;public partial class Default2 : System.Web.UI.Page
    {
        public class EntityTest
        {
            private int id;
            public int ID
            {
                get
                { return this.id; }
                set
                { this.id = value; }
            }
            private string name;
            public string Name
            {
                get { return this.name; }
                set { this.name = value; }
            }
            public EntityTest(int id, string name)
            {
                this.ID = id;
                this.Name = name;
            }
        }
        public List<EntityTest> sourceList = new List<EntityTest> { new EntityTest(1, "aaa"), new EntityTest(2, "bbb"), new EntityTest(3, "ccc"), new EntityTest(4, "ddd") };
        
      
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.GridView1.DataSource = sourceList;
                this.GridView1.DataBind();
            }
        }
        protected void Delete_Command(object sender, CommandEventArgs e)
        {
            string id = null;
            if (e.CommandName == "Delete")
            {
                id = e.CommandArgument.ToString();
                this.sourceList.RemoveAt(int.Parse(id) - 1);
                this.GridView1.DataSource = sourceList;
                this.GridView1.DataBind();
            }    }
    }
      

  6.   

    如果楼主还想用现在的方法来做,也可以,只是你隐藏列的方法要变一下。不能用visible=false而要用css
    在页面的<head></head>中加入<style>
           .hidden { display:none;}
        </style>随后在GridView的列编辑对话框中,对需要进行隐藏的列进行设置,分别设置FootStyle,HeaderStyle,ItemStyle的CssClass属性为“hidden”
    然后你再试一下吧。