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 these values โ€‹โ€‹in encoding and pass them as 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> 
+6
source share
3 answers
 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; } 
+12
source
 foreach (GridViewRow gr in userGrid.Rows) { CheckBox check = (CheckBox)gr.FindControl("chkSelect"); if (check.Checked == true) { string order = userGrid.Rows[gr.RowIndex].Cells[3].Text; gl.updatetracking (order); } } 
+2
source

As you know, Grid View has several lines. This way you can get values โ€‹โ€‹from any of the strings.

Receiving data using data keys:

 var ponumber = GridView1.DataKeys[rowIndex].Values[0].ToString(); var sitename = GridView1.DataKeys[rowIndex].Values[1].ToString(); 

Getting it by cells:

 // easiest way, but not the most recommended. var ponumber = GridView1.Rows[rowIndex].Cells[0].Text; // try 0 or 1 

You can also get data in the Row Data Bound Event:

 protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e) { if(e.Row.RowType == DataControlRowType.DataRow) { var ponumber = GridView1.DataKeys[e.Row.RowIndex].Values[0].ToString(); var sitename = GridView1.DataKeys[e.Row.RowIndex].Values[1].ToString(); } } 
+1
source

All Articles