Here is some simple code that you can use to bind your GridView to your MS CRM BusinessEntityCollection data.

 

// Retrieve the accounts.
BusinessEntityCollection accounts = service.RetrieveMultiple(query);

// Define your Dataset and DataTable
DataSet ds = new DataSet();
DataTable dt = new DataTable();

// Set Column headers for the DataTab.e
dt.Columns.Add("name");
dt.Columns.Add("Address1");
dt.Columns.Add("Address2");
dt.Columns.Add("City");
dt.Columns.Add("MainPhone");
dt.Columns.Add("accountid");

//Iterate the BusinessEntityColleciton
foreach (BusinessEntity Account in accounts.BusinessEntities)
{
    account a = (account)Account;
    DataRow dr = dt.NewRow();
    dr["name"] = a.name;
    dr["Address1"] = a.address1_line1;
    dr["Address2"] = a.address1_line2;
    dr["City"] = a.address1_city;
    dr["MainPhone"] = a.telephone1;
    dr["accountid"] = a.accountid.Value;
    dt.Rows.Add(dr);
}

ds.Tables.Add(dt);

// You are now ready to bind your DataSet to your GridView
GridView1.DataSource = ds;
GridView1.DataBind();
Advertisements