Ayman
Ayman

Reputation: 99

Convert gridview row values to string

If i have grid view that has the following data

TechnicianID FirstName LastName

1            yasser    jon

2            ali       kamal

How can convert these grid row values into string in this below format

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>

Upvotes: 1

Views: 2312

Answers (2)

Anas Al-Qudah
Anas Al-Qudah

Reputation: 129

You can bind Label like that :

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

Upvotes: 1

M. Wiśnicki
M. Wiśnicki

Reputation: 6203

You can use Foreach loop on rows in your DataGridView and get values. This sample show how you can resolve your problem.

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

Upvotes: 1

Related Questions