by Christiawan Liando on Monday, December 24th - 3:52 AM
Hi.. I want to delete a row in a DataTable from the record in DataTable but it always error or nothing's happen or it always leaves the last row on the datatable.
The error I got was:
"Table does not have primary key."
OR "System.Data.RowNotInTableException: Cannot perform this operation on a row not in the table."
OR "System.InvalidOperationException: Collection was modified; enumeration operation might not execute.
"
Actually, I have successfully removing the records, but if it has only one row it will remove that rows, but the number of rows says 2 or more, it always leaves the last row on the datatable. For example it has 4 rows of record, it will remove the first 3 rows and leaves the fourth row in the datatable.
I have read that when you delete a row with normal order the index of each item have to be updated each time. I think that is my problem, how to update the index?
WHAT I WANT TO DO IS WHEN I REMOVE THE COMPANY NODE OR THE EMPLOYEE NODE FROM THE TREEVIEW, THE CORRESPONDING LISTVIEW ITEMS WILL ALSO BE REMOVED.
<DELETING FROM TREEVIEW>
IF I REMOVE IBM THEN ALL EMPLOYEES WORK IN IBM WILL BE REMOVED SO THE LISTVIEW WILL REMOVE Richard, James and Donna.
IF I REMOVE Richard, then THE LISTVIEW WILL ONLY REMOVE Richard, and so on..
Note that deleting from treeView CAN REMOVE THE COMPANY NAME ON THE TREEVIEW SO UNLIKE THE LISTVIEW to TREEVIEW DELETION
-ALL
-IBM
-Richard
-James
-Donna
-Intel
-Paul
-Jonathan
The listView:
Company name | Employee Name | Age | Gender | etc....
===========================================
IBM | Richard | 30 | Male | ....
| James | 31 | Male | ....
| Donna | 35 | Female | ....
Intel | Paul | 31 | Male | ....
| Jonathan | 29 | Male | ....
THE CODES BELOW HAS SUCCESSFULLY REMOVE THE LISTVIEW ITEMS AND THE CORRESPONDING NODES WILL BE DELETED. <DELETING FROM LISTVIEW>
IF I REMOVE Row IBM | Richard | ... then the treeView will also remove Richard.
Note that deleting from listView CANNOT REMOVE THE COMPANY NAME ON THE TREEVIEW..
SO I WANT TO DO IS HOW TO DELETING THE RECORDS FROM TREEVIEW.
I have used the same way to delete the nodes but it did not work.
Can you please fix/add my codes below so that it can be used to do what I am expecting?
2. I also want to display and print the same company name once like on my example above. How to do that? I have tried this one below and also I have tried to change the font color but it did not work.
What I am confused is about the data sorting because it will sort the ""/String::Empty if I use ""/String::Empty.
String ^str = "";
for each (DataRow^ row in empView->ToTable()->Rows) {
ListViewItem^ item = gcnew ListViewItem(row[0]->ToString());
ListView::ListViewItemCollection^ lst = listView1->Items;
IEnumerator^ myEnum = lst->GetEnumerator();
while ( myEnum->MoveNext() )
{
ListViewItem^ it = safe_cast<ListViewItem^>(myEnum->Current);
if (String::Compare(it->Text,str,true) == 0) {
item->Text = String::Empty;
}
}
If anybody have ideas and solutions please let me know soon, because I really need this.
Thank you very much.
The code for removing corresponding treeView nodes from listView:
private void deleteNode(string company, string name) //Delete the Node
{
if (treeView1.Nodes[0].Nodes.Find(company, false).Length > 0)
{
TreeNode comNode = treeView1.Nodes[0].Nodes.Find(company, false)[0];
if (comNode.Nodes.Find(name, false).Length > 0)
{
comNode.Nodes.Find(name, false)[0].Remove();
if (comNode.Nodes.Count == 0) //If it is the only node of the parent node, delete the parent node
{
comNode.Remove();
}
}
}
}
private void listView1_KeyDown(object sender, KeyEventArgs e) //Delete the record in ListView, TreeView and DataTable
{
if (e.KeyCode== Keys.Delete && listView1.SelectedItems.Count > 0)
{
foreach (ListViewItem item in listView1.SelectedItems)
{
foreach (DataRow row in employees.Rows)
{
if (row[0].ToString() == item.SubItems[0].Text && row[1].ToString() == item.SubItems[1].Text)
{
employees.Rows.Remove(row);
break;
}
}
deleteNode(item.SubItems[0].Text, item.SubItems[1].Text);
listView1.Items.Remove(item);
}
}
}