且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何为动态创建的行分配和绑定gridview

更新时间:2023-12-06 15:40:22

一旦保存数据页面将回发。您可以在那里调用绑定数据方法。您查询数据库并获取数据表中的数据并将表绑定到gridview。
once you save the data the page will postback. You can call the bind data method there.you query the database and get the data in datatable and bind the table to gridview.


您可以重新绑定数据库中的值并使用linq显示它们to sql:



you can rebind the values from DB and display them by using linq to sql:

public void RefreshGrid()
       {
           OrdersDataContext contex = DataContextSingleton.Instance;

           var query = (from o in _readyToSave
                        join pr in contex.Products on o.prod_id equals pr.id
                        join d in contex.Discounts on o.discoun_id equals d.id
                        join r in contex.Registrations on d.id_reg equals r.id
                        select new
                        {
                            Article = pr.article,
                            Description = pr.description,
                            Quantity = o.quant,
                            Price = o.price,
                            Discount = d.discounts,
                            TotalPrice = o.totalprice
                        }).OrderBy(x => x.Article);

           OrderGridView.DataSource = query.ToList();
           OrderGridView.DataBind();

       }





这里有一个示例,我从数据库中检索数据然后在某处在我的代码后面会询问RefreshGrid(),然后datagridview将检索并显示来自DB的数据。



here is a sample, i retrieve tha data from DB and then somewhere in my code behind RefreshGrid() would be asked, then the datagridview will retrieve and show the data from DB.