壹:列字段類型:
1 列類型:
BoundField:綁定列,將數據庫中的數據以字符形式綁定顯示
CheckBoxField:復選框列,壹般用來綁定數據庫中的Bit型數,以復選框的形式顯示在GridView中
HyperLinkField:超鏈接列,可以用數據源中的數據作超鏈接文本也可以把所有超鏈接文本設為統壹的文本
ImageField:圖片列,綁定數據源中的圖片路徑,並把圖片顯示出來
CommandField:命令列,常用的“選擇”,“刪除”,“編輯、更新、取消”
ButtonField:按鈕列,其它做用的按鈕
TemplateField:模板列,可以更靈活地自定義顯示格式
2 在 Aapx頁面中主要做幾項工作:
(1) 列類型的選擇
(2) 列樣式選擇(也可再RowDataBound事件中設置)
(3) 列格式化顯示
(4) 列參數設置
(5) 隱藏列的設置(也可再RowDataBound事件中設置)
二:不同事件中,如何取得:主鍵值,列值,列控件
三:在模板列中可加入各種服務器控件,在模板列中的命令按鈕,首先觸發GridView1_RowCommand,其次觸發Button_Click事件
1 在模板列中取得控件,在Button_Click事件
(1) ((Label)(((LinkButton)sender).Parent.FindControl("Label1"))).Text = "change me";
(2) 在TextBox 的TextChanged事件中:
TextBox t = (TextBox)sender;
GridViewRow drv = (GridViewRow)t.NamingContainer;
int rowIndex = drv.RowIndex;
string coid = ((Label)gdvList.Rows[drv.RowIndex].FindControl("lblCoId")).Text;
(1) protected void lbtnViewRole_Click(object sender, EventArgs e)
{
int rowIndex = ((GridViewRow)((LinkButton)sender).NamingContainer).RowIndex;
RoleModel role = RoleBLL.GetRoleInfo(Int32.Parse(gvRoleList.Rows[rowIndex].Cells[0].Text));
txtRoleNameEdit.Text = role.RoleName;
txtRoleDescEdit.Text = role.RoleDesc;
cbIsAuth.Checked = role.IsUser;
cbIsSysEdit.Checked = role.IsSystem;
}
2 在模板列中取得控件,在GridView1_RowCommand事件
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "lbtn")
{
//獲取被點擊的linkButton所在的GridViewRow
GridViewRow gvrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
int index = gvrow.RowIndex; //獲取到行索引 RowIndex
//獲取當前行的某列值
string userid=GridView1.Rows[index].Cells[列索引].Text.Trim();
......
}
}
3 在模板列中取得控件,在GridView1_RowCommand事件
int index = Convert.ToInt32(e.CommandArgument); //獲得該按鈕在gridview中的位置,即第幾行
GridViewRow row = GridView1.Rows[index]; //通過索引返回該行
string i= row.Cells[0].Text.ToString().Trim(); //獲得該行第1列的數據項
四:在<asp:ButtonField>中的命令按鈕只能在GridView1_RowCommand事件中觸發
1 <asp:ButtonField ButtonType=Button ButtonType=Image ButtonType=Link...有三種按鈕類型。
2 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "aa")//aa是關聯的事件名
{
int index = Convert.ToInt32(e.CommandArgument);//獲取命令的參數
int iLeagueID = Convert.ToInt32(this.GridView1.DataKeys[index].Value);//獲取當前點擊列的ID號
Response.Write("<script>alert('" + iLeagueID + "')</script>");
}
}
五:在模板列中非命令字段,<%# Eval("字段名").ToString( ).Trim( ) %>
<%# Eval("PublishDate", "{0:dd/MM/yyyy}") %>
或者用Bind 方法支持讀/寫功能
六:BoundField字段:屬性:DataFormatString,可設置顯示字段的格式 {0:C}格式為貨幣類型
註意,當HtmlCode屬性設置為false DataFormatString才有效
七:HyperLinkField字段
DataNaVigateUrlFields 綁定數據庫字段,多個就用 , 分隔
DatanaVigateUrlFormatstring 超鏈接到的頁面
DatanaVigateUrlFormatstring="default.aspx?name={0}&address={1}&city={2}&state={3}"
DataNaVigateUrlFields="name,address,state,zip"
八:protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
ChangeDate(e);
HighLightCar(e);
GetAvgPrice(e);////////////////////////////
}
private void GetAvgPrice(GridViewRowEventArgs e)
{
//累加當前頁中的汽車的價格總和(_PriceSum)
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
string strprice = e.Row.Cells[4].Text;//帶有RMB字符串,如:RMB50.47
strprice = strprice.Substring(strprice.LastIndexOf(">") + 1);
double price = double.Parse(strprice);
_PriceSum += price;
}
}
//根據_PriceSum計算平均價格,並在頁腳顯示
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[4].Text = "<font color=red>RMB</font>"+(_PriceSum / GridView1.PageSize).ToString();
e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Right;
}
}
1 GridView的數據源DataTabel或者是AccessDataSource控件
2 控件的AutoGenerateColumns屬性如果是True,則自動綁定數據源
3 重點是GridView控件的<Columns>屬性的設置
<1>主鍵列的隱藏和取值
a:在GridView的屬性中設置DataKeyNames="ID字段的名稱",在<Columns>中不出現ID列
<2>主鍵列的取值
GridViewEntity.DataKeys[RowIndex]["ColumsName"]
或者
GridViewEntity.Rows[RowIndex].Cell[Index].Text
或者
GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
或者
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button clicked
// by the user from the Rows collection.
GridViewRow row = CustomersGridView.Rows[index];
item.Text = Server.HtmlDecode(row.Cells[2].Text);
<2>添加序號
<asp:TemplateField HeaderText="序號">
<ItemTemplate>
<%#this.ctrlPager.CurrentPageIndex * this.ctrlPager.PageSize + Container.DataItemIndex + 1%>
</ItemTemplate>
</asp:TemplateField>
希望對妳有幫助!