Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Dynamic removal of rows

Dynamic removal of rows

Scheduled Pinned Locked Moved ASP.NET
csharphelpquestionasp-nettutorial
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sudee
    wrote on last edited by
    #1

    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.

    A 1 Reply Last reply
    0
    • S Sudee

      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.

      A Offline
      A Offline
      Andrew Quinn AUS
      wrote on last edited by
      #2

      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

      S 1 Reply Last reply
      0
      • A Andrew Quinn AUS

        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

        S Offline
        S Offline
        Sudee
        wrote on last edited by
        #3

        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.

        A 1 Reply Last reply
        0
        • S Sudee

          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.

          A Offline
          A Offline
          Andrew Quinn AUS
          wrote on last edited by
          #4

          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

          S 1 Reply Last reply
          0
          • A Andrew Quinn AUS

            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

            S Offline
            S Offline
            Sudee
            wrote on last edited by
            #5

            thanks Andy, Some dudes gotta know how to develop a prg. and some dudes just never get it at all.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups