In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to get the DataTable object status DataRowState by C#. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
DataGridView: gets the state of the DataRow object with a total of five enumerated values.
The Added line has been added to the DataRowCollection and AcceptChanges has not been called. The Deleted line has been deleted through the Delete method of DataRow. The Detached line has been created but does not belong to any DataRowCollection. DataRow is in this state immediately after it is created before it is added to the collection or after it is removed from the collection. The Modified line has been modified and AcceptChanges has not been called. The Unchanged line has not changed since the last call to AcceptChanges.
Just after a DataRow object is created, its state is Detached, which is an isolated existence, so after the DataRow is established, the DataRow is added to the DataTable through the DataTable.Rows.Add (DataRow) method, and the state of the DataRow is changed from Detached to Added.
When the DataRow is modified, the state of the DataRow changes to Modified, and when the DataRow is deleted with the DataRow.Delete () method, the DataRow state changes to Deleted, but this line still exists in the DataTable, but the status has changed, and the number of rows is checked with DataTable.Rows.Count, which is the same as before deletion.
After using the Delete method, RowState becomes "Deleted". It remains "deleted" until you call AcceptChanges, and the row cannot be modified, or an error will be reported: the information of the deleted line cannot be accessed. You can use RejectChanges to undelete rows.
In the Deleted state, if you want to reuse the row, you can re-use the DataTable.Rows.Add (DataRow) method to join after calling AcceptChanges, where the state of the line changes to Added.
Note:
1. Only by using Delete () in the Added state can the row be deleted in the DataTable, making the state of the Rows.Count-1,DataRow Detached (free state, instantiating the line with NewRow (), which is the same state that has not yet been added to the DataTable). If you use the DataTable.AcceptChanges () method in the Added state, and after using the DataRow.Delete () method, the line is not deleted from the DataTable, but the state is changed to Deleted, with the same number of rows.
2. Unchange (the state of a non-Deleted line after calling AcceptChanges, which is also taken directly from the database), changes to Modify or Deleted state after modifying the content or calling Delete ()
3. Added is still Added after modification.
4. Calling Delete () in Unchange and Modify state will change to Deleted, but Rows.Count will not decrease.
5. If you modify the content of the line in Deleted, it will report that the information of the line cannot be accessed through the deleted line.
6. In the Deleted state, if you still want to reuse the row, you can re-use the Table.Rows.Add (DataRow) method to join after calling AcceptChanges, and the state after joining is Added.
Sample code:
Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Data;namespace ConsoleApplication1 {class Program {static void Main (string [] args) {/ / create a DataTable DataTable dtDemo = GetDataTable (); DataRow myDr / / Detached row: the line has been created but does not belong to any DataRowCollection myDr = dtDemo.NewRow (); Console.WriteLine ("NewRow status:" + myDr.RowState); / / add the newly created line to the DataTable dtDemo.Rows.Add (myDr); Console.WriteLine ("Add Row status:" + myDr.RowState) After # region Add, directly use Delete,RowState to change to Detached. The number of rows is-1 / / myDr.Delete (); / / Console.WriteLine ("status of myDr:" + myDr.RowState); / / Console.WriteLine ("number of rows of dtDemo:" + dtDemo.Rows.Count.ToString ()); # endregion dtDemo.AcceptChanges () Console.WriteLine ("AcceptChanges status:" + myDr.RowState); / / modify myDr ["Name"] = "Tom"; Console.WriteLine ("Modified status:" + myDr.RowState); Console.WriteLine ("rows before deletion:" + dtDemo.Rows.Count.ToString ()) # region using Delete,RowState in non-Added state becomes Deleted with the same number of rows myDr.Delete (); Console.WriteLine ("Deleted status:" + myDr.RowState); Console.WriteLine ("number of rows deleted:" + dtDemo.Rows.Count.ToString ()) # endregion / / when the status of myDr changes to Detached Console.WriteLine ("status of myDr:" + myDr.RowState); / / rejoins myDr, the status changes to Added dtDemo.Rows.Add (myDr); Console.WriteLine ("status of myDr:" + myDr.RowState); / / cancels line deletion / / dtDemo.RejectChanges () Console.ReadKey (); create an empty DataTable / static DataTable GetDataTable () {DataTable dt = new DataTable ("MyTable"); DataColumn dc = new DataColumn ("Name", typeof (string)); dt.Columns.Add (dc); return dt } this is the end of the article on "how to get DataTable object status DataRowState by C#". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.