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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. get value from Gridview

get value from Gridview

Scheduled Pinned Locked Moved Database
questiondatabasehelp
4 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
    sbao004
    wrote on last edited by
    #1

    Hi, I want to get the value of "OrderID", which is the first colum in the Gridview2, from the Gridview when a specific row is selected with hard coding. My code is: Session("OrderID") = Convert.ToString(CType(GridView2.Rows(GridView2.EditIndex).FindControl("Label4"), Label).Text) I've also tried to use GridView2.SelectedIndex, which always returns -1, which is an invaliad index value. or GridView2.SelectedRow.Cells(1).Text but it seems the SelectedRow is always nothing. So I don't know what is wrong with me. Can anyone help please? Thank you.

    J 1 Reply Last reply
    0
    • S sbao004

      Hi, I want to get the value of "OrderID", which is the first colum in the Gridview2, from the Gridview when a specific row is selected with hard coding. My code is: Session("OrderID") = Convert.ToString(CType(GridView2.Rows(GridView2.EditIndex).FindControl("Label4"), Label).Text) I've also tried to use GridView2.SelectedIndex, which always returns -1, which is an invaliad index value. or GridView2.SelectedRow.Cells(1).Text but it seems the SelectedRow is always nothing. So I don't know what is wrong with me. Can anyone help please? Thank you.

      J Offline
      J Offline
      Jay_se
      wrote on last edited by
      #2

      Hi, I didnt find any problem with your code. Which Event handler did you used? The below is sample code in C#.For your help[^] protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { Label1.Text = GridView1.SelectedRow.Cells[2].Text; int index = GridView1.SelectedIndex; } But , if you are used this code in SelectedIndexChanging event handler, it won't give correct value.

      Regards, Jay :)

      S 1 Reply Last reply
      0
      • J Jay_se

        Hi, I didnt find any problem with your code. Which Event handler did you used? The below is sample code in C#.For your help[^] protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { Label1.Text = GridView1.SelectedRow.Cells[2].Text; int index = GridView1.SelectedIndex; } But , if you are used this code in SelectedIndexChanging event handler, it won't give correct value.

        Regards, Jay :)

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

        I convered the image button in the Gridview into the Templet, and used the ImageButton1_Click event handler. I think because when you select the row, it will change the row index to "-1", which does not really exist in the GridView, By using: Session("OrderID") = Convert.ToString(CType(GridView2.Rows(GridView2.SelectedIndex).FindControl("Label4"), Label).Text) it keeps on throw me the error that "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" Otherwise if I use: Session("OrderID") = Convert.ToString(CType(GridView2.SelectedRow.FindControl("Label4"), Label).Text) It will say " Object reference not set to an instance of an object." So it just doesn't work. I don't know how to use these 2 properties. Can you please help? Thanks,

        J 1 Reply Last reply
        0
        • S sbao004

          I convered the image button in the Gridview into the Templet, and used the ImageButton1_Click event handler. I think because when you select the row, it will change the row index to "-1", which does not really exist in the GridView, By using: Session("OrderID") = Convert.ToString(CType(GridView2.Rows(GridView2.SelectedIndex).FindControl("Label4"), Label).Text) it keeps on throw me the error that "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" Otherwise if I use: Session("OrderID") = Convert.ToString(CType(GridView2.SelectedRow.FindControl("Label4"), Label).Text) It will say " Object reference not set to an instance of an object." So it just doesn't work. I don't know how to use these 2 properties. Can you please help? Thanks,

          J Offline
          J Offline
          Jay_se
          wrote on last edited by
          #4

          sbao004 wrote:

          when you select the row, it will change the row index to "-1"

          Your SelectedRow property of GridView may not give you the exact Row which is selected currently ( in SelectedIndexChanging event handler, ofcourse which fires first when you select the Row). Have a look at NewSelectedIndex Property[^] What are the GridView event handlers did you used?

          Regards, Jay :)

          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