在 控件中將數(shù)據(jù)行綁定到數(shù)據(jù)時發(fā)生。
命名空間:
程序集: System.Web(在 System.Web.dll 中)
呈現(xiàn) 控件之前,該控件中的每一行必須綁定到數(shù)據(jù)源中的一條記錄。將某個數(shù)據(jù)行(用 對象表示)綁定到 控件中的數(shù)據(jù)以后,將引發(fā) RowDataBound 事件。這使您可以提供一個這樣的事件處理方法,即每次發(fā)生此事件時就執(zhí)行一個自定義例程(如修改綁定到該行的數(shù)據(jù)的值)。
對象將傳遞給事件處理方法,以便您可以訪問正在綁定的行的屬性。若要訪問行中的特定單元格,請使用 對象的 屬性中包含的 對象的 屬性。使用 屬性可確定正在綁定的是哪一種行類型(標(biāo)題行、數(shù)據(jù)行等等)。
有關(guān)處理事件的更多信息,請參見。
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Display the company name in italics.
e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView RowDataBound Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridView RowDataBound Example</h3>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="true"
allowpaging="true"
onrowdatabound="CustomersGridView_RowDataBound"
runat="server">
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="CustomersSqlDataSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
2008-08-14 14:38
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//加入鼠標(biāo)滑過的高亮效果
if (e.Row.RowType == DataControlRowType.DataRow)//判定當(dāng)前的行是否屬于datarow類型的行
{
//當(dāng)鼠標(biāo)放上去的時候 先保存當(dāng)前行的背景顏色 并給附一顏色
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='yellow',this.style.fontWeight='';");
//當(dāng)鼠標(biāo)離開的時候 將背景顏色還原的以前的顏色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
//單擊行改變行背景顏色
e.Row.Attributes.Add("onclick", "this.style.backgroundColor='#99cc00'; this.style.color='buttontext';this.style.cursor='default';");
e.Row.Attributes.Add("ondblclick", "dbclickevent(" + GridView1.DataKeys[e.Row.RowIndex].Values[0].ToString() + ")");
//e.Row.RowIndex為當(dāng)前行的索引
//DataKeys[索引].Value[0]
}
}
GridView是ASP.NET中功能強大的數(shù)據(jù)顯示控件,它的RowDataBound事件為我們提供了方便的控制行、列數(shù)據(jù)的途徑。
要獲取當(dāng)前行的某個數(shù)據(jù)列,我在實踐中總結(jié)有如下幾種方法:
1. Cells[x].Txt。
從列單元格的文本值獲取。這種方法簡單高率,最為常用,但是功能單純。此法存在幾個缺點:
(1)無法獲取到設(shè)置了隱藏屬性的數(shù)據(jù)列的值,所取到的值為“”(空)。
(2)只能獲取在HTML中定義過的數(shù)據(jù)列,無法查詢數(shù)據(jù)源中的當(dāng)前數(shù)據(jù)行的所有字段列。一般情況下,通過HTML設(shè)置GridView的字段列的數(shù)量往往小于數(shù)據(jù)源的實際字段數(shù)量,這是因為從業(yè)務(wù)邏輯的角度看,并不是所有的字段都是要顯示在頁面上的。當(dāng)需要使用沒有被顯示的字段時,此法就不能直接滿足需求了。
2. e.Row.Cells[x].FindControl("YourcontrolName")。---返回YourcontrolName
這是在單元格內(nèi)查找某個服務(wù)器控件,從而獲得其數(shù)據(jù)值。這種方式可以操作單元格內(nèi)的服務(wù)器控件。此法一般用于處理模板列中的數(shù)據(jù)或控件。
3. (DataRowView)e.Row.DataItem.Row.ItemArray[x].ToString()。
此法的核心是e.Row.DataItem,它是GridView的行數(shù)據(jù)集,為Object類型,將其轉(zhuǎn)化為DataRowView類型后,可以獲得更多的操作方法。此數(shù)據(jù)集表示數(shù)據(jù)源當(dāng)前行的全部字段列,ItemArray[x]是當(dāng)前行全部字段列的數(shù)組對象,可以通過索引x獲得任意字段值。此法的強悍之處是可以對數(shù)據(jù)源的全部字段查詢。
4. DataBinder.Eval(e.Row.DataItem, "YourDataFieldName").ToString()。
此法仍然基于e.Row.DataItem,其特點是更快捷的獲得數(shù)據(jù)源的任意字段值,因為使用了DataBinder.Eval(),需要注意的是不要寫錯字段名稱。
5. 將e.Row.DataItem轉(zhuǎn)化為自定義類型,實現(xiàn)數(shù)據(jù)類型化。
例如:
DSRequestTracking.TB_RequestTrackingRow rtrow=(DSRequestTracking.TB_RequestTrackingRow)((DataRowView)e.Row.DataItem).Row;
RequestStatusDropDownList1.SelectedValue = rtrow.StatusID.ToString();
其中DSRequestTracking是通過DSRequestTracking.xsd文件在工程中自定義的強類型DataSet, TB_RequestTrackingRow是VS自動為此強類型生成的創(chuàng)建數(shù)據(jù)行對象的方法。通過此法,實現(xiàn)了類型化數(shù)據(jù)的廣泛應(yīng)用。數(shù)據(jù)類型化的好處很多,顯而易見的便是傳一個類型數(shù)據(jù)給方法,取代一大堆參數(shù),另外就是再也不會發(fā)生字段名拼寫錯誤的情況。
綜述,以上方法各有千秋,恰當(dāng)擇而用之,一定能滿足開發(fā)過程中的不同需求。