【发布时间】:2013-09-04 10:46:09
【问题描述】:
我在我的asp.net webapp 中添加了一个gridview。我还在gridview 中添加了一个选择按钮。但是,当我单击 gridview 中的选择按钮时,我收到此错误
这是我的网格视图代码
<asp:GridView ID="gvnric" runat="server" Align="Center" Width="30%"
BackColor="#CCCCCC" AllowSorting="true" OnSorting="gvnric_Sorting"
BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4"
CellSpacing="2" ForeColor="Black" AutoGenerateSelectButton="True"
OnSelectedIndexChanged="gvnric_SelectedIndexChanged" AllowPaging="True"
PageSize="5" OnPageIndexChanging="gvnric_PageIndexChanging">
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<RowStyle BackColor="White" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
我的 gridview 代码的这种格式适用于我的其他页面,但不适用于我当前的页面。为什么会这样?
这就是我使用 loadgrid 将我的数据绑定到 gridview 的方式。我使用这个 loadgrid 进行分页
private void LoadGrid()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
//conn.ConnectionString = "Data Source = localhost; Initial Catalog = MajorProject; Integrated Security= SSPI";
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("Select nric as [NRIC] from MemberAccount Where status = 'unverified'", conn);
da.Fill(ds);
gvnric.DataSource = ds.Copy();
gvnric.DataBind();
conn.Close();
}
我还使用 DataBindByDataSet 重新绑定它。这次是用于gridview排序
private DataTable DataBindByDataSet()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
//conn.ConnectionString = "Data Source = localhost; Initial Catalog = MajorProject; Integrated Security= SSPI";
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("Select nric as [NRIC] from MemberAccount Where status = 'unverified'", conn);
da.Fill(ds);
conn.Close();
return ds.Tables[0];
}
我所有的其他页面也使用了 2 种不同的绑定方式,它们有助于分页和排序,但不适用于这个 gridview。 loadgrid 和 databindbydataset 的绑定代码实际上是相同的,但每个都有不同的用途。 我发现这个link 提出了类似的问题,但没有帮助。
【问题讨论】:
-
据我回忆,
Select$0表示它正在尝试选择第 0 行,我不确定如果没有任何其他数据,选择按钮将如何显示,网格中有行吗?跨度> -
是的。虽然行中有数据。否则我将无法选择并提示错误。
-
试试
gvnric.DataSource = ds.Tables[0]; -
我刚刚意识到我的问题,很抱歉占用了您的时间。感谢@Samiey Mehdi,我意识到我选择的索引代码是错误的,导致我出现这个错误。感谢您抽出宝贵时间提供帮助。