私はまだ自分の解決策に納得していませんが、これが私がそれを行う方法だと思います...
初めてデータをロードするとき、辞書に各名前の実行日を入力します。
private Dictionary<string, List<DateTime>> runDates = new Dictionary<string, List<DateTime>>();
private void LoadData()
{
DataTable table = new DataTable();
table.Columns.Add("TempName", typeof(string));
using (var connection = new SqlConnection("YourConnectionString"))
using (var command = new SqlCommand("SELECT DISTINCT TempName, RunDate FROM History_Table;", connection))
{
connection.Open();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
string tempName = reader.GetString(0);
if (!runDates.ContainsKey(tempName))
{
DataRow row = table.NewRow();
row[0] = tempName;
table.Rows.Add(row);
runDates.Add(tempName, new List<DateTime>());
}
runDates[tempName].Add(reader.GetDateTime(1));
}
}
GridView1.DataSource = table;
GridView1.DataBind();
}
}
各行を読み取るときに、名前が既に存在するかどうかを確認し、存在しない場合は、GridView をバインドするために使用されるテーブルとディクショナリに追加します。次に、バインド イベントで name ラベルを使用して、辞書から日付のリストを見つけます。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Find the label with the temp name and assign it
string tempName = (e.Row.FindControl("Label1") as Label).Value;
DropDownList ddl = e.Row.FindControl("DropDownList1") as DropDownList;
ddl.DataTextField = "RunDate";
ddl.DataValueField = "TempID";
ddl.DataSource = runDates[tempName];
ddl.DataBind();
}
}
私は他のさまざまなアイデアをいじりました.1つは名前の一意のリストを持つxmlフィールドとして日付リストを返し(前の質問のように)、これを隠しフィールドに格納し、行のデータバインドイベントでデシリアライズしました.別の解決策はデータセットに2つのテーブルを生成し、関係を定義しますが、これは私が提案した方法と原理的に非常に似ており、私はこれを好むと思います...