GridView中,若將AutoGenerateColumns設為False,則所有欄位自行設計,可使用的類型如下表所示,且必須在<Columns></Columns>中進行編輯,才能使GridView正常顯示資料內容。
列欄類型
說明
Boundfield
GridView自訂預設欄位類型。
TemplateField
使用者自定義欄位類型時所預先填入的類型。
CheckBoxField
在GridView增加核取方塊,為boolean值 。
HyperLinkField
將欄位值設為超連結,可連結到別的頁面。
ImageField
為GridView設定圖項顯示。
ButtonField
為GridView設定按鈕控制項,「新增」或「移除」按鈕。
CommandField
用來「選擇」、「編輯」或「刪除」的命令按鈕。
- 9月 11 週一 201711:21
[C#]GridView裡可使用的自行列欄位設計類型
- 9月 11 週一 201711:21
[C#]GridView裡可使用的自行列欄位設計類型
GridView中,若將AutoGenerateColumns設為False,則所有欄位自行設計,可使用的類型如下表所示,且必須在<Columns></Columns>中進行編輯,才能使GridView正常顯示資料內容。
列欄類型
說明
Boundfield
GridView自訂預設欄位類型。
TemplateField
使用者自定義欄位類型時所預先填入的類型。
CheckBoxField
在GridView增加核取方塊,為boolean值 。
HyperLinkField
將欄位值設為超連結,可連結到別的頁面。
ImageField
為GridView設定圖項顯示。
ButtonField
為GridView設定按鈕控制項,「新增」或「移除」按鈕。
CommandField
用來「選擇」、「編輯」或「刪除」的命令按鈕。
- 9月 08 週五 201717:07
[C#]GridView隱藏欄位之一 OnRowDataBound
GridView屬性中AutoGenerateColumns設為False時,內容欄位格式均為自行設計Coding,故若有Primary Key或是欲隱藏不顯示的欄位內容,但欄位值本身仍需做計算或供其他網頁使用,可採用以下方式,將欄位標題及內容隱藏,不顯示於畫面上,但該欄位所有的數值或文字,仍可Coding使用。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
// 隱藏標題列中某特定欄位
// 1
e.Row.Cells[3].Visible = false;
// 2
foreach (TableCell col in e.Row.Cells)
{
if ((((DataControlFieldCell)(col)).ContainingField).HeaderText == "標題文字4")
{
col.Visible = false;
}
}
// 3
foreach (DataControlFieldCell col in e.Row.Cells)
{
if ((col.ContainingField).HeaderText == "標題文字4")
{
col.Visible = false;
}
}
// 4(using System.Linq;) Page_Load中可使用
e.Row.Cells.Cast()
.Where(c => c.Text == "標題文字4")
.ToList()
.ForEach(col => col.Visible = false);
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
// 隱藏標題列中某特定欄位
// 1
e.Row.Cells[3].Visible = false;
// 2
foreach (TableCell col in e.Row.Cells)
{
if ((((DataControlFieldCell)(col)).ContainingField).HeaderText == "標題文字4")
{
col.Visible = false;
}
}
// 3
foreach (DataControlFieldCell col in e.Row.Cells)
{
if ((col.ContainingField).HeaderText == "標題文字4")
{
col.Visible = false;
}
}
// 4(using System.Linq;) Page_Load中可使用
e.Row.Cells.Cast()
.Where(c => c.Text == "標題文字4")
.ToList()
.ForEach(col => col.Visible = false);
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
}
}
- 9月 08 週五 201717:07
[C#]GridView隱藏欄位之一 OnRowDataBound
GridView屬性中AutoGenerateColumns設為False時,內容欄位格式均為自行設計Coding,故若有Primary Key或是欲隱藏不顯示的欄位內容,但欄位值本身仍需做計算或供其他網頁使用,可採用以下方式,將欄位標題及內容隱藏,不顯示於畫面上,但該欄位所有的數值或文字,仍可Coding使用。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
// 隱藏標題列中某特定欄位
// 1
e.Row.Cells[3].Visible = false;
// 2
foreach (TableCell col in e.Row.Cells)
{
if ((((DataControlFieldCell)(col)).ContainingField).HeaderText == "標題文字4")
{
col.Visible = false;
}
}
// 3
foreach (DataControlFieldCell col in e.Row.Cells)
{
if ((col.ContainingField).HeaderText == "標題文字4")
{
col.Visible = false;
}
}
// 4(using System.Linq;) Page_Load中可使用
e.Row.Cells.Cast()
.Where(c => c.Text == "標題文字4")
.ToList()
.ForEach(col => col.Visible = false);
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
// 隱藏標題列中某特定欄位
// 1
e.Row.Cells[3].Visible = false;
// 2
foreach (TableCell col in e.Row.Cells)
{
if ((((DataControlFieldCell)(col)).ContainingField).HeaderText == "標題文字4")
{
col.Visible = false;
}
}
// 3
foreach (DataControlFieldCell col in e.Row.Cells)
{
if ((col.ContainingField).HeaderText == "標題文字4")
{
col.Visible = false;
}
}
// 4(using System.Linq;) Page_Load中可使用
e.Row.Cells.Cast()
.Where(c => c.Text == "標題文字4")
.ToList()
.ForEach(col => col.Visible = false);
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
}
}
