【问题标题】:GridView: convert data before bindind to GridViewGridView:在绑定到GridView之前转换数据
【发布时间】:2009-10-13 08:52:29
【问题描述】:

我的 GridView 使用以下数据绑定语法来绑定数据。 我希望在真正绑定到 GridView 之前进行数据转换。例如,如果 VendorID int 属性(我使用 List 作为数据源)为 0,我希望在该字段上显示空字符串。 我可以利用什么样的事件?如果你能推荐任何代码示例?

非常感谢。

        <asp:TemplateField HeaderText="Vendor ID">
            <ItemStyle Width="1%" BorderColor="#efefef" BorderWidth="1px" />
            <ItemTemplate>
                <asp:Label runat="server" ID="lblNo" Text='<%# Bind("VendorID") %>'></asp:Label>
            </ItemTemplate>
            <HeaderStyle BorderColor="#efefef" />
        </asp:TemplateField>

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    Gridview 的 RowDataBound 事件可以用于此。

    void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
      {
    
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
          // do wat ever u like to do here. 
          // formating, checking, changing data, etc....
    
          e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";
    
        }
    
      }
    

    【讨论】:

      【解决方案2】:

      您可以在 RowDataboundEvent

      中使用以下代码
        if (DataBinder.Eval(args.Row.DataItem, "VendorID").ToString() == "0")
          {
              ((Label)args.Row.FindControl("lblNo")).Text = "";
          }
          else
          {
              ((Label)args.Row.FindControl("lblNo")).Text = DataBinder.Eval(args.Row.DataItem, "VendorID").ToString();
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-10-09
        • 2014-02-09
        • 2012-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多