GridView 模板列中的数据绑定

方法一:直接绑定查询的数据

 
<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="我要留言" HeaderStyle-Font-Size="16">

                            <ItemTemplate>

                                <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>

                                    <tr>

                                        <td style="text-align: left;">

                                            <asp:Label ID="libContent" runat="server" Style="position: relative" Text='<%# eval_r("Content") %>'></asp:Label>

                                        </td>

                                    </tr>

                                    <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" />

                    <PagerStyle 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 = "当前网站中没有留言板列表,请联系管理员创建.出错详细信息:" + 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 = "您没有给别人留言;或者您给别人的留言别人已读并且删除!";

                    btnFirst.Visible = false;//隐藏分页按钮

                    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 = "出现错误,请联系管理员.原因" + err.Message;

            }





        }





 

 
 
<%#
DataBinder.eval_r(Container.DataItem,"数据库字段")%>
 
<%#
DataBinder.eval_r(Container.DataItem,
"ColumnName") %>
<%#
DataBinder.eval_r(Container.DataItem,
"ColumnName", null) %>
<%#
DataBinder.eval_r(Container,
"DataItem.ColumnName", null) %>
 
其他用法
<%#
((DataRowView)Container.DataItem)["ColumnName"] %>
<%#
((DataRowView)Container.DataItem).Row["ColumnName"] %>
<%#
((DataRowView)Container.DataItem)["adtitle"] %>
<%#
((DataRowView)Container.DataItem)[n] %>
<%#
((DbDataRecord)Container.DataItem)[0] %>
<%#
(((自定义类型)Container.DataItem)).属性.ToString()
%>//如果属性为字符串类型就不用ToString()了
 

说明:FirstName  
,LastName皆为数据库字段。这里直接绑定没有对结果做任何格式处理和运算。同时你也可以看到,可以直接在模板里布置多个绑定。
 
方法二:对运算结果进行运算
 
<asp:TemplateField
HeaderText="打折">
                           
<ItemTemplate>
                               
<%# DataBinder.eval_r(Container.DataItem,
"Discount").ToString() == "0"
? "不打" : DataBinder.eval_r(Container.DataItem,
"Discount").ToString()
%>折
                           
</ItemTemplate>
                       
</asp:TemplateField>
 
说明:运用三元运算符,对结果进行判断。这种方法适合那些非True即False的情况。
 
方法三:对结果进行格式化处理
 
<%# DataBinder.eval_r(Container.DataItem,
"Discount_beginDate", "{0:yyyy-m-d}").ToString() ==
"1900-0-1" ? "无" : DataBinder.eval_r(Container.DataItem, "Discount_beginDate",
"{0:yyyy-m-d}").ToString()%>
 
说明:可以看到这里查询日期进行了格式化,然后进行了运算。
 
方法四:绑定后台方法
 
代码
     
       
< asp:TemplateField HeaderText = " 流行程度 " >
< ItemTemplate >

ItemTemplate>
asp:TemplateField>

//后台方法:

protected string GetStatusPicture(object dataItem)
{
int units = Int32.Parse(DataBinder.eval_r(dataItem, "PopularLevel").ToString());
string strNum = "";
switch (units)
{
case 0:
strNum
= "0 颗星";
break;
case 1:
strNum
= "1 颗星";
break;
case 2:
strNum
= "2 颗星";
break;
case 3:
strNum
= "3 颗星";
break;
case 4:
strNum
= "4 颗星";
break;
case 5:
strNum
= "5 颗星";
break;
}
return strNum;
}

//说明:后台方法必须为Public或Proteced,接受的参数最好为Object类型,然后再方法内进行转换。

你可能感兴趣的:(GridView)