asp.net gridview索引错误

时间:2015-07-10 13:11:39

标签: asp.net gridview

我有一个工作正常的gridview。 它有一些隐藏的列,然后是可见的列。 当我单击每一行时,后面的VB代码从所选行中的每个列值获取值,并将值放在gridview下面的单独文本框中,例如

txtProjectId.Text = GridView1.SelectedRow.Cells(2).Text.ToString ' project ID

一切正常。

根据要求查找以下gridview代码(缩短版)。

         <div id="div_GRIDVIEW" class="div_Gridview" clientidmode="Static"  runat="server" >

            <asp:GridView ID="GridView1" Width="1370px"  runat="server" AllowPaging="True" AllowSorting="True"  OnSelectedIndexChanged = "OnSelectedIndexChanged"  OnRowDataBound = "OnRowDataBound" 
                           cssclass="STD_GridView L4"  AutoGenerateColumns ="False" DataKeyNames="Id" DataSourceID="SqlDataSource2"   PageSize = "5" GridLines="Horizontal"   EnableViewState="False"  >
                            <rowstyle cssclass="GridRowStyle HoverableRow" />
                            <alternatingrowstyle cssclass="GridAlternateRowStyle HoverableRow" />                                    
                            <PagerStyle BackColor="#56b7e9" ForeColor="White" HorizontalAlign="Center" width="1370px" Height="20px"/>
                            <SelectedRowStyle BackColor="#eeeeee"  />
                            <SortedAscendingHeaderStyle BackColor="#56b7e9" />
                            <SortedDescendingHeaderStyle BackColor="#56b7e9" />     


            <Columns>                                  
                              <asp:CommandField ShowSelectButton="true" SelectImageUrl="~/images/xxstd/update16.gif">                                 
                                  <ItemStyle CssClass="hiddencol" /><HeaderStyle CssClass="hiddencol" />
                              </asp:CommandField>                                                                   

                              <asp:BoundField DataField="Id"  HeaderText="ProjectMainId"><ItemStyle CssClass="hiddencol" /><HeaderStyle CssClass="hiddencol" /> </asp:BoundField>
                              <asp:BoundField DataField="PDID" HeaderText="ProjectDetailsID"><ItemStyle CssClass="hiddencol" /><HeaderStyle CssClass="hiddencol" /> </asp:BoundField>
                              <asp:BoundField DataField="PN"   HeaderText="PCFlag" ><ItemStyle CssClass="hiddencol" /><HeaderStyle CssClass="hiddencol" /> </asp:BoundField>
                              <asp:BoundField DataField="DD"   HeaderText="OCFlag"  ><ItemStyle CssClass="hiddencol" /><HeaderStyle CssClass="hiddencol" /> </asp:BoundField>
                              <asp:BoundField DataField="OFF"   HeaderText="OFFlag"  ><ItemStyle CssClass="hiddencol" /><HeaderStyle CssClass="hiddencol" /> </asp:BoundField>                                  
                              <asp:BoundField DataField="DCF"   HeaderText="DCFlag"  ><ItemStyle CssClass="hiddencol" /><HeaderStyle CssClass="hiddencol" /> </asp:BoundField>                                  
                              <asp:BoundField DataField="OnCF"   HeaderText="OnCFlag"  ><ItemStyle CssClass="hiddencol" /><HeaderStyle CssClass="hiddencol" /> </asp:BoundField>
                              <asp:BoundField DataField="RF"  HeaderText="ReadyFlag"  ><ItemStyle CssClass="hiddencol" /><HeaderStyle CssClass="hiddencol" /> </asp:BoundField>
                              <asp:BoundField DataField="DF" HeaderText="DeclineFlag"  ><ItemStyle CssClass="hiddencol" /><HeaderStyle CssClass="hiddencol" /> </asp:BoundField>
                              ....many more hidden columns here (over 50)...

                               <asp:TemplateField  SortExpression="PDID"  >

                <HeaderTemplate>
                                          <asp:LinkButton ID="lbItem" runat="server"  Text="Project#" CommandName="Sort" CommandArgument="Item"  tabindex="1000"></asp:LinkButton><br />
                                          <asp:TextBox runat="server" ID="txtS_PDID" CssClass="HF"  OnTextChanged="txtItem_TextChanged"></asp:TextBox>                                               
                                   </HeaderTemplate>



                                   <ItemTemplate>

                                            <table  >
                                              <tr >
                                                <td class="STD_normal" style="width:50px;font-weight:bold"><%#Eval("PDID")%></a></td>                                                  
                                              </tr>
                                              <tr>
                                                <td class="STD_Normal_Grey" style="width:50px;"><%#Eval("Id")%> </td>
                                              </tr>
                                            </table>

                                   </ItemTemplate>                                       
                              </asp:TemplateField>


                               <asp:TemplateField SortExpression="Item">

                                    <HeaderTemplate>
                                          <asp:LinkButton ID="lbItem" runat="server"  Text="Project Name" CommandName="Sort" CommandArgument="Item"  tabindex="1000"></asp:LinkButton><br />
                                          <asp:TextBox runat="server" ID="txtS_PDID" CssClass="HF"  OnTextChanged="txtItem_TextChanged"></asp:TextBox>                                               
                                   </HeaderTemplate>


                                        <ItemTemplate>

                                            <table  >
                                              <tr>
                                                <td class="STD_normal" style="width:150px; font-weight:bold"><%#Eval("PN")%>

                                                </td>                                                    
                                              </tr>
                                              <tr>
                                                <td class="STD_Normal_Grey" style="width:150px"><%#Eval("DD", "{0:dd-MMM-yyyy}")%> </td>
                                              </tr>
                                            </table>

                                          </ItemTemplate>                                       
                                   <HeaderStyle HorizontalAlign="Left" />
                              </asp:TemplateField>



                          </Columns>


        </asp:GridView>

        </div>

以下是选择行时代码的一部分......会话中出现索引错误(&#34; txtcurrentID&#34;)= GridView1.SelectedRow.Cells(1).Text.ToString:

 Protected Sub OnSelectedIndexChanged(sender As Object, e As EventArgs) 'Handles gridview1.SelectedIndexChanged 



        Session("txtcurrentID") = GridView1.SelectedRow.Cells(1).Text.ToString 
        Session("txtcurrentPDID") = gridView1.SelectedRow.Cells(2).Text.ToString 
        Session("PCF") = GridView1.SelectedRow.Cells(3).Text.ToString
        Session("OCF") = GridView1.SelectedRow.Cells(4).Text.ToString
        Session("OFF") = GridView1.SelectedRow.Cells(5).Text.ToString
        Session("DCF") = GridView1.SelectedRow.Cells(6).Text.ToString
        Session("OnCF") = GridView1.SelectedRow.Cells(7).Text.ToString
end sub 

所以现在当我点击每一行时,我收到以下错误:

指数超出范围。必须是非负数且小于集合的大小。

一切正常,gridview正确显示,标题过滤器看起来很棒。 但是我不能再单击/选择每一行...... 任何想法??

0 个答案:

没有答案