Tuesday, December 8, 2009

The Value of Datakeys in GridView RowDataBound

To find the value of your gridview attribute Datakeys use this C# code

protected void gvSummary_RowDataBound(object sender, GridViewRowEventArgs e)

{
   DataTable dt = new DataTable();
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
      //find the invoice number - which is the Datakeys
      int inv = Convert.ToInt32(((GridView)sender).DataKeys[e.Row.RowIndex].Value);

    //find the charges grid - populated based on the invoice number
    GridView gvCh = (GridView)e.Row.FindControl("gvCharges");

    dt = maData.TrapRetrievalInvoiceChargesGet(inv);
    //populate a nested grid
    gvCh.DataSource = dt;
    gvCh.DataBind();

  }
}


ASP Code
asp:GridView ID="gvSummary" runat="server" DataKeyNames="Invoice"

...

No comments:

Post a Comment