Dynamic removal of rows
-
plss somebody help! I'm having a problem with dynamic rows in a HTMLTable, Asp.net/C#. I can dynamically copy one row, and add up a new row in a table. But when it comes to delete the rows... rows.remove() is just not working. Before coming here I checked fews docs to find out I need to reconstruct the rows and cells. Can anyone tell me how can I do it? thanks, dudes know how to prg. and some dudes just never get it.
-
plss somebody help! I'm having a problem with dynamic rows in a HTMLTable, Asp.net/C#. I can dynamically copy one row, and add up a new row in a table. But when it comes to delete the rows... rows.remove() is just not working. Before coming here I checked fews docs to find out I need to reconstruct the rows and cells. Can anyone tell me how can I do it? thanks, dudes know how to prg. and some dudes just never get it.
Hi Sudee, I've not had any problems with either WebControls Table's or HtmlControls Table's. Have you tried RemoveAt instead, Remove requires the Row object so sometimes code-wise it's easy to remove the row when you know the index. Otherwise can you expand a little on how/what you are doing? Cheers, Andy
-
Hi Sudee, I've not had any problems with either WebControls Table's or HtmlControls Table's. Have you tried RemoveAt instead, Remove requires the Row object so sometimes code-wise it's easy to remove the row when you know the index. Otherwise can you expand a little on how/what you are doing? Cheers, Andy
Thanks Andy. Here is the code! ----------------- private void DeleteRow_Click(object sender, System.EventArgs e) { while( GetCheckBoxesIndex() != -1 ) { int RowIndex = GetCheckBoxesIndex(); mainTable.Rows.RemoveAt( RowIndex ); } } -------------------- private int GetCheckBoxesIndex() { for( int i = 1; i < mainTable.Rows.Count; ++i ) { HtmlTableRow tr = (HtmlTableRow)mainTable.Rows[i]; HtmlTableCell cell = tr.Cells[0]; if( cell.HasControls() ) { CheckBox checkBox = (CheckBox)cell.Controls[0]; if ( checkBox.Checked ) { return i; } } } return -1; } ------------------- There is some problem with postback event. I think. So I gotta reconstruct my table after each postback. But don't know how to do it. I want to delete a list of rows by hitting delete button, after selected checkboxes of rows. Some dudes gotta know how to develop a prg. and some dudes just never get it at all.
-
Thanks Andy. Here is the code! ----------------- private void DeleteRow_Click(object sender, System.EventArgs e) { while( GetCheckBoxesIndex() != -1 ) { int RowIndex = GetCheckBoxesIndex(); mainTable.Rows.RemoveAt( RowIndex ); } } -------------------- private int GetCheckBoxesIndex() { for( int i = 1; i < mainTable.Rows.Count; ++i ) { HtmlTableRow tr = (HtmlTableRow)mainTable.Rows[i]; HtmlTableCell cell = tr.Cells[0]; if( cell.HasControls() ) { CheckBox checkBox = (CheckBox)cell.Controls[0]; if ( checkBox.Checked ) { return i; } } } return -1; } ------------------- There is some problem with postback event. I think. So I gotta reconstruct my table after each postback. But don't know how to do it. I want to delete a list of rows by hitting delete button, after selected checkboxes of rows. Some dudes gotta know how to develop a prg. and some dudes just never get it at all.
Hi again, Here is an example of what you need to do. I've used the asp:table implementation but it should be the same as the HtmlTable implementation. In the handler for the delete button...
private void Button1\_Click(object sender, System.EventArgs e) { for(int i=Table1.Rows.Count-1; i>=0; i--) { TableRow row = Table1.Rows\[i\]; if (row != null) { TableCell cell0 = row.Cells\[0\]; if (cell0 != null && cell0.HasControls()) { CheckBox cb = (CheckBox)cell0.Controls\[0\]; if (cb != null && cb.Checked) { Table1.Rows.Remove(row); } } } } }
Note that we go backwards through the table - a commom mistake is to go forward, but remember as soon as you delete say row 1, then row 2 becomes 1, row 3 becomes 2..etc.. but the loop will carry on to row 2 (missing the newly appointed row 1) thus you would get a strange looking table. You were also correct in the assumption of recreating the table upon postback. The code in your Page_Load event (that constructs the table) should be executed whether IsPostback is true or false. Hope this helps, Andy
-
Hi again, Here is an example of what you need to do. I've used the asp:table implementation but it should be the same as the HtmlTable implementation. In the handler for the delete button...
private void Button1\_Click(object sender, System.EventArgs e) { for(int i=Table1.Rows.Count-1; i>=0; i--) { TableRow row = Table1.Rows\[i\]; if (row != null) { TableCell cell0 = row.Cells\[0\]; if (cell0 != null && cell0.HasControls()) { CheckBox cb = (CheckBox)cell0.Controls\[0\]; if (cb != null && cb.Checked) { Table1.Rows.Remove(row); } } } } }
Note that we go backwards through the table - a commom mistake is to go forward, but remember as soon as you delete say row 1, then row 2 becomes 1, row 3 becomes 2..etc.. but the loop will carry on to row 2 (missing the newly appointed row 1) thus you would get a strange looking table. You were also correct in the assumption of recreating the table upon postback. The code in your Page_Load event (that constructs the table) should be executed whether IsPostback is true or false. Hope this helps, Andy