我正在使用asp.net web app .net version 4.我有一个数据表,如下所示:
FCTR_GRP CUST_TIER CUST_TIER
DSC SW Tier 1 1.000000
DSC SW Tier 2 1.000000
DSC SW Tier 3 1.000000
DSC SW Tier 4 1.000000
使用我从Netezza数据库中读取的上述数据表我想将其转换为数据表,如:
FCTR_GRP TIER_1 TIER_2 TIER_3 TIER_4
DSC SW 1.000000 1.000000 1.000000 1.000000
然后将其绑定到gridview。由于层可以在以后添加,因此gridview需要是动态的。 直到现在我到达这里。我可以像这样转换第一个数据表:
FCTR_GRP TIER_1 TIER_2 TIER_3 TIER_4
DSC SW 1.000000
DSC SW 1.000000
DSC SW 1.000000
DSC SW 1.000000
代码用作(不满意代码:(但没有其他选择)
DataTable CustomerCurrentFactorDataTable = new DataTable();
CustomerCurrentFactorDataTable.Columns.Add("Factor Group", typeof(System.String));
foreach (DataRow datarow in dt.Rows)
{
CustomerCurrentFactorDataTable.Columns.Add(datarow["CUST_TIER"].ToString().ToUpper(), typeof(System.String));
}
GridViewSample.DataSource = CustomerCurrentFactorDataTable;
GridViewSample.DataBind();
#region
string query = "SELECT FCTR_GRP, CUST_TIER, CUST_FCTR FROM DBO.CUST_FCTR";
#endregion
dt = getData.GetDataTable(query);
DataRow dr = CustomerCurrentFactorDataTable.NewRow();
int columnCount = CustomerCurrentFactorDataTable.Columns.Count;
int internalCounter = 0;
int count = 0;
bool flag=false;
int loopoutcounter = 0;
foreach (DataRow dataRow in dt.Rows)
{
count = 0;
if (loopoutcounter == 2)
{
loopoutcounter = 0;
CustomerCurrentFactorDataTable.Rows.Add(dr);
dr = CustomerCurrentFactorDataTable.NewRow();
//dr.Delete();
}
foreach (var cell in dataRow.ItemArray)
{
if (count == 0)
{
dr[count] = cell.ToString();
loopoutcounter++;
count++;
}
if (flag)
{
dr[internalCounter] = cell.ToString();
flag = false;
loopoutcounter++;
}
foreach (DataColumn column in CustomerCurrentFactorDataTable.Columns)
{
if (column.ColumnName == cell.ToString().ToUpper())
{
flag = true;
internalCounter = column.Ordinal;
continue;
}
}
}
如果我们可以转换最后一个数据表,例如第二个数据表或任何击球选项,任何帮助都将非常有用。谢谢提前。
答案 0 :(得分:0)
string html = "<table>";
//add header row
html += "<tr>";
for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
html += "<td>" + ds.Tables[0].Columns[i].ColumnName + "</td>";
html += "</tr>";
//add rows
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
html += "<tr>";
for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
html += "<td>" + ds.Tables[0].Rows[i][j].ToString() + "</td>";
html += "</tr>";
}
html += "</table>";