您好,登錄后才能下訂單哦!
前臺:
<div>
標題控件:<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCreated="GridView1_RowCreated">
<Columns>
<asp:BoundField DataField="M_Name" HeaderText="名稱" />
<asp:BoundField DataField="M_ID" HeaderText="字段ID" />
</Columns>
</asp:GridView>
<br /> 多標題:<br />
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" OnRowCreated="GridView2_RowCreated">
<Columns>
<asp:BoundField DataField="M_ID" HeaderText="字段ID1" />
<asp:BoundField DataField="M_ID" HeaderText="字段ID" />
<asp:BoundField DataField="M_Name" HeaderText="名稱" />
<asp:BoundField DataField="M_Name" HeaderText="名稱1" />
</Columns>
</asp:GridView>
</div>
后臺綁定:
private void bing()
{
string sql = "select M_ID,M_Name from Module";
DataTable dt = new DataTable();
dt = DBArticle.RunSqlDt(sql);
GridView1.DataSource = dt.DefaultView;
GridView1.DataBind();
GridView2.DataSource = dt.DefaultView;
GridView2.DataBind();
}
在表頭添加下拉框 :
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)//是否是表頭行
{
TableCellCollection tcl = e.Row.Cells;//獲得表頭元素的實例
int i = 1;
foreach (TableCell item in tcl)
{
string HeadText = item.Text;//本來datatable的表頭的自內容
DropDownList dro = new DropDownList();//創建表頭下拉框
dro.ID = "dro" + i.ToString();//表頭ID
dro.Items.Insert(0, new ListItem("--請選擇--", "-1"));
dro.Items.Add(new ListItem("數學","0"));
dro.Items.Add(new ListItem("語文", "1"));
dro.Items.Add(new ListItem("英語", "2"));
item.Controls.Add(dro);//添加下拉框控件
Label lab = new Label();
lab.ID = "lab" + i.ToString();
lab.Text = "</br>" + HeadText;
item.Controls.AddAt(1, lab);//此刻表頭單元格已有下拉框的label控件
i++;
}
}
}
多標題:
protected void GridView2_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
TableCellCollection tcHeader = e.Row.Cells;
tcHeader.Clear();
tcHeader.Add(new TableHeaderCell());
tcHeader[0].ColumnSpan = 4;//合并第一行的4列
tcHeader[0].Text = "截止到今日還有";
tcHeader[0].Text += "<strong>" + 1111 + "</strong>";
tcHeader[0].Text += "天</th></tr><tr>";
tcHeader.Add(new TableHeaderCell());
tcHeader[1].Style.Add("color", "#004274");
tcHeader[1].Style.Add("height", "30px");
tcHeader[1].ColumnSpan = 2;
tcHeader[1].Text = "姓名";
tcHeader.Add(new TableHeaderCell());
tcHeader[2].Style.Add("color", "#004274");
tcHeader[2].Style.Add("height", "30px");
tcHeader[2].ColumnSpan = 2;
tcHeader[2].Text = "密碼</th></tr><tr>";
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。