【问题标题】:GridView (Binded to ObjectDataSource) not displaying fields in the selected fields zone in Fields DialogGridView(绑定到 ObjectDataSource)未在“字段”对话框的选定字段区域中显示字段
【发布时间】:2012-09-12 10:43:43
【问题描述】:

我有一个绑定到 ObjectDataSoure 的 Gridview。

ObjectDataSource 使用调用 DAL 方法的业务对象,该方法返回 DataTable 和查询结果。

应用程序在运行时工作,但在设计时有一个问题: 当我使用 GridView 的任务向导 -> 编辑列时:没有字段出现在选定字段文本区域中(所以我不能使用向导来编辑列)。

DAL 方法:

public override DataTable GetAllWorkers()
    {
        using (SqlConnection con = new SqlConnection(this.ConnectionString)) {
            SqlCommand cmd = new SqlCommand("t_Workers_GetAllWorkers", con);
            cmd.CommandType = CommandType.StoredProcedure;
            con.Open();
            return GetTable(cmd);
        } 

    }

    private DataTable GetTable(SqlCommand cmd)
    {

        SqlDataAdapter adp = new SqlDataAdapter();
        adp.SelectCommand = cmd;
        DataTable dt = new DataTable();
        adp.Fill(dt);
        return dt;

    }

BLL 方法:

public static DataTable GetAllWorkers()
    {
        return DataProvider.Instance().GetAllWorkers();
    }

ASPX 网页:

       <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
        SelectMethod="GetAllWorkers" TypeName="BLL.BizImpl"></asp:ObjectDataSource>

    <asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource1" 
        EnableModelValidation="True" Height="156px" Width="270px">
    </asp:GridView>

有人能解释一下问题的原因吗?

   Thanks

【问题讨论】:

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


    【解决方案1】:

    似乎因为您没有在您的 aspx 页面中明确声明您的列,您将无法在向导中看到它们。目前您的 GridView 填充了数据,因为

    AutoGenerateColumns
    

    属性默认为真。您可以将 设置为 false 并自己定义列。然后,您可以更好地控制要在其中显示的内容等。

    【讨论】:

    • 感谢您的回答。当我尝试使用 SqlDataSource 时,所有列在设计时出现在 Selected fields 文本区域中。那么当我使用 ObjectDataSoure 时不会发生这种情况?在 ASPX 文件中手动定义列是我唯一的选择吗?
    • 那为什么不使用SqlDataSource呢?
    • 老实说,我不太清楚为什么它适用于一个而不是另一个。我会尝试手动定义列。如果您能够在 SqlDataSource 中执行此操作而无需定义列,它在哪里存储格式信息,您是否注意到它是否在 aspx 中?
    • 因为不推荐。它将DAL逻辑导入UI。
    • 是的,当我使用 SqlDataSource 时,它​​会自动在 ASPX 文件中生成列信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    • 1970-01-01
    • 2014-08-18
    • 2021-09-21
    相关资源
    最近更新 更多