Method 1: bind the queried data directly.
<Asp: gridView ID = "GridView1" runat = "server" OnRowDataBound = "GridView1_RowDataBound1" CellPadding = "4" ForeColor = "#333333" GridLines = "Horizontal" AllowPaging = "True" PageSize =" 9 "Width =" 100% "> <FooterStyle BackColor =" # DBE7F6 "ForeColor =" black "/> <Columns> <asp: boundField HeaderText = "" FooterStyle-Width = "20px"> </asp: BoundField> <asp: templateField HeaderText = "I want to leave a message" HeaderStyle-Font-Size = "16"> <ItemTemp Late> <table width = "100%"> <tr> <td style = "text-align: center;"> <asp: label ID = "libTitle" runat = "server" Style = "position: relative" Text = '<% # eval_r ("Title") %>'> </asp: label> </td> </tr> <td style = "text-align: left;"> <asp: label ID = "libContent" runat = "server" Style = "position: relative" Text = '<% # eval_r ("Content") %>'> </asp: label> </td> </tr> <td style = "text-align: right;"> <asp: Label ID = "LibAuthor" runat = "server" Style = "position: relative" Text = '<% # eval_r ("ToName") %>'> </asp: Label> <asp: label ID = "libCreated" runat = "server" Style = "position: relative" Text = '<% # eval_r ("Created") %>'> </asp: label> </td> </tr> </table> </ItemTemplate> </asp: templateField> </Columns> <RowStyle BackColor = "# EFF3FB"/> <SelectedRowStyle BackColor = "# D1DDF1" Font-Bold = "True" ForeColor = "#333333"/> <pagerSt Yle BackColor = "# 2461BF" ForeColor = "White" HorizontalAlign = "Center"/> <HeaderStyle BackColor = "# 00A0DB" Font-Bold = "True" ForeColor = "White "/> <EditRowStyle BackColor = "# 2461BF"/> <AlternatingRowStyle BackColor = "White"/> <PagerSettings Visible = "False"/> </asp: gridView> public void GridView1Show () {SPList list = null; try {list = SPContext. current. web. lists ["Messageboard"];} catch (Exception err) {// Label lable = this. gridView1.FooterRow. findControl ("libMessage") as Label; libMessage. text = "there is no message board list in the current website. Please contact the Administrator to create it. error details: "+ err. message;} SPQuery query = new SPQuery (); query. query = @ "<Where> <Eq> <FieldRef Name = 'author'/> <Value Type = \ 'text \ '>" + getme () + "</Value> </Eq> </Where> <OrderBy> <FieldRef Name = 'created 'Ascending = 'false'> </FieldRef> </OrderBy> "; try {SPListItemCollection items = List. getItems (query); if (items. count <1) {// Label lable = this. gridView1.FooterRow. findControl ("libMessage") as Label; libMessage. text = "you did not give messages to others; or you have read and deleted messages to others! "; BtnFirst. visible = false; // hide the pagination button btnPrev. visible = false; btnNext. visible = false; btnLast. visible = false;} else {GridView1.DataSource = CreateDataTable (); GridView1.DataBind () ;}} catch (Exception err) {// Label lable = this. gridView1.FooterRow. findControl ("libMessage") as Label; libMessage. text = "An error occurred. Contact the administrator. cause "+ err. message ;}}
Or <% #
DataBinder. eval_r (Container. DataItem, "database Field") %> <% #
DataBinder. eval_r (Container. DataItem, "ColumnName") %>
<% #
DataBinder. eval_r (Container. DataItem, "ColumnName", null) %>
<% #
DataBinder. eval_r (Container, "DataItem. ColumnName", null) %> other usage
<% #
(DataRowView) Container. DataItem) ["ColumnName"] %>
<% #
(DataRowView) Container. DataItem). Row ["ColumnName"] %>
<% #
(DataRowView) Container. DataItem) ["adtitle"] %>
<% #
(DataRowView) Container. DataItem) [n] %>
<% #
(DbDataRecord) Container. DataItem) [0] %>
<% #
(Custom type) Container. DataItem). Attribute. ToString ()
%> // If the attribute is of the string type, you do not need ToString ().
Note: FirstName
And LastName are all database fields. There is no format processing or operation on the result directly bound here. You can also see that multiple bindings can be directly arranged in the template. Method 2: Calculate the operation result <asp: TemplateField
HeaderText = "discount">
<ItemTemplate>
<% # DataBinder. eval_r (Container. DataItem,
"Discount"). ToString () = "0 "? "Do not press": DataBinder. eval_r (Container. DataItem,
"Discount"). ToString () %> Discount
</ItemTemplate>
</Asp: TemplateField> Note: The result is determined by using the ternary operator. This method is applicable to scenarios where the value is False if the value is not True. Method 3: format the result. <% # DataBinder. eval_r (Container. DataItem,
"Discount_beginDate", "{0: yyyy-m-d}"). ToString () =
"1900-0-1 "? "None": DataBinder. eval_r (Container. DataItem, "Discount_beginDate ",
"{0: yyyy-m-d}"). ToString () %> note: the query date is formatted and then computed. Method 4: bind the backend Method
Code
<Asp: TemplateField HeaderText = "popularity">
<ItemTemplate>
ItemTemplate>
Asp: TemplateField>
// Background method:
Protected string GetStatusPicture (object dataItem)
{
Int units = Int32.Parse (DataBinder. eval_r (dataItem, "PopularLevel"). ToString ());
String strNum = "";
Switch (units)
{
Case 0:
StrNum = "0 Stars ";
Break;
Case 1:
StrNum = "1 star ";
Break;
Case 2:
StrNum = "2 stars ";
Break;
Case 3:
StrNum = "3 stars ";
Break;
Case 4:
StrNum = "4 stars ";
Break;
Case 5:
StrNum = "5 stars ";
Break;
}
Return strNum;
}
// Description: the background method must be Public or Proteced. The accepted parameter is preferably of the Object type, and then is converted in the method.