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=""_blank""" >
<HeaderStyle Width="100px" />
</asp:HyperLinkField>
<asp:BoundField HeaderText="学校" DataField="u_school" >
<HeaderStyle Width="260px" />
</asp:BoundField>
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=""_blank""" >
<HeaderStyle Width="100px" />
</asp:HyperLinkField>
<asp:BoundField HeaderText="学校" DataField="u_school" >
<HeaderStyle Width="260px" />
</asp:BoundField>
<asp:GridView ID="GridViewList" runat="server" DataKeyNames="u_id">
删除的时候根据RowIndex获取该行主键
GridViewList.DataKeys[RowIndex].Value.ToString()
刪除后再設為false
<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();
} }
}
在页面的<head></head>中加入<style>
.hidden { display:none;}
</style>随后在GridView的列编辑对话框中,对需要进行隐藏的列进行设置,分别设置FootStyle,HeaderStyle,ItemStyle的CssClass属性为“hidden”
然后你再试一下吧。