Ayman Ayman - 6 days ago 4x
ASP.NET (C#) Question

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


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

mww mww

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