因為用SQL很慢,用.NET比較快
public void combineRow()
{
for (int i = 0; i < Gridview1.Rows.Count; i++)
{
Label lblTime = (Label)Gridview1.Rows[i].FindControl("lblTime");
if (i > 0)
{
Label oldlblTime = (Label)Gridview1.Rows[i-1].FindControl("lblTime");
if (Gridview1.Rows[i].Cells[0].Text == Gridview1.Rows[i - 1].Cells[0].Text
&& Gridview1.Rows[i].Cells[1].Text == Gridview1.Rows[i - 1].Cells[1].Text)
{
if (oldlblTime.Text.Length == 0)//第一個比對值,取BoundField資料
{
lblTime.Text += Gridview1.Rows[i - 1].Cells[2].Text + oldlblTime.Text + "," + Gridview1.Rows[i].Cells[2].Text;
}
else
{
lblTime.Text += oldlblTime.Text + "," + Gridview1.Rows[i].Cells[2].Text;
}
Gridview1.Rows[i-1].Visible = false;
}
}
}
}
private void AddData()
{
DataTable dt = new DataTable();
dt.Columns.Add("date");
dt.Columns.Add("class");
dt.Columns.Add("hour");
dt.Rows.Add(new object[] { "Mon", "HandMAke", "1" });
dt.Rows.Add(new object[] { "Mon", "HandMAke", "2" });
dt.Rows.Add(new object[] { "Mon", "HandMAke", "3" });
dt.Rows.Add(new object[] { "Tur", "BookSearch", "2" });
dt.Rows.Add(new object[] { "Tur", "BookSearch", "3" });
dt.Rows.Add(new object[] { "Fri", "Yoga", "7" });
dt.Rows.Add(new object[] { "Fri", "Yoga", "8" });
dt.Rows.Add(new object[] { "Fri", "Yoga", "9" });
dt.Rows.Add(new object[] { "Fri", "Yoga", "10" });
Gridview1.DataSource = dt;
Gridview1.DataBind();
combineRow();
}
沒有留言:
張貼留言