私はすでにここでこの質問をしています:表示する列の動的数を持つテーブル
しかし、これについてさらにいくつか質問があります。
1.各行に保険名、プランタイプ、保険料などのデータを含むデータ テーブルがあります。
私のフロントエンドでは、次のように表示する必要があります。
Insurance Name HealthNet Harvard UniCare
Plan Type HMO PPO HMO
Premium 100 150 200
このために、次の方法を使用してデータテーブルをピボットし、それをグリッドビューに割り当てています)
private DataTable PivotTable(DataTable origTable){
DataTable newTable = new DataTable();
DataRow dr = null;
//Add Columns to new Table
for (int i = 0; i <= origTable.Rows.Count; i++)
{
newTable.Columns.Add(new DataColumn(origTable.Columns[i].ColumnName, typeof(String)));
}
//Execute the Pivot Method
for (int cols = 0; cols < origTable.Columns.Count; cols++)
{
dr = newTable.NewRow();
for (int rows = 0; rows < origTable.Rows.Count; rows++)
{
if (rows < origTable.Columns.Count)
{
dr[0] = origTable.Columns[cols].ColumnName; // Add the Column Name in the first Column
dr[rows + 1] = origTable.Rows[rows][cols];
}
}
newTable.Rows.Add(dr); //add the DataRow to the new Table rows collection
}
return newTable;
}
また、次のようにデータ バインディングを行います。
DataTable tempTable = PivotTable(actualDataTable);
myGridView.DataSource = tempTable;
myGridView.DataBind();
今、私の出力は次のようになります。
Insurance Name HealthNet Harvard UniCare
Plan Type HMO PPO HMO
Premium 100 150 200
しかし今、グリッド ビューにはデータ ソースからのすべての値が表示されています。各保険名の横にある表の行に画像を挿入し、一部の行も非表示にできるように、グリッド ビューの外観をカスタマイズしたいと考えています。
注:グリッドビューだけに限定されているわけではありません。私はどんなコントロールでも問題ありませんが、データはハードコーディングではなく動的でなければなりません。
ありがとう
編集:
各保険名の横に画像を表示するのではなく、列の数に基づいてループできますか?
protected void myGridview_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[1].Text = "<img src=../images/" + e.Row.Cells[1].Text + ".png />";
e.Row.Cells[2].Text = "<img src=../images/" + e.Row.Cells[2].Text + ".png />";
}
}