⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 remove-delete-rows.aspx

📁 Professional ASP.NET source code
💻 ASPX
字号:
<%@Page Language="C#"%>

<%@Import Namespace="System.Data" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<title>Removing versus Deleting Rows in a DataTable</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Removing versus Deleting Rows in a DataTable</span><hr />
<!--------------------------------------------------------------------------->

<b>The original table contents</b>:
<asp:datagrid id="dgrResult1" runat="server" /><br />
<b>The table contents after Rows(1).Delete()</b>:
<asp:datagrid id="dgrResult2" runat="server" /><br />
<b>The table contents after RejectChanges()</b>:
<asp:datagrid id="dgrResult3" runat="server" /><br />
<b>The table contents after Rows.Remove(1)</b>:
<asp:datagrid id="dgrResult4" runat="server" /><br />
<b>The table contents after RejectChanges()</b>:
<asp:datagrid id="dgrResult5" runat="server" /><br />

<script language="C#" runat="server">

	void Page_Load(Object sender, EventArgs e)
	{

		// create a new empty Table object
		DataTable objTable = new DataTable("newTable");

		// define four columns (fields) within the table
		objTable.Columns.Add("ISBN", Type.GetType("System.String"));
		objTable.Columns.Add("Title", Type.GetType("System.String"));
		objTable.Columns.Add("PublicationDate", Type.GetType("System.DateTime"));
		objTable.Columns.Add("Quantity", Type.GetType("System.Int32"));

		// declare a variable to hold a DataRow object
		DataRow objDataRow;

		// create a new DataRow object instance in this table
		objDataRow = objTable.NewRow();

		// and fill in the values
		objDataRow["ISBN"] = "1234567800";
		objDataRow["Title"] = "Professional Video Recorder Programming";
		objDataRow["PublicationDate"] = new DateTime(2001, 3, 1);
		objDataRow["Quantity"] = "3956";
		objTable.Rows.Add(objDataRow);

		// repeat to add two more rows
		objDataRow = objTable.NewRow();
		objDataRow["ISBN"] = "1234567801";
		objDataRow["Title"] = "Professional WAP Phone Programming";
		objDataRow["PublicationDate"] = new DateTime(2201, 6, 1);
		objDataRow["Quantity"] = "29";
		objTable.Rows.Add(objDataRow);

		objDataRow = objTable.NewRow();
		objDataRow["ISBN"] = "1234567802";
		objDataRow["Title"] = "Professional Radio Station Programming";
		objDataRow["PublicationDate"] = new DateTime(2001, 4, 1);
		objDataRow["Quantity"] = "10456";
		objTable.Rows.Add(objDataRow);

		// call AcceptChanges to accept the changes to the table so far
		objTable.AcceptChanges();

		// assign the DataTable's DefaultView object to the DataGrid control
		dgrResult1.DataSource = objTable.DefaultView;
		dgrResult1.DataBind();  // and bind (display) the data

		// now Delete the second row and display the contents again
		objTable.Rows[1].Delete();
		dgrResult2.DataSource = objTable.DefaultView;
		dgrResult2.DataBind();

		// call RejectChanges to restore the deleted row
		objTable.RejectChanges();
		dgrResult3.DataSource = objTable.DefaultView;
		dgrResult3.DataBind();

		// now Remove the second row from the table
		// note that this is a method of the Rows collection not the Row object
		objTable.Rows.RemoveAt(1);
		dgrResult4.DataSource = objTable.DefaultView;
		dgrResult4.DataBind();

		// call RejectChanges - the deleted row is not restored
		objTable.RejectChanges();
		dgrResult5.DataSource = objTable.DefaultView;
		dgrResult5.DataBind();

	}
</script>


<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -