我的代码如下:我刚学着将代码和页面分开,我只是想显示出DataGrid,并能执行删除操作. using System; using System.Collections; using System.ComponentModel; using System.Data; 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 Web1 { /// <summary> /// Summary description for WebForm2. /// </summary> public class WebForm2 : System.Web.UI.Page { protected System.Web.UI.WebControls.DataGrid DataGrid1; protected System.Web.UI.WebControls.Label Label5; protected System.Web.UI.WebControls.Label Label6; protected System.Web.UI.WebControls.Label Label7; protected System.Web.UI.WebControls.Label Label8; protected System.Web.UI.WebControls.TextBox TextBox1; protected System.Web.UI.WebControls.TextBox TextBox2; protected System.Web.UI.WebControls.TextBox TextBox3; protected System.Web.UI.WebControls.TextBox TextBox4; protected System.Web.UI.WebControls.Button Button1; protected System.Web.UI.WebControls.Panel Panel1; protected System.Web.UI.WebControls.DropDownList DropDownList2; protected System.Web.UI.WebControls.DropDownList DropDownList1; protected System.Web.UI.WebControls.RangeValidator RV1; protected System.Web.UI.WebControls.Label Label9; protected System.Web.UI.WebControls.Label Label10; protected System.Web.UI.WebControls.Label Label11; protected System.Web.UI.WebControls.Label Label12; protected System.Web.UI.WebControls.TextBox TextBox5;
public WebForm2() { Page.Init += new System.EventHandler(Page_Init); } private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here if (this.IsPostBack) return; showgrid(); } private void Page_Init(object sender, EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); } #region Web Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void showgrid() { string strdsn="server=ls;uid=sa;pwd=9876;database=hdb"; System.Data.SqlClient.SqlDataAdapter objada=new System.Data.SqlClient.SqlDataAdapter(); System.Data.SqlClient.SqlConnection objconn=new System .Data.SqlClient.SqlConnection(strdsn);
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
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 Web1
{
/// <summary>
/// Summary description for WebForm2.
/// </summary>
public class WebForm2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Label Label5;
protected System.Web.UI.WebControls.Label Label6;
protected System.Web.UI.WebControls.Label Label7;
protected System.Web.UI.WebControls.Label Label8;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.WebControls.TextBox TextBox4;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Panel Panel1;
protected System.Web.UI.WebControls.DropDownList DropDownList2;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.RangeValidator RV1;
protected System.Web.UI.WebControls.Label Label9;
protected System.Web.UI.WebControls.Label Label10;
protected System.Web.UI.WebControls.Label Label11;
protected System.Web.UI.WebControls.Label Label12;
protected System.Web.UI.WebControls.TextBox TextBox5;
public WebForm2()
{
Page.Init += new System.EventHandler(Page_Init);
} private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (this.IsPostBack)
return;
showgrid();
} private void Page_Init(object sender, EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
} #region Web Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
private void showgrid()
{
string strdsn="server=ls;uid=sa;pwd=9876;database=hdb";
System.Data.SqlClient.SqlDataAdapter objada=new System.Data.SqlClient.SqlDataAdapter();
System.Data.SqlClient.SqlConnection objconn=new System .Data.SqlClient.SqlConnection(strdsn);
string strsql="select * from table1";
objada.TableMappings.Add("Table","table1");
objada.SelectCommand=new System.Data.SqlClient.SqlCommand(strsql,objconn);
System.Data.DataSet objds=new System.Data.DataSet ("table1");
objada.Fill(objds);
this.DataGrid1.DataSource=objds;
this.DataGrid1.DataBind();
} private void DataGrid1_DeleteCommand(object sender, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string strno=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString().Trim();
string strsql="DELETE * FROM table1 WHERE (bh='" +strno+ "')";
string strdsn="server=ls;uid=sa;pwd=9876;database=hdb";
System.Data.SqlClient.SqlDataAdapter objada=new System.Data.SqlClient.SqlDataAdapter();
System.Data.SqlClient.SqlConnection objconn=new System .Data.SqlClient.SqlConnection(strdsn);
System.Data.SqlClient.SqlCommand objcmd =new System.Data.SqlClient.SqlCommand(strsql,objconn);
objada.TableMappings.Add("Table","table1");
objada.SelectCommand=new System.Data.SqlClient.SqlCommand(strsql,objconn);
System.Data.DataSet objds=new System.Data.DataSet ("table1");
objada.Fill(objds);
this.DataGrid1.DataSource=objds;
this.DataGrid1.DataBind();
objcmd.Connection.Open();
objcmd.ExecuteNonQuery();
objcmd.Connection.Close();
{
if (e.CommandName.Trim()=="Delete")
{
}}
string strno=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString().Trim();
出错,说"牵引超出范围,必须为非负并小于集合大小,参数名:index"
我设置的主键是bh字段,char,3
我实在是很闹心,怎么代码和页面分开这么不易吗?有哪位能提供给我一份完整的使用DataGrid 执行删除或编辑的源代码吗?不胜感谢!