【问题标题】:Unable to select my gridview row of data无法选择我的 gridview 数据行
【发布时间】: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,我意识到我选择的索引代码是错误的,导致我出现这个错误。感谢您抽出宝贵时间提供帮助。

标签: c# asp.net gridview


【解决方案1】:

GridView 的 SelectedIndexChanged 代码检测行错误。

【讨论】:

  • 请问gridview中的keyfield到底是什么?
  • 如何填充gridview。
  • 我没有通过 sqldatasource 绑定它。我通过 loadgrid 和 databindbydataset 来完成。
  • 能否分享gridview和SelectedIndexChanged代码的绑定
  • 嘿,谢谢你的想法。我意识到我的 selectedindex 代码完全错误。 gridview 能够绑定数据,但我插入了错误的 selectedindex 代码,导致 gridview 无法理解被选中的行。很抱歉浪费了您的时间
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多