我想从gridview中获取值,具体取决于我选择的行。 我收到错误"索引超出范围。必须是非负数且小于集合的大小。参数名称:index"
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>
答案 0 :(得分:0)
您似乎在错误的EventHandler中尝试以下示例
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 Property