Need GridView Help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jonathan Wood

    #1

    Need GridView Help

    I'm displaying data in a GridView that allows user to select a row. When a
    button is clicked, I need to locate the selected row.

    The problem is that I need an ID value from the selected row, but I do not
    want to display that ID value in the grid. I tried creating a hidden column,
    but I can't seem to be able to access it when the button is clicked. (At
    least, hidden columns do not appear to show up in the Cells collection of
    the selected row.)

    Is there any way to get an ID for the selected row without displaying that
    ID to the user?

    Also, does anyone know if there's any way to populate a GridView control
    without using the DataSource property, by just programatically adding rows
    to the grid?

    Thanks.

    --
    Jonathan Wood
    SoftCircuits Programming


  • Mark Rae [MVP]

    #2
    Re: Need GridView Help

    "Jonathan Wood" <jwood@softcirc uits.comwrote in message
    news:eXe3ZlkXIH A.3940@TK2MSFTN GP05.phx.gbl...
    Is there any way to get an ID for the selected row without displaying that
    ID to the user?
    When the data is bound to the GridView, populate the button's
    CommandArgument property with the ID...

    However, there's no need to use a button if all you want is the user to be
    able to select a row:

    protected void MyGridView_RowD ataBound(object sender, GridViewRowEven tArgs
    e)
    {
    if (e.Row.RowType == DataControlRowT ype.DataRow)
    {
    e.Row.Style["cursor"] = "pointer";
    e.Row.Attribute s.Add("onclick" ,
    ClientScript.Ge tPostBackEventR eference(MyGrid View, "Select$" +
    e.Row.RowIndex. ToString()));
    }
    }

    protected void MyGridView_Sele ctedIndexChange d(object sender, EventArgs e)
    {
    Response.Redire ct("OtherPage.a spx?ID=" +
    MyGridView.Sele ctedValue.ToStr ing(), false);
    }


    --
    Mark Rae
    ASP.NET MVP


    Comment

    • Jonathan Wood

      #3
      Re: Need GridView Help

      Mark,
      When the data is bound to the GridView, populate the button's
      CommandArgument property with the ID...
      >
      However, there's no need to use a button if all you want is the user to be
      able to select a row:
      >
      protected void MyGridView_RowD ataBound(object sender, GridViewRowEven tArgs
      e)
      {
      if (e.Row.RowType == DataControlRowT ype.DataRow)
      {
      e.Row.Style["cursor"] = "pointer";
      e.Row.Attribute s.Add("onclick" ,
      ClientScript.Ge tPostBackEventR eference(MyGrid View, "Select$" +
      e.Row.RowIndex. ToString()));
      }
      }
      >
      protected void MyGridView_Sele ctedIndexChange d(object sender, EventArgs e)
      {
      Response.Redire ct("OtherPage.a spx?ID=" +
      MyGridView.Sele ctedValue.ToStr ing(), false);
      }
      Yeah, I'll play around with that. But, like I mentioned elsewhere in this
      thread, I don't do any processing when the item is selected. I need to be
      able to access the ID of the selected row when a button outside the grid is
      pressed. It sounds like this is mostly geared towards handling row
      events--unless there's a bit more to this.

      Thanks.

      --
      Jonathan Wood
      SoftCircuits Programming


      Comment

      • Mark Rae [MVP]

        #4
        Re: Need GridView Help

        "Jonathan Wood" <jwood@softcirc uits.comwrote in message
        news:%23lyidCrX IHA.4880@TK2MSF TNGP03.phx.gbl. ..
        Yeah, I'll play around with that. But, like I mentioned elsewhere in this
        thread, I don't do any processing when the item is selected. I need to be
        able to access the ID of the selected row when a button outside the grid
        is pressed.
        In that case, the CommandArgument property is definitely the way to go...


        --
        Mark Rae
        ASP.NET MVP


        Comment

        • Jonathan Wood

          #5
          Re: Need GridView Help

          I'll have to take your word for it. I can't seem to see how CommandArgument
          could be made to work for my purposes.

          --
          Jonathan Wood
          SoftCircuits Programming


          "Mark Rae [MVP]" <mark@markNOSPA Mrae.netwrote in message
          news:%23b8nycrX IHA.1132@TK2MSF TNGP06.phx.gbl. ..
          "Jonathan Wood" <jwood@softcirc uits.comwrote in message
          news:%23lyidCrX IHA.4880@TK2MSF TNGP03.phx.gbl. ..
          >
          >Yeah, I'll play around with that. But, like I mentioned elsewhere in this
          >thread, I don't do any processing when the item is selected. I need to be
          >able to access the ID of the selected row when a button outside the grid
          >is pressed.
          >
          In that case, the CommandArgument property is definitely the way to go...
          >
          >
          --
          Mark Rae
          ASP.NET MVP
          http://www.markrae.net

          Comment

          Working...