联合主键的情况怎么在DataGrid中利用DataKeys定位记录?

比如表中三个字段
key1 int,key2 int,item varchar(50)
前面2个字段联合主键

前台代码:

< asp:DataGrid  id ="DataGrid1"  runat ="server"  AutoGenerateColumns ="False"  DataKeyField ="DoubleKey" >
                
< Columns >
                    
< asp:BoundColumn  DataField ="item"  HeaderText ="item" > asp:BoundColumn >
                    
< asp:ButtonColumn  CommandName ="del"  Text ="删除" > asp:ButtonColumn >
                
Columns >
            
asp:DataGrid >


后台代码:

using  System;
using  System.Collections;
using  System.ComponentModel;
using  System.Data;
using  System.Data.SqlClient;
using  System.Drawing;
using  System.Web;
using  System.Web.SessionState;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.HtmlControls;

namespace  csdn2
{
    
public class WebForm20 : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.DataGrid DataGrid1;
    
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
if(!IsPostBack)
            
{
                SetBind();
            }

        }

        
private void SetBind()
        
{
            SqlConnection conn
=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
            SqlDataAdapter da
=new SqlDataAdapter("select * from doublekey",conn);
            DataSet ds
=new DataSet();
            da.Fill(ds);
            DataTable dt
=ds.Tables[0];
            DataColumn dc
=new DataColumn("DoubleKey",typeof(string),"key1+'#'+key2");
            dt.Columns.Add(dc);
            
this.DataGrid1.DataSource=dt;
            
this.DataGrid1.DataBind();
        }

        
Web 窗体设计器生成的代码

        
private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        
{
            
if(e.CommandName=="del")
            
{
                SqlConnection conn
=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]); 
                SqlCommand comm
=new SqlCommand("delete from doublekey where key1=@key1 and key2=@key2",conn); 
                SqlParameter parm1
=new SqlParameter("@key1",SqlDbType.Int);
                SqlParameter parm2
=new SqlParameter("@key2",SqlDbType.Int); 
                
string [] keys=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString().Split('#');
                parm1.Value
=keys[0];
                parm2.Value
=keys[1];
                comm.Parameters.Add(parm1);
                comm.Parameters.Add(parm2); 
                conn.Open(); 
                comm.ExecuteNonQuery(); 
                conn.Close(); 
                SetBind(); 
            }

        }

    }

}

posted on 2005-04-19 11:58 lovecherry 阅读(1311) 评论(3)   编辑  收藏 收藏至365Key 所属分类: webform

private void DataGrid_User_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
String deleteCmd = "delete from users where userid = @userid";
SqlCommand myCommand = new SqlCommand(deleteCmd, new SqlConnection(ClientProxy.GetDataBaseConn ()));
myCommand.Parameters.Add(new SqlParameter("@userid", SqlDbType.NVarChar,63));
myCommand.Parameters["@userid"].Value = DataGrid_User.DataKeys[e.Item.ItemIndex];
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
BindGrid();
}

#region 创建项时被激发
///
/// 功 能:
/// 完 成 人:雷霖
///

///
///
private void DataGrid_User_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.AlternatingItem)||(e.Item.ItemType == ListItemType.EditItem)||(e.Item.ItemType == ListItemType.Item))
{
Control delControl = e.Item.FindControl("btndel");

Button delbtn = null;
if (delControl is Button)
{
delbtn = (Button)delControl;
}
delbtn.Text = "删除这个用户";
delbtn.Attributes.Add("onclick","return confirm('您真的要删除用户: "+DataGrid_User .DataKeys[(int)e.Item.ItemIndex] +" 吗?');");
}
}
#endregion



#region
private void DataGrid_User_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if (e.CommandSource is Button)
{
Button btn = (Button)e.CommandSource;
if (btn.ID == "btndel")
{
DataGrid_User_DeleteCommand(source,e);
}
}
}
#endregion
  
private void DataGrid_User_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
String deleteCmd = "delete from users where userid = @userid";
SqlCommand myCommand = new SqlCommand(deleteCmd, new SqlConnection(ClientProxy.GetDataBaseConn ()));
myCommand.Parameters.Add(new SqlParameter("@userid", SqlDbType.NVarChar,63));
myCommand.Parameters["@userid"].Value = DataGrid_User.DataKeys[e.Item.ItemIndex];
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
BindGrid();
}

#region 创建项时被激发
///
/// 功 能:
/// 完 成 人:雷霖
///

///
///
private void DataGrid_User_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.AlternatingItem)||(e.Item.ItemType == ListItemType.EditItem)||(e.Item.ItemType == ListItemType.Item))
{
Control delControl = e.Item.FindControl("btndel");

Button delbtn = null;
if (delControl is Button)
{
delbtn = (Button)delControl;
}
delbtn.Text = "删除这个用户";
delbtn.Attributes.Add("onclick","return confirm('您真的要删除用户: "+DataGrid_User .DataKeys[(int)e.Item.ItemIndex] +" 吗?');");
}
}
#endregion



#region
private void DataGrid_User_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if (e.CommandSource is Button)
{
Button btn = (Button)e.CommandSource;
if (btn.ID == "btndel")
{
DataGrid_User_DeleteCommand(source,e);
}
}
}
#endregion
  

你可能感兴趣的:(ASP.NET,Web开发)