开发者

Index was out of range. Must be non-negative and less than the size of the collection

i am getting this error while tracking my datakey.. i am not sure why my datakey index is coming out of range.. here is my code :

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            var dataKeys = GridView1.DataKeys[e.Row.RowIndex];
            dataKeys.Value.ToString();
            Label1.Text = "The key field value for the displayed record is " + dataKeys.Value.ToString();
        }

Header Part Of the GridView:

        </ItemTemplate>

        </asp:TemplateField>
    </Columns>
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" 
         />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <EditRowStyle BackColor="#999999" />
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>

<script language="javascript" type="text/javascript">
    function viewProfile(selectedRowIndex, firstName) {
        //alert(firstName);
        //var customGridView = document.getElementById('<%= CustomGridView.ClientID %>');
        var customGridView = document.getElementById('GridView1');
        if (customGridView) {
            var row = customGridView.rows[parseInt(selectedRowIndex) + 1];
            //var cell = customGridView.rows[parseInt(selectedRowIndex) +   1].cells[0];
            //            alert(row.cells[0].innerHTML);
            //            alert(row.cells[1].innerHTML);
            //            alert(row.cells[2].innerHTML);
            //            alert(row.cells[3].innerHTML);
            /开发者_运维问答/            alert(row.cells[4].innerHTML);
            window.open('detatil.aspx?coaid=' + row.cells[1].innerHTML + '&fnomia=' + row.cells[2].innerHTML, "", "scrollbars=no,height=300,width=300");
        }
    }
</script>

The error is: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index


If I remember correctly, that event is fired for header and other non-data-rows. There is a property to know the type of the row. The index of non-data-rows is -1. Add this to check the type of the row:

 if (e.Row.RowType == DataControlRowType.DataRow) {
    // do something
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜