如何将对象列表传递给gridview数据源而不生成列错误

时间:2017-11-23 11:29:42

标签: c# asp.net sql-server sqldatasource

我执行基本的SQL查询以将数据提供给Gridview排序事件。

public DataTable GetDataDataTableGeneric()
{
    string query = "SELECT * FROM Gamer";

    SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["AzureServer"].ConnectionString);
    conn.Open();

    SqlCommand cmd = new SqlCommand(query, conn);

    DataTable dt = new DataTable();
    dt.Load(cmd.ExecuteReader());

    conn.Close();
    return dt;
}

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
    // string query= e.SortExpression + " " + GetSortDirection(e.SortExpression);
    // e.SortExpression;
    DataTable dt = GetDataDataTableGeneric();
    dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);

    List<DataRow> list = dt.AsEnumerable().ToList();

    GridView1.DataSourceID = null;

    GridView1.DataSource = list;
    GridView1.DataBind();
}

由于数据源不接受不可枚举的对象,我决定将数据表转换为列表。

然而,我收到此错误:

  

&#39;的System.Data.DataRow&#39;不包含名称为&#39; Id&#39;。

的媒体资源

有没有办法生成列表并避免错误?

更新 功能:

        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {

        DataTable dt = GetDataDataTableGeneric();
        dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
    //    List<Gamer> list = dt.AsEnumerable().ToList();
        List<Gamer> list = (from DataRow row in dt.Rows
         select new Gamer
         {
             //assign properties here
             Id = Convert.ToInt32(row["Id"]),
             UserID = Convert.ToInt32(row["UserID"]),
             Level = Convert.ToInt32(row["Level"]),
             Loyalty = Convert.ToInt32(row["Loyalty"]),
             Experience = Convert.ToInt32(row["Experience"]),
             OverallPoints = Convert.ToInt32(row["OverallPoints"]),
             OverallBets = Convert.ToInt32(row["OverallBets"]),
             Login = row["Login"].ToString()

         }).ToList();

        GridView1.DataSourceID = null;
        GridView1.DataSource = list;
        GridView1.DataBind();

    }

错误

  

数据源不支持排序。

以下解决方案也失败了。导致相同的排序问题。

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {

       DataTable dt = GetDataDataTableGeneric();
        dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
        List<Gamer> list = (from DataRow row in dt.Rows
         select new Gamer
         {
             //assign properties here
             Id = Convert.ToInt32(row["Id"]),
             UserID = Convert.ToInt32(row["UserID"]),
             Level = Convert.ToInt32(row["Level"]),
             Loyalty = Convert.ToInt32(row["Loyalty"]),
             Experience = Convert.ToInt32(row["Experience"]),
             OverallPoints = Convert.ToInt32(row["OverallPoints"]),
             OverallBets = Convert.ToInt32(row["OverallBets"]),
             Login = row["Login"].ToString()

         }).ToList();
        DataTable dt2 = ConvertToDataTable(list);
        GridView1.DataSourceID = null;
        GridView1.DataSource = dt2;
        GridView1.DataBind();

    }


    private DataTable ConvertToDataTable<T>(IList<T> data)
    {
        PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
        DataTable table = new DataTable();
        foreach (PropertyDescriptor prop in properties)
        {
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
        }
        foreach (T item in data)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
            {
                row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
            }
            table.Rows.Add(row);
        }
        return table;
    }

以下解决方案也无效:

        string cmd = "SELECT * FROM Gamer ORDER BY "+ e.SortExpression + " " + GetSortDirection(e.SortExpression);
        DataSet ds = new DataSet();
        using (SqlConnection connection = new SqlConnection(connectionString))
        {

            SqlDataAdapter sqlAdapter = new SqlDataAdapter(cmd, connection);

            sqlAdapter.Fill(ds);
        }
        GridView1.DataSource = ds;
        GridView1.DataBind();

3 个答案:

答案 0 :(得分:0)

这没有任何意义,将其转换为List<DataRow>并且您没有将其转换为List<Gamer>实际上取得了什么成果,因此这种方式无法正常工作。

您可以做的是直接将其与数据表绑定:

GridView1.DataSource = dt;
GridView1.DataBind();

或者反过来创建一个对你的桌子来说是DTO的类:

public class Gamer
{
   public int Id {get;set;}
}

然后是您的代码,您可以从List<Gamer>或从数据访问创建DataTable,您可以直接返回List<Gamer>

如果您遵循第一种方法,您的方法代码将如下所示:

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{

    DataTable dt = GetDataDataTableGeneric();
    dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);

    GridView1.DataSource = dt;
    GridView1.DataBind();
}

希望它有所帮助。

答案 1 :(得分:0)

您可以使用多个选项,但最简单的选项

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<body> 
  <h2>Hello <b th:text="${name}"></b> - THYMELEAF</h2>
</body>
</html>    

或者您可以使用以下函数从Query动态创建表,然后将其分配给GridView

    DataTable dt = GetDataDataTableGeneric();
    dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
GridView1.DataSource = dt.DefaultView.ToTable();
    GridView1.DataBind();

答案 2 :(得分:0)

奇怪的是,它可以让你所需要的只是放一个空的事件处理程序。

 protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {}