如何在.NET中创建数据表? [英] How to Creating datatables in .NET?

查看:115
本文介绍了如何在.NET中创建数据表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个来自db的DataTable [具有5列],该表已合并了数据.我需要在此组合表上调用group by并创建2个表.一个表按行分组,其他具有项.用C#代码最快的方法是什么?另外,我在下面编写了为这2个表添加列的代码.对吗?

这是我的代码:

I''ve a DataTable[having 5 columns] from db which has combined data. I need to invoke a group by on this combined table and create 2 tables.One with grouped By rows and other having items. What would be fastest way to do this in C# code? Also, I''ve written code below for adding columns for these 2 tables.Is that correct?

Heres my code:

string colName = "ACCOUNT_ID"; 
var allRows = combinedTable.AsEnumerable();      

var accountRowGroups = allRows.GroupBy(row => row[colName]);

DataTable masterDataTable = new DataTable(); 
DataTable childPricesDataTable = new DataTable();

// Create the columns 
            DataColumnCollection pdCols = combinedTable.Columns; 
            for (int ndx = 0; ndx < pdCols.Count; ndx++) 
            { 
                string columnName = pdCols[ndx].ColumnName; 
                Type type = Type.GetType(pdCols[ndx].DataType.ToString());

                masterDataTable.Columns.Add(columnName, type); 
                childPricesDataTable.Columns.Add(columnName, type);

            }

推荐答案


试试这个希望你能得到它
受保护的void Page_Load(对象发送者,EventArgs e)

{

loadDynamicGrid();

}



私有void loadDynamicGrid()

{

#region用于准备DataTable的代码



//创建DataTable的实例

DataTable dt = new DataTable();



//创建一个ID列以添加到Datatable

DataColumn dcol =新的DataColumn(ID,typeof(System.Int32));

dcol.AutoIncrement = true;

dt.Columns.Add(dcol);


谢谢,
MOHAMMAD SIDDIQALI.
//创建一个ID列以添加到Datatable

dcol = new DataColumn(NAME,typeof(System.String));

dt.Columns.Add(dcol);



//现在为动态列添加数据

//由于第一列是自动递增的,因此我们不必添加任何内容.

//让我们在第二列中添加一些数据.

for(int nIndex = 0; nIndex< 10; nIndex ++)

{

//创建新行

DataRow drow = dt.NewRow();



//初始化行数据.

drow [NAME] ="Row-" + Convert.ToString((nIndex + 1));



//将行添加到数据表中.

dt.Rows.Add(drow);

}

#endregion



//遍历数据表的列以动态设置数据绑定字段.

foreach(dt.Columns中的DataColumn col)

{

//声明绑定字段并为绑定字段分配内存.

BoundField bfield = new BoundField();



//初始化DataField值.

bfield.DataField = col.ColumnName;



//初始化HeaderText字段值.

bfield.HeaderText = col.ColumnName;



//将新创建的绑定字段添加到GridView.

GrdDynamic.Columns.Add(bfield);

}



//初始化数据源

GrdDynamic.DataSource = dt;



//使用GridView绑定数据表.

GrdDynamic.DataBind();
Hi
try this hope you get it
protected void Page_Load(object sender, EventArgs e)

{

loadDynamicGrid();

}



private void loadDynamicGrid()

{

#region Code for preparing the DataTable



//Create an instance of DataTable

DataTable dt = new DataTable();



//Create an ID column for adding to the Datatable

DataColumn dcol = new DataColumn(ID ,typeof(System.Int32));

dcol.AutoIncrement = true;

dt.Columns.Add(dcol);


Thanks,
MOHAMMAD SIDDIQALI.
//Create an ID column for adding to the Datatable

dcol = new DataColumn(NAME, typeof(System.String));

dt.Columns.Add(dcol);



//Now add data for dynamic columns

//As the first column is auto-increment, we do not have to add any thing.

//Let''s add some data to the second column.

for (int nIndex = 0; nIndex < 10; nIndex++)

{

//Create a new row

DataRow drow = dt.NewRow();



//Initialize the row data.

drow[NAME] = "Row-" + Convert.ToString((nIndex + 1));



//Add the row to the datatable.

dt.Rows.Add(drow);

}

#endregion



//Iterate through the columns of the datatable to set the data bound field dynamically.

foreach (DataColumn col in dt.Columns)

{

//Declare the bound field and allocate memory for the bound field.

BoundField bfield = new BoundField();



//Initalize the DataField value.

bfield.DataField = col.ColumnName;



//Initialize the HeaderText field value.

bfield.HeaderText = col.ColumnName;



//Add the newly created bound field to the GridView.

GrdDynamic.Columns.Add(bfield);

}



//Initialize the DataSource

GrdDynamic.DataSource = dt;



//Bind the datatable with the GridView.

GrdDynamic.DataBind();


这篇关于如何在.NET中创建数据表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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