【问题标题】:Bind Specific Number of Columns of Dataset to Gridview将数据集的特定列数绑定到 Gridview
【发布时间】:2013-03-31 17:16:56
【问题描述】:

我是桌面应用程序开发的新手。

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

BillNo    |    Descirption    |  HSN Code    |   Qty    | Rate

我有一个数据网格视图文本框列类型的网格视图,其中指定了以下列。

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

我想将“Description”、“HSN Code”、“Qty”和“Rate”从数据集绑定到网格视图,并且我想生成“Sr No.”。 & 以编程方式显示网格视图的“金额”。

我该怎么做?

请帮忙。

【问题讨论】:

    标签: c# data-binding gridview datasource desktop-application


    【解决方案1】:

    在运行时创建新的Datatable对象并根据需要填充数据表并将其绑定到datagrid或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;
        }
    

    【讨论】:

    • 我应该将列的名称与网格视图相同吗?即table.Columns.Add("Sr No.", typeof(int));,在网格视图中,列名是clmSr,标题文本是Sr.@raj
    • 感谢它完美地工作....但是仍然存在一个问题,它将列附加到现有的网格视图列中.. 数据未显示在现有列中怎么办.?? @raj
    • 进行了更改,但仍将列附加到网格中。不在其实际位置显示。@raj
    • 你能发布你的gridview标记代码吗?很难理解你在说什么。
    • 或者您是在讲述绑定数据网格的代码吗? @raj
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 2013-03-27
    • 2010-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多