Jacman Jacman - 3 years ago 174
ASP.NET (C#) Question

How to get GridView values from asp:BoundField?

I have a

GridView
that retrieves values from a
DataSource
that joins two tables, and I need to get those values in the code-behind and pass them as a
String
. Any ideas on what would be the best approach? Below is my
GridView
in aspx:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDsPoNumber" EnableModelValidation="True" Visible="True"
DataKeyNames="PO_NUMBER,SITE_NAME">
<Columns>
<asp:BoundField DataField="PO_NUMBER" HeaderText="PO_NUMBER"
SortExpression="PO_NUMBER" />
<asp:BoundField DataField="SITE_NAME" HeaderText="SITE_NAME"
SortExpression="SITE_NAME" />
</Columns>
</asp:GridView>

Answer Source
foreach (GridViewRow gr in GridView1.Rows)
{

   string cell_1_Value= GridView1.Rows[gr.RowIndex].Cells[0].Text; 
   string cell_2_Value= GridView1.Rows[gr.RowIndex].Cells[1].Text; 

}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download