我做了很多这样的工作,并使用这种技术创建了一个下拉菜单和一个树视图。一个 ajax 调用,用于动态创建控件并将它们返回到使用 jqueryajax 和 C# 完全构建和配置的页面。但我坚持从我的类对象创建数据表。该控件显然返回一个双循环 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;
}
}
}
虽然上面的答案是对的。
但更好的方法是编写扩展方法,将集合转换为 DataTable,我们可以在应用程序中的任何位置使用它,我的项目中有一个用于转换List<T>
to 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();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)