Button in a GridView row

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • obs
    New Member
    • Oct 2007
    • 8

    Button in a GridView row

    Hi.
    I have read a previous post in the subject, but could not find an answer, so my question is how do I find the SelectedIndex of a row in a gridview row where there is a command button ?

    The set up is as follows :

    I have a gridview with a title column and a button column (template column). I want the button click event to do something when the title column in the row of the button that is clicked is not empty.

    Many Thanks ...
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Do the buttons get their own unique id? (like "button1", "button2", "button3")?
    If so, in the event function there should be a "sneder" object, you should be able to use that to determine which button was clicked. And possibly determine the row that has that button?
    i.e. if button0 is clicked you know row 0 is the row you want.

    Comment

    • obs
      New Member
      • Oct 2007
      • 8

      #3
      Originally posted by Plater
      Do the buttons get their own unique id? (like "button1", "button2", "button3")?
      If so, in the event function there should be a "sneder" object, you should be able to use that to determine which button was clicked. And possibly determine the row that has that button?
      i.e. if button0 is clicked you know row 0 is the row you want.
      the buttons do not have a unique id. it is set as a template, where each button has the same id : "addButton" . how do I give a button in a gridview row a unique id ? is it even possible ?

      Comment

      • nateraaaa
        Recognized Expert Contributor
        • May 2007
        • 664

        #4
        You can loop through the rows of the gridview and use the FindControl method to find the button you want to alter using the id you set in the TemplateField.

        for (int i = 0; i < gvResults.Rows. Count; i++)
        {
        Button btn = (Button)GridVie w1.Rows[i].FindControl("b tnAdd");
        //Change the property settings for the button in your TemplateField
        btn.Visible = false;
        }

        Hope this helps.

        Nathan

        Comment

        • obs
          New Member
          • Oct 2007
          • 8

          #5
          Originally posted by nateraaaa
          You can loop through the rows of the gridview and use the FindControl method to find the button you want to alter using the id you set in the TemplateField.

          for (int i = 0; i < gvResults.Rows. Count; i++)
          {
          Button btn = (Button)GridVie w1.Rows[i].FindControl("b tnAdd");
          //Change the property settings for the button in your TemplateField
          btn.Visible = false;
          }

          Hope this helps.

          Nathan
          Dear Nathan,
          thanks for the reply, but how can I use your code to perform certain actions when the button is clicked ?
          The "btnAdd" ID is the same for all the buttons in all the rows, since I'm using a template.
          I need is to use a certain Select property (like SelectedIndex) to retrieve the row of the clicked button, and to put all the code in the button on click event.
          But the following code, which I put in the on click event, does not work :

          Dim grd As GridView = FormView1.FindC ontrol("myGridV iew")
          Dim row As GridViewRow = grd.Rows(grd.Se lectedIndex)
          Dim str As String = row.cells(colum n_index).Text.T oString()

          What's wrong with it ?

          Comment

          • nateraaaa
            Recognized Expert Contributor
            • May 2007
            • 664

            #6
            Dear Nathan,
            thanks for the reply, but how can I use your code to perform certain actions when the button is clicked ?
            The "btnAdd" ID is the same for all the buttons in all the rows, since I'm using a template.
            I need is to use a certain Select property (like SelectedIndex) to retrieve the row of the clicked button, and to put all the code in the button on click event.
            But the following code, which I put in the on click event, does not work :

            Dim grd As GridView = FormView1.FindC ontrol("myGridV iew")
            Dim row As GridViewRow = grd.Rows(grd.Se lectedIndex)
            Dim str As String = row.cells(colum n_index).Text.T oString()

            What's wrong with it ?[/QUOTE]

            Try this.
            Add a CommandName to your button like CommandName = "Add".
            Then in the RowCommand event use an if statement to see if the CommandName is = to "Add".
            If it is use your code above

            Like this
            [CODE=cpp]protected void myGridView_RowC ommand(object sender, GridViewCommand EventArgs e)
            {
            if(e.CommandNam e == "Add")
            {
            GridView grd = FormView1.FindC ontrol("myGridV iew");
            GridViewRow row = grd.Rows[grd.SelectedInd ex];
            string str = row.cells[column_index].Text.ToString( );
            }
            }[/CODE]
            Let me know if this works for you. If you need a conversion tool you can use this site.

            Nathan

            Comment

            Working...