2013-02-26 41 views
0

例如,我有一個記錄來源(Id,Name)。我想將它綁定到GridView。但是,我想在將每條記錄添加到GridView之前對其進行格式化,例如,我想爲字段爲「名稱」的所有記錄編寫前綴「test」。我聽說我需要使用onRowDataBound事件,但我不知道如何。如何在gridview中加載格式化數據?

+1

查看本文 - 它提供了非常直觀的示例:http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx – Nogard 2013-02-26 10:33:50

回答

0

那麼你可以做到這一點通過捕獲結合事件。

YourGrid.DataBound += YourGrid_RowDataBound 

void YourGrid_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     e.Row.Cells[1].Text = "test_" + e.Row.Cells[1].Text; 
    } 
} 
0

在您的gridview列中使用DataFormatString。例如:

<asp:BoundField DataField="name" DataFormatString="test_{0}" HeaderText="name" 
        HtmlEncode="False" SortExpression="name" /> 

這將導致:

test_YourData