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. General Programming
  3. C#
  4. trying to access rows in a table

trying to access rows in a table

Scheduled Pinned Locked Moved C#
testingbeta-testingquestion
12 Posts 3 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.
  • T Offline
    T Offline
    theStorminMormon
    wrote on last edited by
    #1

    I have a nice table that I create and show in a datagrid. The data loads perfectly, and then displays nicely. But I need to do some things with it. For starters, I want to add another column that is for display only. So I've tried to add the column "Date" as shown, but it never shows up in the datagrid. I also want to be able to set the Date column, which I thought would entail cycling through the rows. However when I try to cycle through the rows (in the foreach statement) nothing happens. The MessageBox.Show never pops up. Any idea why my new column won't show up and why I can't cycle through the rows of my table? If there's an easier way to just add a column and dynamically change the entries for each row, let me know. And if there's a way to keep data in the data set/data table and NOT show it in the datagrid, I'd like to know that too (I don't want my primary and foreign keys showing up at all but i need to keep track of them). Thanks! strClaimsSQL = this.GetClaimsSQL(); da = new OleDbDataAdapter(strClaimsSQL, strConn); ds = new DataSet(); ClaimsTable = new DataTable(); da.Fill(ds, "ClaimsTable"); DataColumn claimDates = ClaimsTable.Columns.Add("Date"); claimDates.ColumnName = "Date"; this.dgrClaimsData.DataSource = ds; this.dgrClaimsData.DataMember = "ClaimsTable"; foreach (DataRow row in ClaimsTable.Rows) { MessageBox.Show("This is a row in the Claims Table.", "Testing Output"); int i = 0; row["Date"] = i.ToString(); i++; row["Premiums"] = i.ToString(); } The ends can never justify the means. It is the means that determine the ends.

    M T 2 Replies Last reply
    0
    • T theStorminMormon

      I have a nice table that I create and show in a datagrid. The data loads perfectly, and then displays nicely. But I need to do some things with it. For starters, I want to add another column that is for display only. So I've tried to add the column "Date" as shown, but it never shows up in the datagrid. I also want to be able to set the Date column, which I thought would entail cycling through the rows. However when I try to cycle through the rows (in the foreach statement) nothing happens. The MessageBox.Show never pops up. Any idea why my new column won't show up and why I can't cycle through the rows of my table? If there's an easier way to just add a column and dynamically change the entries for each row, let me know. And if there's a way to keep data in the data set/data table and NOT show it in the datagrid, I'd like to know that too (I don't want my primary and foreign keys showing up at all but i need to keep track of them). Thanks! strClaimsSQL = this.GetClaimsSQL(); da = new OleDbDataAdapter(strClaimsSQL, strConn); ds = new DataSet(); ClaimsTable = new DataTable(); da.Fill(ds, "ClaimsTable"); DataColumn claimDates = ClaimsTable.Columns.Add("Date"); claimDates.ColumnName = "Date"; this.dgrClaimsData.DataSource = ds; this.dgrClaimsData.DataMember = "ClaimsTable"; foreach (DataRow row in ClaimsTable.Rows) { MessageBox.Show("This is a row in the Claims Table.", "Testing Output"); int i = 0; row["Date"] = i.ToString(); i++; row["Premiums"] = i.ToString(); } The ends can never justify the means. It is the means that determine the ends.

      M Offline
      M Offline
      Mohamad Al Husseiny
      wrote on last edited by
      #2

      The columns Didnt displayed and you can not loop throgh The Rows because the tables was empty look with me theStorminMormon wrote: da.Fill(ds, "ClaimsTable"); Now you filled the DataSet with Data from your table This why all other Columns displayed in the grid theStorminMormon wrote: ClaimsTable = new DataTable();i> Now you Create **New** DataTable which have not any relation with ds DataSet So Remove it and write` DataColumn claimDates = sd.Tables["ClaimsTable"].Columns.Add("Date"); claimDates.ColumnName = "Date"; `To loop for each row foreach (DataRow dr in ds.tables["ClaimsTable"] .Rows) { //do your work } I hope this help MCAD

      T 2 Replies Last reply
      0
      • T theStorminMormon

        I have a nice table that I create and show in a datagrid. The data loads perfectly, and then displays nicely. But I need to do some things with it. For starters, I want to add another column that is for display only. So I've tried to add the column "Date" as shown, but it never shows up in the datagrid. I also want to be able to set the Date column, which I thought would entail cycling through the rows. However when I try to cycle through the rows (in the foreach statement) nothing happens. The MessageBox.Show never pops up. Any idea why my new column won't show up and why I can't cycle through the rows of my table? If there's an easier way to just add a column and dynamically change the entries for each row, let me know. And if there's a way to keep data in the data set/data table and NOT show it in the datagrid, I'd like to know that too (I don't want my primary and foreign keys showing up at all but i need to keep track of them). Thanks! strClaimsSQL = this.GetClaimsSQL(); da = new OleDbDataAdapter(strClaimsSQL, strConn); ds = new DataSet(); ClaimsTable = new DataTable(); da.Fill(ds, "ClaimsTable"); DataColumn claimDates = ClaimsTable.Columns.Add("Date"); claimDates.ColumnName = "Date"; this.dgrClaimsData.DataSource = ds; this.dgrClaimsData.DataMember = "ClaimsTable"; foreach (DataRow row in ClaimsTable.Rows) { MessageBox.Show("This is a row in the Claims Table.", "Testing Output"); int i = 0; row["Date"] = i.ToString(); i++; row["Premiums"] = i.ToString(); } The ends can never justify the means. It is the means that determine the ends.

        T Offline
        T Offline
        theStorminMormon
        wrote on last edited by
        #3

        I'll answer my own question now that I figured it out. Just in case anyone else ever wants to know... Right, so the problem was thinking that the table I created on this line: ClaimsTable = new DataTable(); was the same as the table created by the data adapter fill method: da.Fill(ds, "ClaimsTable"); But the two are not the same. To access the table with my data I need something like da.tables[0]... So when I ran the foreach (DataRow row in ClaimsTables.Rows) I was accessing a table that had been created, but that had no rows. So essentially I had two tables with identical names, but one held data and one did not and I needed to learn how to access the one that DID hold data. Yay for learning experiences. The ends can never justify the means. It is the means that determine the ends.

        M 1 Reply Last reply
        0
        • M Mohamad Al Husseiny

          The columns Didnt displayed and you can not loop throgh The Rows because the tables was empty look with me theStorminMormon wrote: da.Fill(ds, "ClaimsTable"); Now you filled the DataSet with Data from your table This why all other Columns displayed in the grid theStorminMormon wrote: ClaimsTable = new DataTable();i> Now you Create **New** DataTable which have not any relation with ds DataSet So Remove it and write` DataColumn claimDates = sd.Tables["ClaimsTable"].Columns.Add("Date"); claimDates.ColumnName = "Date"; `To loop for each row foreach (DataRow dr in ds.tables["ClaimsTable"] .Rows) { //do your work } I hope this help MCAD

          T Offline
          T Offline
          theStorminMormon
          wrote on last edited by
          #4

          I guess I was writing my own reply at the same time that you were writing this one. I just wanted to say thanks for your response. It's kind of you to help out, even though in this case I had figured it out too (usually I don't figure it out on my own). Thanks! The ends can never justify the means. It is the means that determine the ends.

          1 Reply Last reply
          0
          • M Mohamad Al Husseiny

            The columns Didnt displayed and you can not loop throgh The Rows because the tables was empty look with me theStorminMormon wrote: da.Fill(ds, "ClaimsTable"); Now you filled the DataSet with Data from your table This why all other Columns displayed in the grid theStorminMormon wrote: ClaimsTable = new DataTable();i> Now you Create **New** DataTable which have not any relation with ds DataSet So Remove it and write` DataColumn claimDates = sd.Tables["ClaimsTable"].Columns.Add("Date"); claimDates.ColumnName = "Date"; `To loop for each row foreach (DataRow dr in ds.tables["ClaimsTable"] .Rows) { //do your work } I hope this help MCAD

            T Offline
            T Offline
            theStorminMormon
            wrote on last edited by
            #5

            Is there any wya to add the "Dates" column so that it will be the first column in the data grid? Thanks! The ends can never justify the means. It is the means that determine the ends.

            M 1 Reply Last reply
            0
            • T theStorminMormon

              I'll answer my own question now that I figured it out. Just in case anyone else ever wants to know... Right, so the problem was thinking that the table I created on this line: ClaimsTable = new DataTable(); was the same as the table created by the data adapter fill method: da.Fill(ds, "ClaimsTable"); But the two are not the same. To access the table with my data I need something like da.tables[0]... So when I ran the foreach (DataRow row in ClaimsTables.Rows) I was accessing a table that had been created, but that had no rows. So essentially I had two tables with identical names, but one held data and one did not and I needed to learn how to access the one that DID hold data. Yay for learning experiences. The ends can never justify the means. It is the means that determine the ends.

              M Offline
              M Offline
              Mohamad Al Husseiny
              wrote on last edited by
              #6

              Good you figured it out As you guessed i wrote my previous message when you sent your message any way I Update it may look to it MCAD

              1 Reply Last reply
              0
              • T theStorminMormon

                Is there any wya to add the "Dates" column so that it will be the first column in the data grid? Thanks! The ends can never justify the means. It is the means that determine the ends.

                M Offline
                M Offline
                Mohamad Al Husseiny
                wrote on last edited by
                #7

                If you want to display Date Column at firs Add it First Before You Fill The DataTable a modification of previous code that will display Date column at first will be as following

                //create the table
                DataTable table=new DataTable("Customer");
                //create the colum
                DataColumn  dc=new DataColumn("Data");
                //add it
                table.Columns.Add(dc);
                //NOW fill it
                sqlDataAdapter1.Fill(table);
                dataGrid1.DataSource=table;
                

                MCAD

                D 1 Reply Last reply
                0
                • M Mohamad Al Husseiny

                  If you want to display Date Column at firs Add it First Before You Fill The DataTable a modification of previous code that will display Date column at first will be as following

                  //create the table
                  DataTable table=new DataTable("Customer");
                  //create the colum
                  DataColumn  dc=new DataColumn("Data");
                  //add it
                  table.Columns.Add(dc);
                  //NOW fill it
                  sqlDataAdapter1.Fill(table);
                  dataGrid1.DataSource=table;
                  

                  MCAD

                  D Offline
                  D Offline
                  Dan Neely
                  wrote on last edited by
                  #8

                  Could you please stop using that background image. It makes reading your posts all but impossible.

                  M 1 Reply Last reply
                  0
                  • D Dan Neely

                    Could you please stop using that background image. It makes reading your posts all but impossible.

                    M Offline
                    M Offline
                    Mohamad Al Husseiny
                    wrote on last edited by
                    #9

                    What background image you are taking about? are you mean

                    This Not background image it is pre tag MCAD

                    D 1 Reply Last reply
                    0
                    • M Mohamad Al Husseiny

                      What background image you are taking about? are you mean

                      This Not background image it is pre tag MCAD

                      D Offline
                      D Offline
                      Dan Neely
                      wrote on last edited by
                      #10

                      Yes that's exactly what I'm refering to. I don't know what you want it to do, but in both firefox and ID it's streching the length of the screen and making the text behind unreadable without highlighting with the mouse.

                      M 1 Reply Last reply
                      0
                      • D Dan Neely

                        Yes that's exactly what I'm refering to. I don't know what you want it to do, but in both firefox and ID it's streching the length of the screen and making the text behind unreadable without highlighting with the mouse.

                        M Offline
                        M Offline
                        Mohamad Al Husseiny
                        wrote on last edited by
                        #11

                        Sorry for annoying you but nothing from what you mentioned appear in my screen So i use it to highlight the code and its displayed whith out problem MCAD

                        D 1 Reply Last reply
                        0
                        • M Mohamad Al Husseiny

                          Sorry for annoying you but nothing from what you mentioned appear in my screen So i use it to highlight the code and its displayed whith out problem MCAD

                          D Offline
                          D Offline
                          Dan Neely
                          wrote on last edited by
                          #12

                          What's it look like in your browser, in FF1.06 and IE6 the area is tiled with a letter image. Anyone else seeing the same?

                          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