【问题标题】:An issue for Data binding at datagridviewdatagridview 的数据绑定问题
【发布时间】:2012-05-16 13:33:18
【问题描述】:

实际上,说问题可能是错误的。我有 2 页。我可以从第一页获取第二页的查询字符串。这个查询字符串是我查询的关键部分。在调试模式下,我可以看到查询的结果,它们就是我想要的。但它不能显示在我的网格视图上。 这是我的代码块: 我的客户列表页面,

public partial class CustomerList : System.Web.UI.Page
    {
        CustomerBusiness m_CustomerBusiness = new CustomerBusiness();
        COMPANY m_Company = new COMPANY();

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    BindCustomers();
                }
            }

        private void BindCustomers()
                {
                    string CompanyName = Request.QueryString.Get("CompanyName");

                    LabelCompanyName.Text = CompanyName;
                    List<CUSTOMER> CustomerListt = m_CustomerBusiness.SelectByFirmName(CompanyName);
                    GridViewCustomerList.DataSource = CustomerListt;
                    GridViewCustomerList.DataBind();
                }
}

GridViewCustomerList:

 <asp:GridView ID="GridViewCustomerList" runat="server" 
            AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" 
            BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" 
            ondatabinding="GridViewCustomerList_DataBinding" 
            onselectedindexchanged="GridViewCustomerList_SelectedIndexChanged" 
            Width="239px">
            <AlternatingRowStyle BackColor="#DCDCDC" />
            <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
            <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
            <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
            <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F1F1F1" />
            <SortedAscendingHeaderStyle BackColor="#0000A9" />
            <SortedDescendingCellStyle BackColor="#CAC9C9" />
            <SortedDescendingHeaderStyle BackColor="#000065" />
        </asp:GridView>

CustomerList 是我想要的,但我的绑定部分不起作用,我在运行项目时看不到 GridViewCustomerList。我研究了一些asp.net页面生命周期模型,目标解决方案可能与此有关。

【问题讨论】:

  • 你能显示你的GridView的标记吗?
  • 如果我能看到,我会展示它.. 但正如我所说,我可以在调试模式下看到查询结果..
  • 抱歉,我指的是 GridViewCustomerList 的标记,因为它出现在您的 ASPX 页面(在 Visual Studio 中)。请在您的问题中添加那个
  • 没问题,但我想你还是误会了我 =) 在你的 ASPX 页面中,我想看到你的 GridView 声明。它应该以这样的开头:&lt;asp:GridView runat="server" ID="GridViewCustomerList"&gt;。然后使用edit 按钮将该内容添加到您的问题中。
  • 我注意到我仍然误解你并在你之前删除我的评论 =) 好的。我加了。

标签: c# asp.net data-binding gridview page-lifecycle


【解决方案1】:

这里的问题是您的标记中没有明确的&lt;Column&gt; 声明,并且您将AutoGenerateColumns property 设置为“false”。因此,即使数据绑定到您的 GridView 控件,它也不知道要显示什么(因此它什么也不显示。

这里最简单的解决方案是从您的 GridView 声明中删除 AutoGenerateColumns="False"(默认情况下它是“true”),您应该一切顺利。它会根据您的数据源自动创建您的列。

或者,您可以通过将列部分添加到 GridView 来指定所需的列。

    <columns>
      <asp:boundfield datafield="columnOne" headertext="Column 1"/>
      <asp:boundfield datafield="columnTwo" headertext="Column 2"/>
      <asp:boundfield datafield="columnThree" headertext="Column 3"/>
    </columns>
</asp:GridView>

【讨论】:

  • 非常感谢@jadarnel27。我仍然不敢相信问题的解决方案只是改变'假'=)
  • @yiddow 有时候就是这么简单 =)
猜你喜欢
  • 1970-01-01
  • 2012-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多