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. Read data from gridview

Read data from gridview

Scheduled Pinned Locked Moved ASP.NET
csshelpquestion
12 Posts 5 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.
  • M mail572352

    Hi, I have a gridview containing several rows of multi-column data. I've bound my grid from an ArrayList specifying bound fields on my gridview columns like this:

    <asp:BoundField HeaderText="Price" DataField="SalesPrice" />

    I can then read in cell/column data by indexing into the row and indexing to the column I need like this:

    String str = (gvItems.Rows[i].Cells[0]).Text;

    Knowing exactly which column it is, is error-prone so what I'd like to do is access my data knowing the datafield name, "SalesPrice". Can anyone suggest a good way of doing this? Do I need to have a template?

    D Offline
    D Offline
    dinus28
    wrote on last edited by
    #2

    hi it is always better to use index than using the name. bcoz if the name is changed u have to modify everywhere wherver u use it. for huge projects it is a very tedious job. my suggestion would be that u better use index.

    M 1 Reply Last reply
    0
    • M mail572352

      Hi, I have a gridview containing several rows of multi-column data. I've bound my grid from an ArrayList specifying bound fields on my gridview columns like this:

      <asp:BoundField HeaderText="Price" DataField="SalesPrice" />

      I can then read in cell/column data by indexing into the row and indexing to the column I need like this:

      String str = (gvItems.Rows[i].Cells[0]).Text;

      Knowing exactly which column it is, is error-prone so what I'd like to do is access my data knowing the datafield name, "SalesPrice". Can anyone suggest a good way of doing this? Do I need to have a template?

      G Offline
      G Offline
      gauthee
      wrote on last edited by
      #3

      how about: gvItems.Rows[i]["column name"].Text

      Gautham

      D M 2 Replies Last reply
      0
      • G gauthee

        how about: gvItems.Rows[i]["column name"].Text

        Gautham

        D Offline
        D Offline
        dinus28
        wrote on last edited by
        #4

        what would happen if column name is changed???

        G 2 Replies Last reply
        0
        • M mail572352

          Hi, I have a gridview containing several rows of multi-column data. I've bound my grid from an ArrayList specifying bound fields on my gridview columns like this:

          <asp:BoundField HeaderText="Price" DataField="SalesPrice" />

          I can then read in cell/column data by indexing into the row and indexing to the column I need like this:

          String str = (gvItems.Rows[i].Cells[0]).Text;

          Knowing exactly which column it is, is error-prone so what I'd like to do is access my data knowing the datafield name, "SalesPrice". Can anyone suggest a good way of doing this? Do I need to have a template?

          V Offline
          V Offline
          varshavmane
          wrote on last edited by
          #5

          If u just want to read the Gridview values then try this : For intGridRowcnt = 1 To GridViewQuery.Rows.Count If intGridRowcnt = GridViewQuery.Rows.Count Then strWhereClause = strWhereClause & GridViewQuery.Rows(intGridRowcnt - 1).Cells(3).Text & " " strWhereClause = strWhereClause & GridViewQuery.Rows(intGridRowcnt - 1).Cells(4).Text & " " strWhereClause = strWhereClause & GridViewQuery.Rows(intGridRowcnt - 1).Cells(5).Text & " " Else strWhereClause = strWhereClause & GridViewQuery.Rows(intGridRowcnt - 1).Cells(3).Text & " " strWhereClause = strWhereClause & GridViewQuery.Rows(intGridRowcnt - 1).Cells(4).Text & " " strWhereClause = strWhereClause & GridViewQuery.Rows(intGridRowcnt - 1).Cells(5).Text & " " strWhereClause = strWhereClause & GridViewQuery.Rows(intGridRowcnt - 1).Cells(6).Text & " " End If Next

          1 Reply Last reply
          0
          • D dinus28

            what would happen if column name is changed???

            G Offline
            G Offline
            gauthee
            wrote on last edited by
            #6

            if the coumn name changes the code changes......

            Gautham

            1 Reply Last reply
            0
            • D dinus28

              what would happen if column name is changed???

              G Offline
              G Offline
              gauthee
              wrote on last edited by
              #7

              change the code when the column name changes!!!!!

              Gautham

              1 Reply Last reply
              0
              • D dinus28

                hi it is always better to use index than using the name. bcoz if the name is changed u have to modify everywhere wherver u use it. for huge projects it is a very tedious job. my suggestion would be that u better use index.

                M Offline
                M Offline
                mail572352
                wrote on last edited by
                #8

                Thanks for your reply. It is more likely for the column order to be changed rather than the name of the data field because the latter is what the gridview control is bound on. The problem is that if the order of the columns changed, and the types of data were different as they are in my grid, the code would break. Whilst each column is a string value, some of those strings are actually numbers and I use a Parse method to parse them. If these methods tried to parse a string which wasn't actually a number, an exception would be raised. Also, I pass the values to a method. If I use indexing, I have to arrange my parameters according to what position I read them in as, like this:

                obj = new MyObject
                (
                (String)(gvItems.Rows[i].Cells[0]).Text, // Product code
                (String)(gvItems.Rows[i].Cells[1]).Text, // Description
                (String)(gvItems.Rows[i].Cells[6]).Text, // Part Number
                Decimal.Parse((gvItems.Rows[i].Cells[2]).Text), // Sales Price
                Decimal.Parse((gvItems.Rows[i].Cells[3]).Text), // Quantity
                Decimal.Parse((gvItems.Rows[i].Cells[5]).Text), // VAT Rate
                Decimal.Parse((gvItems.Rows[i].Cells[4]).Text) // Discount
                );

                It's far better to use the name if at all possible.

                D 1 Reply Last reply
                0
                • G gauthee

                  how about: gvItems.Rows[i]["column name"].Text

                  Gautham

                  M Offline
                  M Offline
                  mail572352
                  wrote on last edited by
                  #9

                  gauthee wrote:

                  gvItems.Rows[i]["column name"].Text

                  This doesn't compile unfortunately :(

                  1 Reply Last reply
                  0
                  • M mail572352

                    Thanks for your reply. It is more likely for the column order to be changed rather than the name of the data field because the latter is what the gridview control is bound on. The problem is that if the order of the columns changed, and the types of data were different as they are in my grid, the code would break. Whilst each column is a string value, some of those strings are actually numbers and I use a Parse method to parse them. If these methods tried to parse a string which wasn't actually a number, an exception would be raised. Also, I pass the values to a method. If I use indexing, I have to arrange my parameters according to what position I read them in as, like this:

                    obj = new MyObject
                    (
                    (String)(gvItems.Rows[i].Cells[0]).Text, // Product code
                    (String)(gvItems.Rows[i].Cells[1]).Text, // Description
                    (String)(gvItems.Rows[i].Cells[6]).Text, // Part Number
                    Decimal.Parse((gvItems.Rows[i].Cells[2]).Text), // Sales Price
                    Decimal.Parse((gvItems.Rows[i].Cells[3]).Text), // Quantity
                    Decimal.Parse((gvItems.Rows[i].Cells[5]).Text), // VAT Rate
                    Decimal.Parse((gvItems.Rows[i].Cells[4]).Text) // Discount
                    );

                    It's far better to use the name if at all possible.

                    D Offline
                    D Offline
                    dinus28
                    wrote on last edited by
                    #10

                    hi the code varshavmane has sent would be more optimized one. did u try that one.

                    M 1 Reply Last reply
                    0
                    • D dinus28

                      hi the code varshavmane has sent would be more optimized one. did u try that one.

                      M Offline
                      M Offline
                      mail572352
                      wrote on last edited by
                      #11

                      No, I don't want to refer to the index of the columns at all. I need to retrieve the data based on the datafield name defined in the .aspx form.

                      1 Reply Last reply
                      0
                      • M mail572352

                        Hi, I have a gridview containing several rows of multi-column data. I've bound my grid from an ArrayList specifying bound fields on my gridview columns like this:

                        <asp:BoundField HeaderText="Price" DataField="SalesPrice" />

                        I can then read in cell/column data by indexing into the row and indexing to the column I need like this:

                        String str = (gvItems.Rows[i].Cells[0]).Text;

                        Knowing exactly which column it is, is error-prone so what I'd like to do is access my data knowing the datafield name, "SalesPrice". Can anyone suggest a good way of doing this? Do I need to have a template?

                        M Offline
                        M Offline
                        Murthy Puvvada
                        wrote on last edited by
                        #12

                        The best thing i would recommend is as follows: TextBox shorttitle = Bgt_Main.FooterRow.FindControl("txtshorttitle") as TextBox; GridViewRow row= Bgt_Main.SelectedRow/ GridViewRow row = Bgt_Main.Rows[Bgt_Main.SelectedIndex]; TextBox shortt = row.FindControl("short") as TextBox; Label mainid = row.FindControl("lblrow") as Label; and then use: shorttitle.Text/ shorrtt.Text/ mainid.Text so the general syntax would be: = ..... as here "txtshorttitle","short","lblrow" are the ID's of the textbox/label's in the itemtemplate of the girdview/DetailsView. Even if the column name changes like in Bind(..) or Eval(..) you dont have to change the code. I hope this makes everything clear Murthy here

                        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