.NET - SelectedRow not returning any value

2

I am new with .net!

I have two gridviews connected to a large database. The first one is returning a list of issues searched by ID while the other is returning the issues searched by subject.

I am trying to get the ID from the gridview returning issues from a select button but when I use selectedRow it doesn't return anything.

I tried multiple methods and this is what I have now. Any Suggestions?

    Protected Sub IssuesGV_SelectedIndexChanging(ByVal sender As Object, ByVal e As GridViewSelectEventArgs)
        Dim pName As String
        pName = IssuesGV.SelectedRow.Cells(0).Text
        BindGridComments(pName)
    End Sub

    Protected Sub IssuesGV_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Attributes("onmouseover") = "this.style.backgroundColor='aquamarine';"
            e.Row.Attributes("onmouseout") = "this.style.backgroundColor='white';"
            e.Row.ToolTip = "Click last column for selecting this row."
            ' e.Row.Cells(0).Attributes.Add("onclick", )
        End If
    End Sub
Protected Sub IssuesGV_RowCommand(sender As Object, e As GridViewCommandEventArgs)


        ' ' Dim row As GridViewRow = IssuesGV.Rows(rowIndex)

        '     v = row.Cells(1).Text
        'v = IssuesGV.SelectedRow.Cells(0).Text
        ' TextBox1.Text = v
        'TextBox1.Text = v

        If (e.CommandName = "Select1") Then
            Dim index As Int16
            index = Convert.ToInt32(e.CommandArgument)
            Dim row As GridViewRow
            row = IssuesGV.Rows(index)
            Dim item As ListItem
            item.Text = Server.HtmlDecode(row.Cells(0).Text)
        End If




    End Sub

My gridview code is the following (the one where I am using the select button):

 <asp:GridView ID="IssuesGV" runat="server" AutoPostBack="true" OnRowCommand ="IssuesGV_RowCommand" OnRowDataBound="IssuesGV_RowDataBound" OnSelectedIndexChanged = "IssuesGV_OnSelectedIndexChanged" SelectedIndexChaning ="IssuesGV_SelectedIndexChanging" AutoGenerateColumns="False" DataKeyNames="number" DataSourceID="IssueDS" EnableModelValidation="True">
            <Columns>
                <asp:BoundField DataField="number" HeaderText="number" ReadOnly="True" SortExpression="number" />
                <asp:BoundField DataField="subject" HeaderText="subject" SortExpression="subject" />
                <asp:BoundField DataField="description" HeaderText="description" SortExpression="description" />
                <asp:BoundField DataField="created_at" HeaderText="created_at" SortExpression="created_at" />
                <asp:BoundField DataField="opener_name" HeaderText="opener_name" SortExpression="opener_name" />
                <asp:BoundField DataField="project_name" HeaderText="project_name" SortExpression="project_name" />
                <asp:ButtonField Text="Select" CommandName="Select1" ItemStyle-Width="30" ButtonType="Button" HeaderText="Select" ShowHeader="True" SortExpression="number"  >
                <ItemStyle Width="30px" />
                </asp:ButtonField>
            </Columns>
        </asp:GridView>

The error I am receiving is this one:

System.ArgumentOutOfRangeException HResult=0x80131502
Message=Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index Source= StackTrace:

Many Thanks!

asp.net
.net
gridview
asked on Stack Overflow Dec 21, 2018 by Giada Littara

1 Answer

1

Front End:

Your GridView should look like this:

<asp:GridView ID="IssuesGV" runat="server" AutoGenerateColumns="false" 
    OnSelectedIndexChanged="IssuesGV_OnSelectedIndexChanged">
    <Columns>
        <asp:BoundField DataField="number" HeaderText="number" />

            ...Some Other Fields

        <asp:ButtonField Text="Select" CommandName="Select" ItemStyle-Width="150" />
    </Columns>
</asp:GridView>

Back End:

Then add this code OnSelectedIndexChanged of GridView:

Protected Sub IssuesGV_OnSelectedIndexChanged(sender As Object, e As EventArgs)
    'Accessing Selected BoundField Column
    Dim number As String = IssuesGV.SelectedRow.Cells(0).Text

    label.Text = "<b>Number Value:</b> " & number & " <b>"
End Sub

Ref: See full example here.


Edit

For some reason, if OnSelectedIndexChanged method is not firing then you've just need to add below attribute in your GridView header markup:

AutoGenerateSelectButton="True"

This will create a Select link in your GridView rows, which'll fire the OnSelectedIndexChanged method.

PS: If above all workarounds not works then see this post.

answered on Stack Overflow Dec 25, 2018 by 5377037 • edited Jan 10, 2019 by 5377037

User contributions licensed under CC BY-SA 3.0