添加对象DataTable,并创建一个动态的GridView [英] Adding object to DataTable and create a dynamic GridView

查看:177
本文介绍了添加对象DataTable,并创建一个动态的GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多这方面的工作,并创建了一个下拉列表,并使用这种技术树视图。调用Ajax动态创建控件,并将其退回到完全建成并使用jqueryajax和C#配置的页面。但林停留在从我的类对象创建的DataTable。控制显然会返回一个双循环的GridView,我只是希望它与GridView的CRUD OPS的所有善良写出返回数据的视图,最终。它的一些简单的即时通讯做错了,你能帮忙吗?

下面是我的C#code创建的GridView

  [的WebMethod]
        公共静态AjaxReturnObject GetProductsByCategoryID(字符串categoryId)
        {
            AjaxReturnObject O =新AjaxReturnObject();
            INT CATID = Convert.ToInt32(类别id);
            。Product.ProductCollection产品=新产品()GetProductsByCategoryID(CATID);
            如果(products.Count == 0)
            {
                o.Message =没有返回的数据;
                o.Status = 999;                返回O;
            }
            其他
            {
                //建立一个新的GridView(或列表视图)的用户界面,并用数据填充它。
                // 1:初始化类型的DataTable的对象。
                DataTable的DT =新的DataTable();                // 2:初始化类型的对象的DataRow
                DataRow的卓尔;                // 3:初始化类型DataColumns的足够的对象
                DataColumn的COL1 =新的DataColumn(产品名称,typeof运算(字符串));
                COL2的DataColumn =新的DataColumn(产品说明中的typeof(字符串));
                DataColumn的COL3 =新的DataColumn(价格,typeof运算(字符串));
                DataColumn的COL4 =新的DataColumn(COL4,typeof运算(字符串));                // 4:添加到DataColumns DataTable的DT
                dt.Columns.Add(COL1);
                dt.Columns.Add(COL2);
                dt.Columns.Add(COL3);
                dt.Columns.Add(COL4);                // 5:DataColumns添加值
                的for(int i = 0; I< products.Count;我++)
                {                    的foreach(在产品中的产品项)
                    {                        卓尔= dt.NewRow();
                        dt.Rows.Add(卓尔);
                        dt.Rows [Ⅰ] [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 =新的GridView();
                GridView1.DataSource = DT;
                GridView1.DataBind();
                //渲染新的控制并将其返回给Ajax返回的对象
                StringWriter的TW =新的StringWriter();
                Html32TextWriter作家=新Html32TextWriter(TW);
                GridView1.RenderControl(作家);
                writer.Close();
                o.Object = tw.ToString();
                o.Message =结果数据电文;
                o.Status = 1;                返回O;
            }
        }
    }


解决方案

我相信你有做环路上的错误

  //删除了这一行
//的for(int i = 0; I< products.Count;我++)
INT I = 0;
{
    的foreach(在产品中的产品项)
    {
        卓尔= dt.NewRow();
        dt.Rows.Add(卓尔);
        dt.Rows [Ⅰ] [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);
        //这里移动到下一个
        我++;
    }
}

I have a lot of this working and have created a dropdown and a treeview using this technique. An ajax call to create controls on the fly and return them to the page fully constructed and configured using jqueryajax and C#. But Im stuck on creating a datatable from my class object. The control obviously returns a double looped gridView, I just want it to write out a view of the returned data, eventually with all the goodness of gridView CRUD Ops. Its something simple Im doing wrong can you help?

Here is my C# code for creating a GridView

[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;
            }
        }
    }

解决方案

I believe that you have make a mistake on the loop

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

这篇关于添加对象DataTable,并创建一个动态的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆