显示动态创建的表 [英] Display dynamically created table
本文介绍了显示动态创建的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
HI
我正在使用查询
选择 c.name as CompanyName,b.name as BusinessUnit,d.name as DepartmentName
来自公司c 内部 join BusinessUnit b on c.id = b.companyid inner join 部门d
b.ID = d.BUID
将显示的显示为:
<桌边=1>
在数据库方面很好..但在用户界面上,我想要它显示as:
公司 | 业务部门 | 部门 |
三星 | 电话 | Grand 2 |
注3 | ||
TV | Smart 32 | |
参考rigerator | 240 2D | |
笔记本电脑 | NP 530 U4C | |
Apple | 电话 | I电话5 |
I Phone 6 | ||
音乐播放器 | IPod |
请帮助。
解决方案
public class CustomeGrid
{
private string 表;
private StringBuilder TableRow;
private StringBuilder DataColumn;
private 列表< CustomeGridColumn> GridColumns;
private DataTable dataTable;
public CustomeGrid(DataTable dt)
{
// 数据可以在数据表中排序
dataTable = dt;
}
私有 void CreteTableHeader()
{
表= < table>;
int index = 0 ;
GridColumns = new 列表< CustomeGridColumn>();
foreach (DataColumn col in dataTable.Columns)
{
表+ = < th> + col.ColumnName + < / th>;
GridColumns.Add( new CustomeGridColumn {Index = index,Name = col.ColumnName});
index ++;
}
}
私有 void CreateRow( )
{
TableRow = new StringBuilder();
TableRow.Append( < tr>);
}
私有 void CloseRow()
{
TableRow.Append( < / tr>);
表+ = TableRow.ToString();
}
private void CreateColumn(CustomeGridColumn col)
{
SetRowSpan(col);
DataColumn = new StringBuilder();
DataColumn.Append( < td#' + col.Name + '#>);
DataColumn.Append( < span>);
}
private void CloseColumn()
{
DataColumn.Append( < / span>);
DataColumn.Append( < / td>);
TableRow.Append(DataColumn.ToString());
}
私有 void AddData( string data)
{
DataColumn.Append(data);
}
私有 void CloseTable()
{
表+ = < / table>;
SetRowSpanOnEndOfDataTable();
}
私有 void SetRowSpan(CustomeGridColumn col)
{
Table = Table.Replace( #' + col.Name + '#, rowspan =' + col.RowSpan + ');
}
private void SetRowSpanOnEndOfDataTable()
{
GridColumns.ForEach( delegate (CustomeGridColumn gc)
{
SetRowSpan(gc);
});
}
public string GetHtmlGrid()
{
CreteTableHeader();
foreach (DataRow行 in dataTable.Rows)
{
CreateRow();
GridColumns.ForEach( delegate (CustomeGridColumn gc)
{
if (gc.PreviousValue!= row [gc.Name] .ToString())
{
CreateColumn(gc);
gc.RowSpan = 1 ;
gc.PreviousValue = row [gc.Name] .ToString();
AddData(row [gc.Name] .ToString());
CloseColumn() ;
}
else
gc.RowSpan ++;
} );
CloseRow();
}
CloseTable();
return 表;
}
}
public class CustomeGridColumn
{
public int 索引{获得跨度>; set ; }
public string 名称{ get 跨度>; set ; }
public string PreviousValue { get 跨度>; set ; }
public int RowSpan { get 跨度>; set ; }
}
<
使用SQL查询中的有序数据表初始化CustomeGrid类
并调用CustomeGrid类中的GetHtmlGrid()方法,它将返回一个带有格式化html表的html字符串,如下面的代码行
CustomeGrid grid = new CustomeGrid(orderedDataTable);
TableContainer.InnerHtml = grid.GetHtmlGrid();
为什么不使用它的Pivot表?请在这里查看我的答案。
从两行检索数据并将其显示在一行 [ ^ ]
HI
I am using the query
select c.name as CompanyName, b.name as BusinessUnit,d.name as DepartmentName
from Company c inner join BusinessUnit b on c.id = b.companyid inner join Department d on
b.ID = d.BUID
which displays the recors as:
Company | Business Unit | Department |
Samsung | Phone | Grand 2 |
Samsung | Phone | Note 3 |
Samsung | TV | Smart 32 |
Samsung | Refrigerator | 240 2D |
Samsung | Laptop | NP 530 U4C |
Apple | Phone | I Phone 5 |
Apple | Phone | I Phone 6 |
Apple | Music Player | IPod |
Its fine on DB side..but on UI , I want it to be displayed as:
Company | Business Unit | Department |
Samsung | Phone | Grand 2 |
Note 3 | ||
TV | Smart 32 | |
Refrigerator | 240 2D | |
Laptop | NP 530 U4C | |
Apple | Phone | I Phone 5 |
I Phone 6 | ||
Music Player | IPod |
Kindly Help.
解决方案
public class CustomeGrid { private string Table; private StringBuilder TableRow; private StringBuilder DataColumn; private List<CustomeGridColumn> GridColumns; private DataTable dataTable; public CustomeGrid(DataTable dt) { //data sould be ordered in the data table dataTable = dt; } private void CreteTableHeader() { Table = "<table>"; int index = 0; GridColumns = new List<CustomeGridColumn>(); foreach (DataColumn col in dataTable.Columns) { Table += "<th>" + col.ColumnName + "</th>"; GridColumns.Add(new CustomeGridColumn { Index = index, Name = col.ColumnName }); index++; } } private void CreateRow() { TableRow = new StringBuilder(); TableRow.Append("<tr>"); } private void CloseRow() { TableRow.Append("</tr>"); Table += TableRow.ToString(); } private void CreateColumn(CustomeGridColumn col) { SetRowSpan(col); DataColumn = new StringBuilder(); DataColumn.Append("<td #'"+col.Name+"'#>"); DataColumn.Append("<span>"); } private void CloseColumn() { DataColumn.Append("</span>"); DataColumn.Append("</td>"); TableRow.Append(DataColumn.ToString()); } private void AddData(string data) { DataColumn.Append(data); } private void CloseTable() { Table += "</table>"; SetRowSpanOnEndOfDataTable(); } private void SetRowSpan(CustomeGridColumn col) { Table = Table.Replace("#'" + col.Name + "'#", "rowspan='" + col.RowSpan + "'"); } private void SetRowSpanOnEndOfDataTable() { GridColumns.ForEach(delegate(CustomeGridColumn gc) { SetRowSpan(gc); }); } public string GetHtmlGrid() { CreteTableHeader(); foreach (DataRow row in dataTable.Rows) { CreateRow(); GridColumns.ForEach(delegate(CustomeGridColumn gc) { if (gc.PreviousValue != row[gc.Name].ToString()) { CreateColumn(gc); gc.RowSpan = 1; gc.PreviousValue = row[gc.Name].ToString(); AddData(row[gc.Name].ToString()); CloseColumn(); } else gc.RowSpan++; }); CloseRow(); } CloseTable(); return Table; } } public class CustomeGridColumn { public int Index { get; set; } public string Name { get; set; } public string PreviousValue { get; set; } public int RowSpan { get; set; } }
<
initialize the CustomeGrid class with the ordered data table from SQL query
and call the GetHtmlGrid() method in CustomeGrid class and it will =return a html string with the formated html table like the code lines below
CustomeGrid grid = new CustomeGrid(orderedDataTable); TableContainer.InnerHtml = grid.GetHtmlGrid();
Why don't you use it Pivot table ? Please see my answer here.
Retrieving data from two row and showing it in one row[^]
这篇关于显示动态创建的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文