4 Replies - 2498 Views - Last Post: 13 September 2012 - 09:30 AM Rate Topic: -----

#1 dbca  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-September 12

How to retrieve data from gridviewrow

Posted 06 September 2012 - 07:55 PM

I cannot access the data I have assigned to a gridviewrow.
The following code will not work:

protected void QueryPaneResults_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "SelectionData")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = QueryPaneResults.Rows[index];
            Label1.Text = row.Cells[1].Text;
        }
    }



Does anybody know why this causes the following error:

Specified argument was out of the range of valid values.
Parameter name: index

Is This A Good Question/Topic? 0
  • +

Replies To: How to retrieve data from gridviewrow

#2 dbca  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-September 12

Re: How to retrieve data from gridviewrow

Posted 06 September 2012 - 08:08 PM

the following is in the ItemTemplate:

CommandName="SelectionData"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
Was This Post Helpful? 0
  • +
  • -

#3 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 445
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: How to retrieve data from gridviewrow

Posted 07 September 2012 - 08:57 PM

can you show the code for the entire GridView?
Was This Post Helpful? 0
  • +
  • -

#4 dbca  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-September 12

Re: How to retrieve data from gridviewrow

Posted 11 September 2012 - 04:42 PM

View PostNakor, on 07 September 2012 - 08:57 PM, said:

can you show the code for the entire GridView?


Thanks for your reply Nakor! I've found that the code works fine until I add the autogeneratecolumns="false"

Here is the GridView Code:

    <asp:GridView ID="QueryPaneResults" runat="server" AllowPaging="True" DataSourceID="QueryData" OnRowCommand="QueryPaneResults_RowCommand" AutoGenerateColumns="false" >
        <Columns>
            <asp:ButtonField ButtonType="Button" CommandName="Selection" DataTextField="OrgName" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="QueryData" runat="server" ConnectionString="<%$ ConnectionStrings:dbca %>" SelectCommand="SELECT * FROM [Organization]" ></asp:SqlDataSource>



The following is the rowcommand event:

    protected void QueryPaneResults_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Selection")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = QueryPaneResults.Rows[index];
            TableCell NameTableCell = row.Cells[0];
            TableCell PhoneTableCell = row.Cells[5];
            string Name = NameTableCell.Text;
            string Phone = PhoneTableCell.Text;

            Label1.Text = Name;
            Label2.Text = Phone;
        }
    }



The two renders the following error:

Specified argument was out of the range of valid values.
Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index

Source Error:


Line 111: int index = Convert.ToInt32(e.CommandArgument);
Line 112: GridViewRow row = QueryPaneResults.Rows[index];
Line 113: TableCell NameTableCell = row.Cells[3];
Line 114: TableCell PhoneTableCell = row.Cells[5];
Line 115: string Name = NameTableCell.Text;

Line 113 is where the error begins.

Thanks for your help!!
Was This Post Helpful? 0
  • +
  • -

#5 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1182
  • View blog
  • Posts: 1,677
  • Joined: 24-August 11

Re: How to retrieve data from gridviewrow

Posted 13 September 2012 - 09:30 AM

I'm not really familiar with using this method of selecting a row, but don't you have to specify the CommandArgument property of the button in order to use it?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1