简单的ASP.NET问题。各位帮忙啊。急等啊,帮鹧鸪菜菜了。(100分)

  • 主题发起人 蓝叶菱
  • 开始时间

蓝叶菱

Unregistered / Unconfirmed
GUEST, unregistred user!
ASP.NET关于GridView的删除问题,我打算使用SQL 进行删除,但是怎么取得当前的ID啊?
public partial class adminuserview : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack){
BindGrid();
}
}
private void BindGrid()
{
string connstr = System.Configuration.ConfigurationSettings.AppSettings["xaHome"];
SqlConnection conn = new SqlConnection(connstr);
//设置适配器
SqlDataAdapter da = new SqlDataAdapter("select * from k_user",conn);
//创建客户端DataSet
DataSet ds = new DataSet();
try
{
da.Fill(ds,"mtable");
//开始绑定,根据内容表设置数据源,并且绑定
GridView1.DataSource=ds;
GridView1.DataBind();
}
catch(Exception error){
Response.Write(error.ToString());
}

}
[red] protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//----------这里不知道怎么写了!!!!!!!!!!!!!!!!!!
string connstr = System.Configuration.ConfigurationSettings.AppSettings["xaHome"];
SqlConnection conn = new SqlConnection(connstr);
//---
String logText = "";
// Append the values of the key fields to the log text.
foreach (DictionaryEntry keyEntry in e.Keys)
{
logText += keyEntry.Key + "="
+ keyEntry.Value + ";";
}
// Append the values of the non-key fields to the log text.
foreach (DictionaryEntry valueEntry in e.Values)
{
logText += valueEntry.Key + "="
+ valueEntry.Value + ";";
}
// Display the log content.

Label1.Text = logText;
/*
SqlCommand qdml = new SqlCommand();
qdml.Connection = conn;
qdml.CommandType = CommandType.Text;
qdml.CommandText=String.Format("delete from k_user where id={0}",GridView1.
*/
}
}[/red]
 
我打算使用SQL 进行删除,但是怎么取得当前的ID啊?
主要得不到当前记录的ID,是e.item吗?我找不到啊。。
 
e.Item.Cells[0].Text
 
你用的是 web2.0 呀
我怎么没有记得有这个 gridView 呀
 
e并没有item只有key,value什么的....
我用的是.NET 2.0。。。。Web Develpor.2005
2005只有gridView
 
那我就不知道了,只会 1。1 ,你看看帮助呀
 
帮我看看了,谢谢,是否愿意结交鹧鸪菜鸟呢?
QQ"360446276
 
try
{
da.Fill(ds,"mtable");
//开始绑定,根据内容表设置数据源,并且绑定
GridView1.DataSource=ds;
///此处可以设
GridView1.DataKeyNames=KeyField;
///这样可以取得Key
this.GridView1.DataKeys[e.RowIndex].Value.ToString();
GridView1.DataBind();
}
catch(Exception error){
Response.Write(error.ToString());
 
Supermay, 我的QQ在上面。
QQ"360446276
 
多人接受答案了。
 
绑定时设置主健:DataGrid1.DataKeyFied="name";
删除时再取得主健:string str=this.DataGrid1.DataKeys(e.Item.ItemIndex);
次代码在1.0上通过 但是DataView和DataGri非常相似 原理也差不多
 
顶部