2013-04-09 48 views
0

我是桌面应用程序开发的新手。将数据集的特定列数绑定到Gridview

我具有包含以下字段的数据集:

BillNo | Descirption | HSN Code | Qty | Rate 

和我有类型数据网格视图文本框柱的网格图,其中指定下列。

Sr No. | Descirption  | HSN Code | Qty | Rate | Amount 

我要绑定“描述”,“HSN代码”,“数量” &“费率”从数据集中的网格视图和我想要生成的“SR号” &以编程方式显示网格视图的“金额”。

我该怎么做?

请帮忙。

回答

1

在运行时创建新的DataTable对象和填充数据表按规定并将其绑定到数据网格或gridview的

如下图所示:

Private DataTable GetTable() 
    { 

    DataTable table = new DataTable(); 
    table.Columns.Add("Sr No.", typeof(int)); 
    table.Columns.Add("Descirption", typeof(string)); 
    table.Columns.Add("Code", typeof(string)); 
    ......... 
    ........ 


    retrive the data from datasourec in datatable dt 

    //Here first loop through your orignal datatable or dataset 

    forecch(DataRow drow in dt.Rows) 
    { 
     table.Rows.Add("your sr no", drow["Descirption" ],drow["Code"]);  
    } 
    return table; 
    } 
+0

我应该列的名称相同网格视图? ie'table.Columns.Add(“Sr No.”,typeof(int));'在网格视图中,列名是'clmSr',标题文本是'Sr.' @raj – 2013-04-09 10:46:08

+0

谢谢它的完美工作。 ..但仍然有一个问题是,它将列附加到现有的网格视图列。数据不显示在现有的列做什么。 @raj – 2013-04-09 10:54:57

+0

是的,使网格视图的列名称相同,它将起作用。 – Gajendra 2013-04-09 11:01:16