📄 datagridedit.aspx.cs
字号:
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 Chapter_09
{
/// <summary>
/// Summary description for DataGridEdit.
/// </summary>
public class DataGridEdit : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid MyGrid;
protected System.Data.SqlClient.SqlDataAdapter PubAdapter;
protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
protected System.Data.SqlClient.SqlCommand sqlInsertCommand1;
protected System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
protected System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
protected System.Data.SqlClient.SqlConnection PubsConn;
protected Chapter_09.PublisherDataSet publisherDataSet1;
private void Page_Load(object sender, System.EventArgs e)
{
PubAdapter.Fill(publisherDataSet1);
if (!IsPostBack)
{
MyGrid.DataSource = publisherDataSet1.publishers.DefaultView;
MyGrid.DataBind();
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.PubAdapter = new System.Data.SqlClient.SqlDataAdapter();
this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand();
this.PubsConn = new System.Data.SqlClient.SqlConnection();
this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand();
this.publisherDataSet1 = new Chapter_09.PublisherDataSet();
((System.ComponentModel.ISupportInitialize)(this.publisherDataSet1)).BeginInit();
this.MyGrid.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.MyGrid_CancelCommand);
this.MyGrid.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.MyGrid_EditCommand);
this.MyGrid.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.MyGrid_SortCommand);
this.MyGrid.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.MyGrid_UpdateCommand);
this.MyGrid.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.MyGrid_DeleteCommand);
//
// PubAdapter
//
this.PubAdapter.DeleteCommand = this.sqlDeleteCommand1;
this.PubAdapter.InsertCommand = this.sqlInsertCommand1;
this.PubAdapter.SelectCommand = this.sqlSelectCommand1;
this.PubAdapter.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "publishers", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("pub_id", "pub_id"),
new System.Data.Common.DataColumnMapping("pub_name", "pub_name"),
new System.Data.Common.DataColumnMapping("city", "city"),
new System.Data.Common.DataColumnMapping("state", "state"),
new System.Data.Common.DataColumnMapping("country", "country")})});
this.PubAdapter.UpdateCommand = this.sqlUpdateCommand1;
//
// sqlDeleteCommand1
//
this.sqlDeleteCommand1.CommandText = @"DELETE FROM dbo.publishers WHERE (pub_id = @Original_pub_id) AND (city = @Original_city OR @Original_city IS NULL AND city IS NULL) AND (country = @Original_country OR @Original_country IS NULL AND country IS NULL) AND (pub_name = @Original_pub_name OR @Original_pub_name IS NULL AND pub_name IS NULL) AND (state = @Original_state OR @Original_state IS NULL AND state IS NULL)";
this.sqlDeleteCommand1.Connection = this.PubsConn;
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_pub_id", System.Data.SqlDbType.NVarChar, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "pub_id", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_city", System.Data.SqlDbType.NVarChar, 20, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "city", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_country", System.Data.SqlDbType.NVarChar, 30, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "country", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_pub_name", System.Data.SqlDbType.NVarChar, 40, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "pub_name", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_state", System.Data.SqlDbType.NVarChar, 2, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "state", System.Data.DataRowVersion.Original, null));
//
// PubsConn
//
this.PubsConn.ConnectionString = "workstation id=GEI_XP1;packet size=4096;integrated security=SSPI;data source=\"(lo" +
"cal)\\VSdotNET\";persist security info=False;initial catalog=pubs";
//
// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = "INSERT INTO dbo.publishers(pub_id, pub_name, city, state, country) VALUES (@pub_i" +
"d, @pub_name, @city, @state, @country); SELECT pub_id, pub_name, city, state, co" +
"untry FROM dbo.publishers WHERE (pub_id = @pub_id)";
this.sqlInsertCommand1.Connection = this.PubsConn;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@pub_id", System.Data.SqlDbType.NVarChar, 4, "pub_id"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@pub_name", System.Data.SqlDbType.NVarChar, 40, "pub_name"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@city", System.Data.SqlDbType.NVarChar, 20, "city"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@state", System.Data.SqlDbType.NVarChar, 2, "state"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@country", System.Data.SqlDbType.NVarChar, 30, "country"));
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT pub_id, pub_name, city, state, country FROM dbo.publishers";
this.sqlSelectCommand1.Connection = this.PubsConn;
//
// sqlUpdateCommand1
//
this.sqlUpdateCommand1.CommandText = @"UPDATE dbo.publishers SET pub_id = @pub_id, pub_name = @pub_name, city = @city, state = @state, country = @country WHERE (pub_id = @Original_pub_id) AND (city = @Original_city OR @Original_city IS NULL AND city IS NULL) AND (country = @Original_country OR @Original_country IS NULL AND country IS NULL) AND (pub_name = @Original_pub_name OR @Original_pub_name IS NULL AND pub_name IS NULL) AND (state = @Original_state OR @Original_state IS NULL AND state IS NULL); SELECT pub_id, pub_name, city, state, country FROM dbo.publishers WHERE (pub_id = @pub_id)";
this.sqlUpdateCommand1.Connection = this.PubsConn;
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@pub_id", System.Data.SqlDbType.NVarChar, 4, "pub_id"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@pub_name", System.Data.SqlDbType.NVarChar, 40, "pub_name"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@city", System.Data.SqlDbType.NVarChar, 20, "city"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@state", System.Data.SqlDbType.NVarChar, 2, "state"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@country", System.Data.SqlDbType.NVarChar, 30, "country"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_pub_id", System.Data.SqlDbType.NVarChar, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "pub_id", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_city", System.Data.SqlDbType.NVarChar, 20, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "city", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_country", System.Data.SqlDbType.NVarChar, 30, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "country", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_pub_name", System.Data.SqlDbType.NVarChar, 40, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "pub_name", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_state", System.Data.SqlDbType.NVarChar, 2, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "state", System.Data.DataRowVersion.Original, null));
//
// publisherDataSet1
//
this.publisherDataSet1.DataSetName = "PublisherDataSet";
this.publisherDataSet1.Locale = new System.Globalization.CultureInfo("en-US");
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)(this.publisherDataSet1)).EndInit();
}
#endregion
private void MyGrid_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
DataView SortView = publisherDataSet1.publishers.DefaultView;
SortView.Sort = e.SortExpression;
MyGrid.DataSource = SortView;
MyGrid.DataKeyField = "pub_id";
MyGrid.DataBind();
}
private void MyGrid_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
MyGrid.DataSource = publisherDataSet1.publishers.DefaultView;
MyGrid.DataKeyField = "pub_id";
MyGrid.EditItemIndex = e.Item.ItemIndex;
MyGrid.DataBind();
}
private void MyGrid_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
MyGrid.DataSource = publisherDataSet1.publishers.DefaultView;
MyGrid.DataKeyField = "pub_id";
MyGrid.EditItemIndex = -1;
MyGrid.DataBind();
}
private void MyGrid_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
TextBox m_pub_name, m_city, m_state, m_country;
m_pub_name = (TextBox)e.Item.Cells[3].Controls[0];
m_city = (TextBox)e.Item.Cells[4].Controls[0];
m_state = (TextBox)e.Item.Cells[5].Controls[0];
m_country = (TextBox)e.Item.Cells[6].Controls[0];
PublisherDataSet.publishersDataTable pubTable = publisherDataSet1.publishers;
PublisherDataSet.publishersRow rowToUpdate = (PublisherDataSet.publishersRow)pubTable.Rows[e.Item.ItemIndex];
rowToUpdate.pub_name = m_pub_name.Text;
rowToUpdate.city = m_city.Text;
rowToUpdate.state = m_state.Text;
rowToUpdate.country = m_country.Text;
PubAdapter.Update(publisherDataSet1);
MyGrid.DataSource = publisherDataSet1.publishers.DefaultView;
MyGrid.EditItemIndex = -1;
MyGrid.DataBind();
}
private void MyGrid_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
publisherDataSet1.publishers.Rows[e.Item.ItemIndex].Delete();
PubAdapter.Update(publisherDataSet1);
MyGrid.DataSource = publisherDataSet1.publishers.DefaultView;
MyGrid.EditItemIndex = -1;
MyGrid.DataBind();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -