iContact のデータをDevExpress Data Grid にデータバインドする。
CData API Driver for ADO.NET は、サードパーティ製コントロールと組み合わせて使える標準的なADO.NET データアクセスコンポーネントを備えています。通常のADO.NET データバインドの手順に沿って進めるだけで、UI コントロールからリアルタイムデータの取得・表示を実現できます。この記事では、CData のコンポーネントを使って DevExpress UI コントロール(Windows Forms およびWeb コントロール) にデータバインドする方法を紹介します。今回は、リアルタイムのデータを可視化するチャートにデータバインドしていきます。
まず、Profile 接続プロパティをディスク上の iContact プロファイルの場所に設定します(例:C:\profiles\IContact.apip)。次に、ProfileSettings 接続プロパティを iContact の接続文字列に設定します(以下を参照)。
iContact API プロファイル設定
iContact アカウントで「Settings & Billing > Integrations」に移動し、Custom API Integration を作成します。AppId、Username、Password、AccountId はすべて、その連携の Getting Connected ページで確認できます。
Windows Forms コントロール
下のコードでは、iContact でDevExpress のチャートに追加する方法を説明します。APIDataAdapter はチャートコントロールのSeries プロパティにバインドします。コントロールのDiagram プロパティはx 軸とy 軸をカラム名として定義します。
using (APIConnection connection = new APIConnection(
"Profile=C:\profiles\IContact.apip;ProfileSettings='AppId=your_app_id;Username=your_username;Password=your_password;AccountId=your_account_id';")) {
APIDataAdapter dataAdapter = new APIDataAdapter(
"SELECT ClientFolderId, CampaignId FROM Campaigns WHERE Name = 'Summer Newsletter 2024'", connection);
DataTable table = new DataTable();
dataAdapter.Fill(table);
DevExpress.XtraCharts.Series series = new DevExpress.XtraCharts.Series();
chartControl1.Series.Add(series);
DataTable table = new DataTable();
series.ValueDataMembers.AddRange(new string[] { "CampaignId" });
series.ArgumentScaleType = DevExpress.XtraCharts.ScaleType.Qualitative;
series.ArgumentDataMember = "ClientFolderId";
series.ValueScaleType = DevExpress.XtraCharts.ScaleType.Numerical;
chartControl1.Legend.Visibility = DevExpress.Utils.DefaultBoolean.False;
((DevExpress.XtraCharts.SideBySideBarSeriesView)series.View).ColorEach = true;
}
Web コントロール
下のコードではiContact でDevExpress Web を操作するための設定方法を説明します。APIDataAdapter はチャートのSeries プロパティにバインドします。Diagram プロパティはx 軸とy 軸をカラム名として定義します。
using DevExpress.XtraCharts;
using (APIConnection connection = new APIConnection(
"Profile=C:\profiles\IContact.apip;ProfileSettings='AppId=your_app_id;Username=your_username;Password=your_password;AccountId=your_account_id';"))
{
APIDataAdapter APIDataAdapter1 = new APIDataAdapter("SELECT ClientFolderId, CampaignId FROM Campaigns WHERE Name = 'Summer Newsletter 2024'", connection);
DataTable table = new DataTable();
APIDataAdapter1.Fill(table);
DevExpress.XtraCharts.Series series = new Series("Series1", ViewType.Bar);
WebChartControl1.Series.Add(series);
DataTable table = new DataTable();
series.ValueDataMembers.AddRange(new string[] { "CampaignId" });
series.ArgumentScaleType = ScaleType.Qualitative;
series.ArgumentDataMember = "ClientFolderId";
series.ValueScaleType = ScaleType.Numerical;
((DevExpress.XtraCharts.SideBySideBarSeriesView)series.View).ColorEach = true;
}