我在我的asp.net webapp 中添加了一个gridview。我还在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 的绑定代码实际上是相同的,但每个都有不同的用途。我发现这个链接问了类似的问题,但它没有帮助。