Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6366247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:23:49+00:00 2026-05-25T00:23:49+00:00

I have a GridView which I bind to a SqlDataReader on Page_Load. It has

  • 0

I have a GridView which I bind to a SqlDataReader on Page_Load. It has a column with buttons and I am trying to get the row when a button is clicked with the following code:

int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];

EDIT : Pasting the .aspx page from the comments sectio

 <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" OnRowCommand="GridView1_RowCommand" DataKeyNames="id" GridLines="None"> <AlternatingRowStyle BackColor="White" /> 
    <Columns> 
        <asp:TemplateField> 
             <ItemTemplate> 
                  <asp:Button ID="btnChange" runat="server" Text="Change" CommandName="Test" Visible='<%# Convert.ToBoolean(Eval("Tested")) == true ? true : false %>' /> 
             </ItemTemplate> 
         </asp:TemplateField> 
     </Columns>
</aspx:GridView> 

I get the following error: ‘System.FormatException: Input string was not in a correct format.’ on line ‘int index = Convert.ToInt32(e.CommandArgument);’.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:23:50+00:00Added an answer on May 25, 2026 at 12:23 am

    You need to check which command in the GridView row has been clicked. Your markup should correspondingly map. See egs below.
    The e.CommandArgument you are getting may not correspond to your button click.

    In CodeBehind:

        void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
        {
        // If multiple buttons are used in a GridView control, use the CommandName property to determine which button was clicked.
        if(e.CommandName=="Add")
        {
        // Convert the row index stored in the CommandArgument property to an Integer.
        int index = Convert.ToInt32(e.CommandArgument);
    
        // Retrieve the row that contains the button clicked by the user from the Rows collection.
        GridViewRow row = CustomersGridView.Rows[index];
    
        // additional logic...
        }
        // additional logic...
    }
    

    In Markup:

    Also please ensure you have set your CommandArgument attribute appropriately. Example below:

    <asp:Button (...) CommandArgument="<%# Container.DataItemIndex %>" />
    

    OR use a buttonfield

    <asp:ButtonField ButtonType="button" CommandName="Add" Text="Add" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have GridView which I can select a row. I then have a button
I have a GridView in which each row has a custom view. The grid
I have a GridView to which I bind a dataTable that I manually created.
I have a gridview which is databound, I want to dynamically add a row
In my .aspx page, I have an <asp:GridView runat=server ID=CustomerGridView> control which I bind
Hi have a repeater which is inside a gridview. when i bind the data
I have a gridview which i bind to a datatable. After i bind i
I have a GridView on an ASP.NET page that I'm trying to bind to
I have a GridView, which features a a checkbox on each row (in a
I have a gridview which contains few Template columns, In the first Template column

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.