C# 如何将QuickBooks报告结果返回到WPF DataGrid项?
我正在QuickBooks桌面上查询特定日期范围内的事务。我正在使用样板或类似的代码将报表数据分配给事务类,并将事务类的属性添加到WPF数据网格中,以供最终用户查看 我的AllItemsDataGrid在下面的XAML中的用户控件内实例化:C# 如何将QuickBooks报告结果返回到WPF DataGrid项?,c#,wpf,quickbooks,qbfc,C#,Wpf,Quickbooks,Qbfc,我正在QuickBooks桌面上查询特定日期范围内的事务。我正在使用样板或类似的代码将报表数据分配给事务类,并将事务类的属性添加到WPF数据网格中,以供最终用户查看 我的AllItemsDataGrid在下面的XAML中的用户控件内实例化: <DataGrid Name="AllItemsDataGrid" AutoGenerateColumns="False" CanUserAddRows="False" IsReadOnly="True" Loaded="EntriesDataGrid
<DataGrid Name="AllItemsDataGrid" AutoGenerateColumns="False" CanUserAddRows="False" IsReadOnly="True" Loaded="EntriesDataGrid_OnLoaded">
</DataGrid>
我创建报告请求、响应、报告返回,并将事务填充到DataGrid中,如下所示:
private void EntriesDataGrid_OnLoaded(object sender, RoutedEventArgs e)
{
try
{
if (WalkReportRet != null)
{
var transactions = new List<Transaction>();
var transaction = new Transaction
{
TxnType = transaction.TxnType.ToString(),
TxnNumber = transaction.TxnNumber.ToString(),
TxnDate = transaction.TxnDate.Date(),
TxnName = transaction.TxnName.ToString(),
GLAccount = transaction.GLAccount.ToString(),
TxnAmount = transaction.TxnAmount.ToString(),
TxnCleared = transaction.TxnCleared.ToString(),
TxnSplit = transaction.TxnSplit.ToString()
};
}
return;
AllItemsDataGrid.ItemsSource = transactions;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
}
public void RunQuery(object sender, RoutedEventArgs e)
{
bool sessionBegun = false;
bool connectionOpen = false;
QBSessionManager sessionManager = null;
try
{
//Create Session Manager
sessionManager = new QBSessionManager();
//Create the request to obtain the Profit and Loss Summary Report
IMsgSetRequest requestMsgSet = sessionManager.CreateMsgSetRequest("US", 13, 0);
requestMsgSet.Attributes.OnError = ENRqOnError.roeContinue;
BuildGeneralDetailReportQueryRq(requestMsgSet);
//Connect to QB Desktop and begin a Session
sessionManager.OpenConnection(
@"C:\Users\Public\Public Documents\Intuit\QuickBooks\Company Files\MyCompany.QBW",
"MyCompany");
connectionOpen = true;
sessionManager.BeginSession("", ENOpenMode.omDontCare);
sessionBegun = true;
IMsgSetResponse responseMsgSet = sessionManager.DoRequests(requestMsgSet);
sessionManager.EndSession();
sessionBegun = false;
sessionManager.CloseConnection();
connectionOpen = false;
WalkGeneralDetailReportQueryRs(responseMsgSet);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
finally
{
if (sessionBegun)
{
sessionManager.EndSession();
}
if (connectionOpen)
{
sessionManager.CloseConnection();
}
}
}
void BuildGeneralDetailReportQueryRq(IMsgSetRequest requestMsgSet)
{
IGeneralDetailReportQuery txQuery = requestMsgSet.AppendGeneralDetailReportQueryRq();
txQuery.GeneralDetailReportType.SetValue(ENGeneralDetailReportType.gdrtTxnListByDate);
txQuery.DisplayReport.SetValue(true);
txQuery.ORReportPeriod.ReportPeriod.FromReportDate.SetValue(StartDate.Value);
txQuery.ORReportPeriod.ReportPeriod.ToReportDate.SetValue(EndDate.Value);
}
void WalkGeneralDetailReportQueryRs(IMsgSetResponse responseMsgSet)
{
if (responseMsgSet == null) return;
IResponseList responseList = responseMsgSet.ResponseList;
if (responseList == null) return;
//if we sent only one request, there is only one response, we'll walk the list for this sample
for (int i = 0; i < responseList.Count; i++)
{
IResponse response = responseList.GetAt(i);
//check the status code of the response, 0=ok, >0 is warning
if (response.StatusCode >= 0)
{
//the request-specific response is in the details, make sure we have some
if (response.Detail != null)
{
//make sure the response is the type we're expecting
ENResponseType responseType = (ENResponseType) response.Type.GetValue();
if (responseType == ENResponseType.rtGeneralDetailReportQueryRs)
{
//upcast to more specific type here, this is safe because we checked with response.Type check above
IReportRet ReportRet = (IReportRet) response.Detail;
WalkReportRet(ReportRet);
}
}
}
}
}
void WalkReportRet(IReportRet ReportRet)
{
if (ReportRet == null) return;
if (ReportRet.ReportData != null)
{
if (ReportRet.ReportData.ORReportDataList != null)
{
for (int i35 = 0; i35 < ReportRet.ReportData.ORReportDataList.Count; i35++)
{
IORReportData ORReportData = ReportRet.ReportData.ORReportDataList.GetAt(i35);
if (ORReportData.DataRow != null)
{
if (ORReportData.DataRow != null)
{
if (ORReportData.DataRow.RowData != null)
{
}
if (ORReportData.DataRow.ColDataList != null)
{
for (int i36 = 0; i36 < ORReportData.DataRow.ColDataList.Count; i36++)
{
IColData ColData = ORReportData.DataRow.ColDataList.GetAt(i36);
}
}
}
}
if (ORReportData.TextRow != null)
{
if (ORReportData.TextRow != null)
{
}
}
if (ORReportData.SubtotalRow != null)
{
if (ORReportData.SubtotalRow != null)
{
if (ORReportData.SubtotalRow.RowData != null)
{
}
if (ORReportData.SubtotalRow.ColDataList != null)
{
for (int i37 = 0; i37 < ORReportData.SubtotalRow.ColDataList.Count; i37++)
{
IColData ColData = ORReportData.SubtotalRow.ColDataList.GetAt(i37);
}
}
}
}
if (ORReportData.TotalRow != null)
{
if (ORReportData.TotalRow != null)
{
if (ORReportData.TotalRow.RowData != null)
{
}
if (ORReportData.TotalRow.ColDataList != null)
{
for (int i38 = 0; i38 < ORReportData.TotalRow.ColDataList.Count; i38++)
{
IColData ColData = ORReportData.TotalRow.ColDataList.GetAt(i38);
}
}
}
}
}
}
}
}
private void EntriesDataGrid_onload(对象发送方,RoutedEventArgs e)
{
尝试
{
如果(WalkReportRet!=null)
{
var事务=新列表();
var事务=新事务
{
TxnType=transaction.TxnType.ToString(),
TxnNumber=transaction.TxnNumber.ToString(),
TxnDate=transaction.TxnDate.Date(),
TxnName=transaction.TxnName.ToString(),
GLAccount=transaction.GLAccount.ToString(),
TxnAmount=transaction.TxnAmount.ToString(),
TxnCleared=transaction.TxnCleared.ToString(),
TxnSplit=transaction.TxnSplit.ToString()
};
}
返回;
AllItemsDataGrid.ItemsSource=事务;
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message,“Error”);
}
}
public void RunQuery(对象发送方,RoutedEventArgs e)
{
bool sessionbegined=false;
bool connectionOpen=false;
QBSessionManager会话管理器=null;
尝试
{
//创建会话管理器
sessionManager=newqbsessionmanager();
//创建请求以获取损益汇总报告
IMsgSetRequest requestMsgSet=sessionManager.CreateMsgSetRequest(“US”,13,0);
requestMsgSet.Attributes.OnError=ENRqOnError.roeContinue;
BuildGeneralDetailReportQueryRq(requestMsgSet);
//连接到QB桌面并开始会话
sessionManager.OpenConnection(
@“C:\Users\Public\Public Documents\Intuit\QuickBooks\Company Files\MyCompany.QBW”,
“MyCompany”);
connectionOpen=true;
sessionManager.BeginSession(“,ENOpenMode.omDontCare);
sessionbegined=true;
IMsgSetResponse responsemssgset=sessionManager.DoRequests(requestMsgSet);
sessionManager.EndSession();
sessionbegined=false;
sessionManager.CloseConnection();
connectionOpen=false;
WalkGeneralDetailReportQueryRs(响应集);
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message,“Error”);
}
最后
{
如果(会议开始)
{
sessionManager.EndSession();
}
如果(连接打开)
{
sessionManager.CloseConnection();
}
}
}
void BuildGeneralDetailReportQueryRq(imsgsetRequestMsgSet)
{
IGeneralDetailReportQuery txQuery=requestMsgSet.AppendGeneralDetailReportQueryRq();
txQuery.GeneralDetailReportType.SetValue(ENGeneralDetailReportType.gdrtTxnListByDate);
txQuery.DisplayReport.SetValue(true);
txQuery.ORReportPeriod.ReportPeriod.FromReportDate.SetValue(StartDate.Value);
txQuery.ORReportPeriod.ReportPeriod.ToReportDate.SetValue(EndDate.Value);
}
void WalkGeneralDetailReportQueryRs(IMsgSetResponse响应emsgset)
{
if(responsemsgsset==null)返回;
IResponseList responseList=responseMsgSet.responseList;
if(responseList==null)返回;
//如果我们只发送了一个请求,那么只有一个响应,我们将遍历此示例的列表
for(int i=0;i0表示警告
如果(response.StatusCode>=0)
{
//请求特定的响应在详细信息中,请确保我们有一些
if(response.Detail!=null)
{
//确保响应是我们期望的类型
ENResponseType responseType=(ENResponseType)response.Type.GetValue();
if(responseType==ENResponseType.rtGeneralDetailReportQueryRs)
{
//在这里向上转换到更具体的类型,这是安全的,因为我们使用上面的response.type检查进行了检查
IReportRet ReportRet=(IReportRet)response.Detail;
WalkReportRet(ReportRet);
}
}
}
}
}
void WalkReportRet(IReportRet ReportRet)
{
if(ReportRet==null)返回;
if(ReportRet.ReportData!=null)
{
if(ReportRet.ReportData.ORReportDataList!=null)
{
for(int i35=0;i35