Today, when I write a program, I encounter the problem that the able cannot be added. The Code is as follows:
DataTable dt = dataGridView1.DataSource as DataTable; DataTable dtTemp = stfBLL.GetDataByBoxID(boxId); DataRow dr = dt.NewRow(); dr["BoxID"] = dtTemp.Rows[0]["PicID"].ToString(); dr["Item"] = dtTemp.Rows[0]["Item"].ToString(); dr["LotNumber"] = dtTemp.Rows[0]["KS_LOT"].ToString(); dr["STK"] = stk; dr["Bin"] = bin; dr["Qty"] = Convert.ToSingle(qty); dt.ImportRow(dr); dataGridView1.DataSource = dt;
After several attempts, I did not add the data, so I asked du Niang for help because of the difference between importrow () and rows. Add () of datatable;
1. First, let's talk about datatable. newrow () method. This method can be used to create a datarow with the same schema as the table (and this method must be used to create a datarow with the same schema as the original table ), the new row is added to the original table. However, I found that this blank row does not exist in this able. Why? The reason is as follows: the rows in the original able have a rowstate attribute. There are several types of attributes:
1, detachedThis row has been created but does not belong to any datarowcollection. Datarow is in this status immediately after being created or added to or removed from the collection.
2, unchangedThis row has not been changed since the last acceptchanges call.
3, addedThis row has been added to datarowcollection, and acceptchanges has not been called.
4, deletedThis row has been deleted through the delete method of datarow.
5, modifiedThis row has been modified and acceptchanges has not been called.
In fact, the rowstate of the row created by newrow () is detached, and the newly created row is obviously invisible.
2. importrow ()
Copy daterow to able to retain any attribute settings, initial values, and current values. However, when the rowstate attribute of datarow is detached, the data cannot be copied. Therefore, the preceding yellow code is unavailable. Some people on the Internet say that the new row cannot be copied because it belongs to the original table. I think this is incorrect because of the row attribute. In fact, if we change the row attributes, the importrow () method is still feasible.
Importrow () is generally used to copy data from one table to another. The instance code is as follows:
1 private DataTable HowToUseImportRow(DataTable dt)2 {3 DataTable dtNew = dt.Clone();4 foreach (DataRow dr in dt.Rows)5 {6 dtNew.ImportRow(dr);7 }8 return dtNew;9 }
View code
3. Add ()
Add the specified datarow to the datarowcollection object. The instance code is as follows:
1 private void AddRow(DataTable table) 2 { 3 DataRowCollection rowCollection = table.Rows; 4 // Instantiate a new row using the NewRow method. 5 6 DataRow newRow = table.NewRow(); 7 // Insert code to fill the row with values. 8 9 // Add the row to the DataRowCollection.10 table.Rows.Add(newRow);11 }
View code
After adding datarow, The rowstate attribute of datarow is changed to added. Change the preceding dtappendix. importrow (DR) to dtappendix. Rows. Add (DR.
Difference between able. importrow () and datatable. Rows. Add ()