【问题标题】:How to use ObjectDataSource with Grid View?如何将 ObjectDataSource 与网格视图一起使用?
【发布时间】:2019-10-26 09:37:18
【问题描述】:

使用对象数据源填充网格视图时,web表单的cs文件中应该做些什么?

我现在正在使用

GridView1.DataSource = OrdersData.GetOrders(txtSearchInOrders.Text);
GridView1.DataBind();

OrdersData 是我的 DAL 类,其中定义了一个用于从 db 检索数据的方法:

public class OrdersData
    {
public static DataSet GetOrders(string searchString)
        {
            string sqlQuery = null;

            if (string.IsNullOrEmpty(searchString))
                sqlQuery = String.Format("GetOrders");
            else
                sqlQuery = String.Format("SearchInOrders");

            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString))
            {
                connection.Open();

                SqlDataAdapter sda = new SqlDataAdapter(sqlQuery, connection);


                sda.SelectCommand.CommandType = CommandType.StoredProcedure;

                if (!string.IsNullOrEmpty(searchString))
                {
                    sda.SelectCommand.Parameters.Add("@SearchString", System.Data.SqlDbType.VarChar);
                    sda.SelectCommand.Parameters["@SearchString"].Value = searchString;
                    sda.SelectCommand.Parameters["@SearchString"].Direction = ParameterDirection.Input;
                }

                DataSet ds = new DataSet();
                sda.Fill(ds);

                return ds;
            }
        }
}

所以当我将我的网格视图与 objectDataSource 关联时,它会为网格视图提供一个 ID,例如 DataSourceID = "ObjectDataSource1"

在 web 表单 cs 文件中我使用的是 DataSource,所以在运行它时抱怨我同时使用 DataSource 和 DataSourceID。 顺便说一句,我正在做这些都是为了实现网格视图排序。

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    从 .aspx 页面中删除 DataSourceID = "ObjectDataSource1"

    根据返回的内容,您可能需要从数据集中获取一个表:

    GridView1.DataSource = OrdersData.GetOrders(txtSearchInOrders.Text).Tables[0];
    

    【讨论】:

      猜你喜欢
      • 2016-02-09
      • 2010-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多