2014-04-07 17 views
6

Ho un GridView che recupera i valori da un DataSource che unisce due tabelle e ho bisogno di ottenere quei valori nel code-behind e passarli come String. Qualche idea su quale sarebbe l'approccio migliore? Qui di seguito è il mio GridView in aspx:Come ottenere i valori GridView da asp: BoundField?

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

risposta

12
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; 

} 
1

Come sapete, la Grid View ha più righe. Quindi puoi ottenere i valori da una qualsiasi delle righe.

Ottenere usando i tasti dati:

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

Ottenerlo dalle cellule:

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

è anche possibile ottenere i dati in caso di dati Row Bound:

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(); 
    } 
} 
2
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); 
    } 
} 
Problemi correlati