网格视图:索引超出范围.必须是非负数和更少

本文关键字:是非 视图 索引 范围 网格 | 更新日期: 2023-09-27 18:33:02

我想根据我选择的行从我的网格视图中获取值。我收到错误"索引超出范围。必须是非负数且小于集合的大小。参数名称:索引"

   protected void OnSelectedIndexChanged(object sender, EventArgs e)
        {
            string name = GridView1.SelectedRow.Cells[0].Text;
        } 
/*---------------------------------------------------------------------------*/
   <asp:GridView ID="GridView1" 
     OnSelectedIndexChanged = "OnSelectedIndexChanged"  
 autogeneratecolumns="false" runat="server">
    <Columns>
  <asp:BoundField DataField="ESS_ID" HeaderText="ID" Visible=false/>
  <asp:BoundField DataField="ESS_Pay_Dt" HeaderText="Pay Date" DataFormatString="{0:d}"    
    HtmlEncode="false" ItemStyle-Width="100" HeaderStyle-BorderColor=navy ItemStyle-BorderColor="navy" ItemStyle-HorizontalAlign="center"/>
  <asp:BoundField DataField="ESS_Emp_Name" HeaderText="Employee Name" ItemStyle-Width="250" HeaderStyle-BorderColor=navy ItemStyle-BorderColor="navy" ItemStyle-HorizontalAlign="center"/>
  <asp:BoundField DataField="ESS_Emp_Num" HeaderText="Number" ItemStyle-Width="75" HeaderStyle-BorderColor=navy ItemStyle-BorderColor="navy" ItemStyle-HorizontalAlign="center"/>
  <asp:BoundField DataField="ESS_Emp_Dept" HeaderText="Department" ItemStyle-Width="100" HeaderStyle-BorderColor=navy ItemStyle-BorderColor="navy" ItemStyle-HorizontalAlign="center"/>
  <asp:BoundField DataField="ESS_Pay_End_Dt" HeaderText="Pay End Date" Visible="false" />
  <asp:BoundField DataField="ESS_Net_Amt" HeaderText="Net Amount" ItemStyle-Width="150" HeaderStyle-BorderColor=navy ItemStyle-BorderColor="navy" ItemStyle-HorizontalAlign="center"/>
  <asp:BoundField DataField="ESS_Total_Earnings1" HeaderText="Total Earnings1" Visible="false"/>
  <asp:BoundField DataField="ESS_Total_Earnings2" HeaderText="Total Earnings2" Visible="false"/>
  <asp:BoundField DataField="ESS_Total_Taxes1" HeaderText="Total Taxes1" Visible="false"/>
  <asp:BoundField DataField="ESS_Total_Taxes2" HeaderText="Total Taxes2" Visible="false"/>
  <asp:BoundField DataField="ESS_Total_Deductions1" HeaderText="Total Deductions1" Visible="false"/>
  <asp:BoundField DataField="ESS_Total_Deductions2" HeaderText="Total Deductions2" Visible="false"/>
  <asp:BoundField DataField="ESS_Net_Pay1" HeaderText="Net Pay1" Visible="false"/>
  <asp:BoundField DataField="ESS_Net_Pay2" HeaderText="Net Pay2" Visible="false"/>
  <asp:BoundField DataField="ESS_Vac_Hrs" HeaderText="Vacation Hours" Visible="false"/>
  <asp:BoundField DataField="ESS_Sck_Hrs" HeaderText="Sick Hours" Visible="false" />
  <asp:BoundField DataField="ESS_Batch_Num" HeaderText="Batch Number" Visible="false"/>
  <asp:BoundField DataField="ESS_Load_Count" HeaderText="Load Count" Visible="false"/>
  <asp:BoundField DataField="ESS_Load_Dt" HeaderText="Load Date" Visible="false"/>
    <asp:ButtonField Text="Select" CommandName="Select" ItemStyle-Width="150" />  
        </Columns>
    </asp:GridView>

网格视图:索引超出范围.必须是非负数和更少

您似乎在错误的事件处理程序中,请尝试以下示例

private void GridView1_SelectionChanged(object sender, EventArgs e)
{
   if (GridView1.SelectedCells.Count > 0)
   {
     int selectedrowindex = GridView1.SelectedCells[0].RowIndex;
     DataGridViewRow selectedRow = GridView1.Rows[selectedrowindex];  
     name = Convert.ToString(selectedRow.Cells["Cell Zero's Name goes here"].Value);           
   }
}

如果要在此方法之外使用name局部变量,则需要string name在本地范围之外声明

例如,在类级别将其声明为以下内容,然后您可以全局使用它

public static string name = string.Empty;

您还可以签出一些其他方法/事件 DataGrid.SelectedItem 属性