序號 | 名稱 | 特質 |
4 | Aries | 憤怒,暴躁,直爽 |
5 | Taurus | 溫和,愛睏 |
6 | Gemini | 渙散,分裂 |
有一對多的資料...學會的話不管要套在標籤~Gridview~ListView~Textbox都可以喔~
因為考慮到資料可能會重複
所以有額外加上將重複資料刪掉的方法...是在大陸網站找到的,可是我網址弄丟了
如果原作者或是知道這是哪來的人麻煩請留言跟我說好嗎?
不是故意隨意轉貼的..
參考資料
前台
<div>
<asp:GridView ID="gvData" runat="server" AutoGenerateColumns="False"
onrowdatabound="gvData_RowDataBound">
<Columns>
<asp:BoundField HeaderText="序號" DataField="cSerialNo" />
<asp:BoundField HeaderText="名稱" DataField="cType" />
<asp:TemplateField HeaderText="特質">
<ItemTemplate>
<asp:Label ID="lblCharactics" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
後台
protected void Page_Load(object sender, EventArgs e)
{
gvData.DataSource = TmpData();
gvData.DataBind();
}
protected void gvData_RowDataBound(object sender, GridViewRowEventArgs e)
{
Label olblCharactics;
string strCharactics = "";
DataTable dtc = SelectDistinctByField(TmpDetailData(), "cCharactics");
//輸入要選取的Table跟要Distinct的欄位名稱
for (int i = 0; i < gvData.Rows.Count; i++)
{
for (int j = 0; j < dtc.Rows.Count; j++)
{
olblCharactics = (Label)gvData.Rows[i].FindControl("lblCharactics");
if (gvData.Rows[i].Cells[0].Text == dtc.Rows[j][1].ToString())
{
strCharactics += dtc.Rows[j][0].ToString();
if (j < dtc.Rows.Count - 1)
{
strCharactics += ",";
}
olblCharactics.Text = strCharactics;
}
if (olblCharactics.Text.Length > 0)
{
if (olblCharactics.Text.Substring(olblCharactics.Text.Length - 1, 1) == ",")
{
olblCharactics.Text = olblCharactics.Text.Substring(0, olblCharactics.Text.Length - 1);
}
}
}
strCharactics = "";
}
// (GridView)GridView1.Rows[i].FindControl("GridView2");
}
private DataTable TmpData()
{
DataTable dtr = new DataTable();
dtr.Columns.Add("cType");
dtr.Columns.Add("cSerialNo");
dtr.Rows.Add(new object[] { "Aries", "4" });
dtr.Rows.Add(new object[] { "Taurus", "5" });
dtr.Rows.Add(new object[] { "Gemini", "6" });
dtr.Rows.Add(new object[] { "Cancer", "7" });
dtr.Rows.Add(new object[] { "Leo", "8" });
dtr.Rows.Add(new object[] { "Virgo", "9" });
dtr.Rows.Add(new object[] { "Libra", "10" });
dtr.Rows.Add(new object[] { "Scorpio", "11" });
dtr.Rows.Add(new object[] { "Sagittarius", "12" });
dtr.Rows.Add(new object[] { "Capricorn", "1" });
dtr.Rows.Add(new object[] { "Aquarius", "2" });
dtr.Rows.Add(new object[] { "Pisces", "3" });
return dtr;
}
private DataTable TmpDetailData()
{
DataTable dtr = new DataTable();
dtr.Columns.Add("cCharactics");
dtr.Columns.Add("cTypeNo");
dtr.Rows.Add(new object[] { "憤怒", "4" });
dtr.Rows.Add(new object[] { "暴躁", "4" });
dtr.Rows.Add(new object[] { "直爽", "4" });
dtr.Rows.Add(new object[] { "憤怒", "4" });
dtr.Rows.Add(new object[] { "暴躁", "4" });
dtr.Rows.Add(new object[] { "直爽", "4" });
dtr.Rows.Add(new object[] { "溫和", "5" });
dtr.Rows.Add(new object[] { "愛睏", "5" });
dtr.Rows.Add(new object[] { "渙散", "6" });
dtr.Rows.Add(new object[] { "分裂", "6" });
dtr.Rows.Add(new object[] { "可愛", "7" });
dtr.Rows.Add(new object[] { "愛家", "7" });
dtr.Rows.Add(new object[] { "霸氣", "8" });
dtr.Rows.Add(new object[] { "雍容", "8" });
dtr.Rows.Add(new object[] { "潔癖", "9" });
dtr.Rows.Add(new object[] { "仔細", "9" });
dtr.Rows.Add(new object[] { "公平", "10" });
dtr.Rows.Add(new object[] { "傻笑", "10" });
dtr.Rows.Add(new object[] { "神秘", "11" });
dtr.Rows.Add(new object[] { "桀傲", "12" });
dtr.Rows.Add(new object[] { "好動", "12" });
dtr.Rows.Add(new object[] { "樂觀", "12" });
dtr.Rows.Add(new object[] { "耐心", "1" });
dtr.Rows.Add(new object[] { "踏實", "1" });
dtr.Rows.Add(new object[] { "控制", "1" });
dtr.Rows.Add(new object[] { "未知", "2" });
dtr.Rows.Add(new object[] { "理想", "2" });
dtr.Rows.Add(new object[] { "博愛", "2" });
dtr.Rows.Add(new object[] { "愛哭", "3" });
dtr.Rows.Add(new object[] { "好騙", "3" });
return dtr;
}
// 過濾DataTable中的指定column重複的行
public DataTable SelectDistinctByField(DataTable dt, string FieldName)
{
DataTable returnDt = new DataTable();
returnDt = dt.Copy();//將原DataTable複製一個新的
DataRow[] drs = returnDt.Select(" ", FieldName);//將DataTable按指定的 column 排序
object LastValue = null;
for (int i = 0; i < drs.Length; i++)
{
if ((LastValue == null) || (!(ColumnEqual(LastValue, drs[i][FieldName]))))
{
LastValue = drs[i][FieldName];
continue;
}
drs[i].Delete();
}
return returnDt;
}
private bool ColumnEqual(object A, object B)
{
if (A == DBNull.Value && B == DBNull.Value) // both are DBNull.Value
return true;
if (A == DBNull.Value || B == DBNull.Value) // only one is DBNull.Value
return false;
return (A.Equals(B)); // value type standard comparison
}
沒有留言:
張貼留言