joe_pool_is Posted July 30, 2008 Posted July 30, 2008 I have two procedures that work together. If a child node is selected on a TreeView control, the records for the parent are displayed and focus is given to the child record. If the parent node is selected, I simply want to display the records associated with the parent node. It worked ok (by displaying all of the data rows) until I tried to apply a RowFilter to the DefaultView. Now, the DataGridView contains 0 Rows (see line 576 below), and my app would sit there and loop until it reaches a stack overflow (because of the test condition on line 552 and the recursive call on line 554) if I tried to deply it now. Instead of getting rid of the loop condition, I'd like to figure out how to fix the DataGridView's DataSource problem, because my code does not seem to take the statement like it is supposed to. One example that I saw showed to use DataGridView's DataBind method afterward applying a DataSource (line 575 below), but I don't know how to "unbind" the DataGridView for the next time I need to display a different table.535: private void DataGrid_FindRow(TreeNode node) 536: { 537: string colName = node.Parent.Tag.ToString(); 538: if (DataGridView1.Columns.Contains(colName) == false) 539: { 540: DataGrid_Show(node.Parent); 541: } 542: if (DataGridView1.Columns.Contains(colName) == true) 543: { // just in case there was an error 544: DataGridViewCell cell = null; 545: for (int i = 0; (i < DataGridView1.Rows.Count) && (cell == null); i++) 546: { 547: if (DataGridView1.Rows[i].Cells[colName].Value.ToString().Trim() == node.Text) 548: { 549: cell = DataGridView1.Rows[i].Cells[colName]; 550: } 551: } 552: if (cell == null) { 553: DataGrid_Show(node.Parent); 554: DataGrid_FindRow(node); 555: } else { 556: DataGridView1.CurrentCell = cell; 557: } 558: } 559: } 560: 561: private void DataGrid_Show(TreeNode node) 562: { 563: if (0 < DataGridView1.Columns.Count) 564: DataGridView1.Columns.Clear(); 565: if (0 < DataGridView1.Rows.Count) 566: DataGridView1.Rows.Clear(); 567: if (Global.CpAppDataSet.Tables.Contains(node.Parent.Tag.ToString()) == true) 568: { 569: DataTable dt = Global.CpAppDataSet.Tables[node.Parent.Tag.ToString()].Copy(); 570: m_title = string.Format("{0} LIKE '{1}'", node.Tag.ToString(), node.Text); 571: dt.DefaultView.RowFilter = m_title; 572: dt.AcceptChanges(); // dt: Columns.Count = 9; Rows.Count = 1589 573: if (dt.Columns.Contains(node.Tag.ToString()) == true) 574: { 575: DataGridView1.DataSource = dt.DefaultView; 576: DataGridView1.Refresh(); // DataGridView1: Columns.Count = 9; Rows.Count = 0 577: } 578: DataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells); 579: string output = string.Format("{0} - {1} Records returned", node.Tag.ToString(), DataGridView1.Rows.Count); 580: UpdateStatusBar(output); 581: } 582: } As an FYI: If I do *not* create the DataTable copy on line 569 or apply a RowFilter on 571, the code correctly selects the row entry that matches the entry in the TreeView - but it also shows all 1589 records, whereas I want to filter this to only show the 10-100 records associated with that record's parent node. I hope this makes sense to others, and I hope my code example isn't too long and is easy enough to understand. Quote Avoid Sears Home Improvement
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.