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. Removing items from a datagrid???

Removing items from a datagrid???

Scheduled Pinned Locked Moved ASP.NET
csshelptutorialquestion
24 Posts 2 Posters 1 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.
  • I Ironsmith1977

    Is there a way to create a column that AutoIncrements? Or is there a way that I can hard code in row numbers? However, since I'll never know exactly how many rows there will be in the DataGrid, that's most likely not a good idea. Well, unless I can AutoIncrement a column somehow, I guess I'll just have to leave it as is, it deletes the first row. lol. Horrible, but I'm running out of time. Take care Ironsmith

    M Offline
    M Offline
    minhpc_bk
    wrote on last edited by
    #14

    It's very simple to create an autoincrement column, take a quick look at the sample code below:

    DataColumn itemIDCol = dataTable.Columns.Add("ItemID", typeof(Int32));
    itemIDCol.AutoIncrement = true;
    itemIDCol.AutoIncrementSeed = 0;
    itemIDCol.AutoIncrementStep = 1;

    This column starts with a value of 0 and adds incrementally in steps of 1. For more information, see Creating AutoIncrement Columns[^] DataColumn Class[^]

    I 1 Reply Last reply
    0
    • M minhpc_bk

      It's very simple to create an autoincrement column, take a quick look at the sample code below:

      DataColumn itemIDCol = dataTable.Columns.Add("ItemID", typeof(Int32));
      itemIDCol.AutoIncrement = true;
      itemIDCol.AutoIncrementSeed = 0;
      itemIDCol.AutoIncrementStep = 1;

      This column starts with a value of 0 and adds incrementally in steps of 1. For more information, see Creating AutoIncrement Columns[^] DataColumn Class[^]

      I Offline
      I Offline
      Ironsmith1977
      wrote on last edited by
      #15

      Oh that is awesome, I just did it and it works great. Good to know that I'm not completely screwed! Thank you very much! I'm back to the same problem, but I now have a unique identifier column. I just keep getting that "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" exception error. Any thoughts? Take care Ironsmith

      M 1 Reply Last reply
      0
      • I Ironsmith1977

        Oh that is awesome, I just did it and it works great. Good to know that I'm not completely screwed! Thank you very much! I'm back to the same problem, but I now have a unique identifier column. I just keep getting that "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" exception error. Any thoughts? Take care Ironsmith

        M Offline
        M Offline
        minhpc_bk
        wrote on last edited by
        #16

        I just did it and it works great Sound better, :) Can you post here what line is causing that error?

        I 1 Reply Last reply
        0
        • M minhpc_bk

          I just did it and it works great Sound better, :) Can you post here what line is causing that error?

          I Offline
          I Offline
          Ironsmith1977
          wrote on last edited by
          #17

          This is where it stops:

          string id = PurchaseDataGrid.DataKeys[index] as string;

          I've tried altering [index], but that didn't work out so well. I've stepped through the code, and really I can't see why this code doesn't work. But this is the first time I'm using this sort of code, not to mention I just started ASP.NET(C#) a few weeks ago. Take care Ironsmith

          M 1 Reply Last reply
          0
          • I Ironsmith1977

            This is where it stops:

            string id = PurchaseDataGrid.DataKeys[index] as string;

            I've tried altering [index], but that didn't work out so well. I've stepped through the code, and really I can't see why this code doesn't work. But this is the first time I'm using this sort of code, not to mention I just started ASP.NET(C#) a few weeks ago. Take care Ironsmith

            M Offline
            M Offline
            minhpc_bk
            wrote on last edited by
            #18

            That's what I'm expecting, do you set the DataGrid.DataKeyField property? , here is the sample code:

            private void Page_Load(object sender, System.EventArgs e)
            {
            if(!IsPostBack)
            {
            DataTable table = CreateDataSource();

                //The ItemID is used as a key to select a specific row in a table.  
                //You may probably replace it with your real column name.
                PurchaseDataGrid.DataKeyField = "ItemID";
                                    
                PurchaseDataGrid.DataSource = table;
                PurchaseDataGrid.DataBind();
                               
                Session\["Items"\] = table;
             }
            

            }

            In addition, you can also set this property in the design window.

            I 1 Reply Last reply
            0
            • M minhpc_bk

              That's what I'm expecting, do you set the DataGrid.DataKeyField property? , here is the sample code:

              private void Page_Load(object sender, System.EventArgs e)
              {
              if(!IsPostBack)
              {
              DataTable table = CreateDataSource();

                  //The ItemID is used as a key to select a specific row in a table.  
                  //You may probably replace it with your real column name.
                  PurchaseDataGrid.DataKeyField = "ItemID";
                                      
                  PurchaseDataGrid.DataSource = table;
                  PurchaseDataGrid.DataBind();
                                 
                  Session\["Items"\] = table;
               }
              

              }

              In addition, you can also set this property in the design window.

              I Offline
              I Offline
              Ironsmith1977
              wrote on last edited by
              #19

              Well, I had that line of code in the wrong section...I had it in the click event, not the page load. But, it's progress, it got past that line and down to the next where it generated this exception error: "Cannot perform '=' operation on System.Int32 and System.String." This is the line of code:

              DataRow[] rows = dt.Select("ItemID='"+ id + "'");

              One thing is for certain, I'm learning more here from you than I have from my instructor all year! lol. Ironsmith

              M 2 Replies Last reply
              0
              • I Ironsmith1977

                Well, I had that line of code in the wrong section...I had it in the click event, not the page load. But, it's progress, it got past that line and down to the next where it generated this exception error: "Cannot perform '=' operation on System.Int32 and System.String." This is the line of code:

                DataRow[] rows = dt.Select("ItemID='"+ id + "'");

                One thing is for certain, I'm learning more here from you than I have from my instructor all year! lol. Ironsmith

                M Offline
                M Offline
                minhpc_bk
                wrote on last edited by
                #20

                I also guess this reason then I post all code in the Page_Load event. The key thing is to set that property before the datasource is really bound to the datagrid. Because at binding time, the DataKeys collection will be constructed based on the DataKeyField. If the ItemID column is of the Integer type, you simply remove the apostrophes in code, it will look like this:

                DataRow[] rows = dt.Select("ItemID="+ id);

                I 1 Reply Last reply
                0
                • M minhpc_bk

                  I also guess this reason then I post all code in the Page_Load event. The key thing is to set that property before the datasource is really bound to the datagrid. Because at binding time, the DataKeys collection will be constructed based on the DataKeyField. If the ItemID column is of the Integer type, you simply remove the apostrophes in code, it will look like this:

                  DataRow[] rows = dt.Select("ItemID="+ id);

                  I Offline
                  I Offline
                  Ironsmith1977
                  wrote on last edited by
                  #21

                  The ItemID column is of Integer type, but when I take out the apostrophes I get a "Syntax error: Missing operand after '=' operator." error. On the line of code:

                  DataRow[] rows = dt.Select("ItemID="+ id);

                  Take care Ironsmith

                  M 1 Reply Last reply
                  0
                  • I Ironsmith1977

                    The ItemID column is of Integer type, but when I take out the apostrophes I get a "Syntax error: Missing operand after '=' operator." error. On the line of code:

                    DataRow[] rows = dt.Select("ItemID="+ id);

                    Take care Ironsmith

                    M Offline
                    M Offline
                    minhpc_bk
                    wrote on last edited by
                    #22

                    Oh , I'm missing one thing. Because the ItemID column is of Integer type, then the code :

                    string id = PurchaseDataGrid.DataKeys[index] as string;

                    should be replaced by :

                    int id = (int) PurchaseDataGrid.DataKeys[index];

                    sorry about that, it should be working now.

                    I 1 Reply Last reply
                    0
                    • M minhpc_bk

                      Oh , I'm missing one thing. Because the ItemID column is of Integer type, then the code :

                      string id = PurchaseDataGrid.DataKeys[index] as string;

                      should be replaced by :

                      int id = (int) PurchaseDataGrid.DataKeys[index];

                      sorry about that, it should be working now.

                      I Offline
                      I Offline
                      Ironsmith1977
                      wrote on last edited by
                      #23

                      OH MY GOD, it's working!!! lmfao! Thank you so much my friend, I will be singing your praises for weeks. lmfao. Take care Ironsmith

                      1 Reply Last reply
                      0
                      • I Ironsmith1977

                        Well, I had that line of code in the wrong section...I had it in the click event, not the page load. But, it's progress, it got past that line and down to the next where it generated this exception error: "Cannot perform '=' operation on System.Int32 and System.String." This is the line of code:

                        DataRow[] rows = dt.Select("ItemID='"+ id + "'");

                        One thing is for certain, I'm learning more here from you than I have from my instructor all year! lol. Ironsmith

                        M Offline
                        M Offline
                        minhpc_bk
                        wrote on last edited by
                        #24

                        Well-done Ironsmith, :beer: :) Good luck to you.

                        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