将对象添加到DataTable并创建动态GridView

我有很多工作,并使用这种技术创建了一个下拉列表和一个树视图。 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; } } } 

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

 // 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++; } } 

你正在运行不必要的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); //} } 

虽然上面的答案是正确的。

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

它来了:

 public static class ListExtensions { public static DataTable ToDataTable(this List 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上使用它:

 List products = new List(); DataTable dtProducts = products.ToDataTable();