(Forum answer bit) How do you use DataKeys to locate records in a DataGrid with a combination of primary keys?

Source: Internet
Author: User
Datagrid

Like three fields in a table
Key1 int,key2 Int,item varchar (50)
Front 2 Fields Union primary key

Foreground code:

<asp:datagrid id= "DATAGRID1" runat= "Server" autogeneratecolumns= "False" datakeyfield= "Doublekey" >
<Columns>
<asp:boundcolumn datafield= "Item" headertext= "Item" ></asp:BoundColumn>
<asp:buttoncolumn commandname= "del" text= "delete" ></asp:ButtonColumn>
</Columns>
</asp:DataGrid>


Background code:

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 Forms Designer generated Code #region Web Forms Designer generated code
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

/**////<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This. Datagrid1.itemcommand + = new System.Web.UI.WebControls.DataGridCommandEventHandler (this. Datagrid1_itemcommand);
This. Load + = new System.EventHandler (this. Page_Load);

}
#endregion

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 ();
}
}
}
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.