user2179026
user2179026

Reputation:

Make panel visible inside inside gridview control in asp.net c#

I have written like below lines of code

in aspx file

     <asp:TemplateField HeaderText="Is Active">
                  <ItemTemplate>            
      <asp:LinkButton ID="lnkEdit" CssClass="colorlnkbtnedit" runat="server" ToolTip="Edit" CommandArgument='<%# DataBinder.Eval (Container.DataItem, "ProductDocument") %>'
                   CommandName="EditIsActive"><i class=" icon-pencil"></i>      </asp:LinkButton>

                  <asp:Panel ID="pnlIsActEdit" runat="server" Visible="false">  
  ...
  ...
         </asp:Panel>

                  </ItemTemplate>
                  </asp:TemplateField>

in .cs file

    protected void gvProductView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Download")
        {
         ...
         ... 
        }
     else if (e.CommandName == "EditIsActive")
        {
            int rowID = Convert.ToInt32(e.CommandArgument);
            GridViewRow r = gvProductView.Rows[rowID]; 

            Panel p = (Panel)r.FindControl("pnlIsActEdit");
            p.Visible = false;
        }
     }

It's not working !!! It throws "Index was out of range. Must be non-negative and less than the size of the collection" error message at GridViewRow r = gvProductView.Rows[rowID]; line of code.

Upvotes: 1

Views: 1299

Answers (2)

Tim Schmelter
Tim Schmelter

Reputation: 460138

You exptect the index of the row in the CommandArgument but actually you are initializing it with the ProductDocument of the underyling datasource here:

CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ProductDocument") %>'

So this causes the exception.

Instead use:

 CommandArgument='<%# Container.DataItemIndex %>' 

or

CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'

Upvotes: 1

Ramesh Rajendran
Ramesh Rajendran

Reputation: 38683

Why You got this error?

Simply you have 10 rows only, but if your rowID is greater than 11. then you got his error.


Solution

Please verify your row count and rowID value to solve this issue.


Suggestion

I think you need to get current selected row. if yes, then please see this MSDN Document

Upvotes: 0

Related Questions