将gridview行值转换为字符串

时间:2016-10-16 10:17:11

标签: c# asp.net gridview

如果我的网格视图包含以下数据

TechnicianID FirstName LastName

1            yasser    jon

2            ali       kamal

如何将这些网格行值转换为以下格式的字符串

yasser jon , ali kamal

GridView的

<asp:GridView ID="gridtechnicians" CssClass="hidden" AutoGenerateColumns="false" runat="server">
        <Columns>
            <asp:BoundField DataField="TechnicianID" HeaderText="TechnicianID" SortExpression="TechnicianID" />
            <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
            <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
        </Columns>
</asp:GridView>

2 个答案:

答案 0 :(得分:1)

您可以对Foreach中的行使用DataGridView循环并获取值。此示例显示了如何解决您的问题。

 string yourString = String.Empty;

 foreach (GridViewRow rowDatos in this.gridtechnicians.Rows)
    {
      if (rowDatos.RowType == DataControlRowType.DataRow)
      {
      string firstName=gridtechnicians.DataKeys[rowDatos.RowIndex].Values[1].ToString();
      string lastName=gridtechnicians.DataKeys[rowDatos.RowIndex].Values[2].ToString();  

      yourString += firstName+" "+lastName  
      }
    }

答案 1 :(得分:1)

你可以像这样绑定Label

<asp:Label ID="lblFullName" runat="server" Text='<%# Eval("FirstName") + " " + Eval("LastName") %>' />