https://pramaire.pixnet.net/blog/post/31837470
using System;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
namespace prjGridV
{
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
conn.ConnectionString = WebConfigurationManager.ConnectionStrings["dbconn"].ConnectionString;
conn.Open();
cmd.Connection = conn;
cmd.CommandText = "SELECT [U_NAME],[U_LEVEL] FROM [AMS00].[dbo].[UNIT] order by [U_LEVEL]";
//cmd.Parameters.Clear();
//cmd.Parameters.AddWithValue("",);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
conn.Close();
//string dr_U_NAME = "無任何資料1";
//string dr_U_LEVE = "無任何資料2";
//==========================================
//if (dt.Rows.Count>0)
//{
// dr_U_NAME = "";
// dr_U_LEVE = "";
// foreach (DataRow dr_data in dt.Rows)
// {
// dr_U_NAME += dr_data["U_NAME"].ToString();
// dr_U_NAME += "<br />";
// dr_U_LEVE += dr_data["U_LEVEL"].ToString();
// dr_U_LEVE += "<br />";
// }
//}
//Literal1.Text = dr_U_LEVE;
//Literal2.Text = dr_U_NAME;
//==========================================
//事後加一筆=====================================
//DataRow dr = dt.NewRow();
//dr["U_NAME"] = "千機";
//dr["U_LEVEL"] = 9;
//dt.Rows.Add(dr);
//==============================================
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
cell1.Text = "表格第一列";
row.Cells.Add(cell1);
TableCell cell2 = new TableCell();
cell2.Text = "表格第二列";
//cell2.Font.Size = FontUnit.Large; //設定文字尺寸
cell2.ForeColor = System.Drawing.Color.Blue; //設定文字顏色
cell2.BackColor = System.Drawing.Color.Red; //設定文字顏色
row.Cells.Add(cell2);
TableCell cell3 = new TableCell();
cell3.Text = "表格第三列";
row.Cells.Add(cell3);
//產生表格
Table1.Rows.Add(row);
}
protected void GridView1_PreRender(object sender, EventArgs e)
{
int i = 1;
foreach (GridViewRow gvItem in GridView1.Rows)
{
if (gvItem.RowIndex != 0)
{
//比對如果名稱如果相同就合併(RowSpan+1)
if (gvItem.Cells[0].Text.Trim() == GridView1.Rows[(gvItem.RowIndex - i)].Cells[0].Text.Trim())
{
GridView1.Rows[(gvItem.RowIndex - i)].Cells[0].RowSpan += 1;
gvItem.Cells[0].Visible = false;
i = i + 1;
}
else
{
GridView1.Rows[(gvItem.RowIndex)].Cells[0].RowSpan += 1;
i = 1;
}
}
else
{
gvItem.Cells[0].RowSpan = 1;
}
}
}
}
}
沒有留言:
張貼留言