2

我有很多这样的工作,并使用这种技术创建了一个下拉列表和一个树视图。用于动态创建控件并将它们返回到使用 jqueryajax 和 C# 完全构建和配置的页面的 ajax 调用。但我坚持从我的类对象创建一个数据表。该控件显然返回了一个双循环gridView,我只是希望它写出返回数据的视图,最终具有gridView CRUD Ops的所有优点。它很简单我做错了你能帮忙吗?

这是我用于创建 GridView 的 C# 代码

[WebMethod]
        public static AjaxReturnObject GetProductsByCategoryID(string CategoryID)
        {
            AjaxReturnObject o = new AjaxReturnObject();
            int catID = Convert.ToInt32(CategoryID);
            Product.ProductCollection products = new Product().GetProductsByCategoryID(catID);
            if (products.Count == 0)
            {
                o.Message = "There was no data returned";
                o.Status = 999;

                return o;
            }
            else
            {
                // build a new GridView (or List View) for the UI and populate it with data.
                // 1: Initialize a object of type DataTable.
                DataTable dt = new DataTable();

                //2: Initialize a object of type DataRow
                DataRow drow;

                //3: Initialize enough objects of type DataColumns
                DataColumn col1 = new DataColumn("Product Name", typeof(string));
                DataColumn col2 = new DataColumn("Product Description", typeof(string));
                DataColumn col3 = new DataColumn("Price", typeof(string));
                DataColumn col4 = new DataColumn("Col4", typeof(string));

                //4: Adding DataColumns to DataTable dt
                dt.Columns.Add(col1);
                dt.Columns.Add(col2);
                dt.Columns.Add(col3);
                dt.Columns.Add(col4);

                //5: Adding values in DataColumns       
                for (int i = 0; i < products.Count; i++)
                {

                    foreach (Product item in products)
                    {

                        drow = dt.NewRow();
                        dt.Rows.Add(drow);
                        dt.Rows[i][col1] = item.ProductName.ToString();// i.ToString();
                        dt.Rows[i][col2] = item.ProductDescription.ToString();
                        dt.Rows[i][col3] = String.Format("{0:C}", item.Price);
                        dt.Rows[i][col4] = String.Format("{0:.00}", item.Price);
                    }

                }


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


                // Render the new control and return it to the Ajax Return Object
                StringWriter tw = new StringWriter();
                Html32TextWriter writer = new Html32TextWriter(tw);
                GridView1.RenderControl(writer);
                writer.Close();
                o.Object = tw.ToString();


                o.Message = "Result Data Message";
                o.Status = 1;

                return o;
            }
        }
    }
4

3 回答 3

2

虽然上面的答案是对的。

但更好的方法是编写将集合转换为 DataTable 的扩展方法,我们可以在应用程序的任何地方使用它,我的项目中有一个用于转换List<T>DataTable.

它是这样的:

public static class ListExtensions
{
   public static DataTable ToDataTable<T>(this List<T> iList)
   {
    DataTable dataTable = new DataTable();
    PropertyDescriptorCollection propertyDescriptorCollection =
        TypeDescriptor.GetProperties(typeof(T));
    for (int i = 0; i < propertyDescriptorCollection.Count; i++)
    {
        PropertyDescriptor propertyDescriptor = propertyDescriptorCollection[i];
        Type type = propertyDescriptor.PropertyType;

        if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
            type = Nullable.GetUnderlyingType(type);


        dataTable.Columns.Add(propertyDescriptor.Name, type);
    }
    object[] values = new object[propertyDescriptorCollection.Count];
    foreach (T iListItem in iList)
    {
        for (int i = 0; i < values.Length; i++)
        {
            values[i] = propertyDescriptorCollection[i].GetValue(iListItem);
        }
        dataTable.Rows.Add(values);
    }
    return dataTable;
  }
}

并以任何List<T>这种方式使用它:

List<Product> products = new List<Product>();
DataTable dtProducts = products.ToDataTable();
于 2015-03-18T10:52:05.410 回答
1

我相信你在循环上犯了一个错误

// remove that line
// for (int i = 0; i < products.Count; i++)
int i = 0;
{    
    foreach (Product item in products)
    {    
        drow = dt.NewRow();
        dt.Rows.Add(drow);
        dt.Rows[i][col1] = item.ProductName.ToString();// i.ToString();
        dt.Rows[i][col2] = item.ProductDescription.ToString();
        dt.Rows[i][col3] = String.Format("{0:C}", item.Price);
        dt.Rows[i][col4] = String.Format("{0:.00}", item.Price);
        // and here move to next
        i++;
    }    
}
于 2013-01-23T11:16:23.370 回答
0

您正在运行不必要的 foreach 循环。改变你的循环结构如下:

//5: Adding values in DataColumns       
                for (int i = 0; i < products.Count; i++)
                {

                    //foreach (Product item in products)
                    //{
                    Product item = products[i];
                    drow = dt.NewRow();
                    dt.Rows.Add(drow);
                    dt.Rows[i][col1] = item.ProductName.ToString();// i.ToString();
                    dt.Rows[i][col2] = item.ProductDescription.ToString();
                    dt.Rows[i][col3] = String.Format("{0:C}", item.Price);
                    dt.Rows[i][col4] = String.Format("{0:.00}", item.Price);
                    //}

                }
于 2013-01-23T11:20:09.560 回答