GridView实用代码

    技术2022-05-12  10

    ----自定义表头

    ----没有数据的时候显示表头

        protected void Button1_Click(object sender, EventArgs e)    {        DataTable testTable = new DataTable();        if (testTable.Rows.Count == 0)        {            DataColumn test1Column = new DataColumn("name", System.Type.GetType("System.String"));            DataColumn test2Column = new DataColumn("face", System.Type.GetType("System.String"));            testTable.Columns.Add(test1Column);            testTable.Columns.Add(test2Column);

                DataRow testRow = testTable.NewRow();            testTable.Rows.Add(testRow);

                this.GridViewTest.DataSource = testTable;            this.GridViewTest.DataBind();

                this.GridViewTest.Rows[0].Cells.Clear();            this.GridViewTest.Rows[0].Cells.Add(new TableCell());            this.GridViewTest.Rows[0].Cells[0].ColumnSpan = testTable.Columns.Count;            this.GridViewTest.Rows[0].Cells[0].Text = "No Record";

                this.GridViewTest.HeaderRow.Cells.Clear();            this.GridViewTest.HeaderRow.Cells.Add(new TableCell());            this.GridViewTest.HeaderRow.Cells[0].Text = "</td><td colspan='2'>Face</td></tr><tr><td>Name</td><td>Face1</td><td>Face2";

                this.GridViewTest.Rows[0].Cells[0].Style.Add("text-align", "center");

            }    } 


    最新回复(0)