Please excuse me as I am not really sure if I am asking this the correct way. I need to get the value of a field, when the field name is "dynamic" and is equal to another variable.
I tried this: Here are my using statements:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Interop.QBFC13;
using CtixQBIntegration.Session_Framework;
using CTixQBIntegration.ExtensionMethods;
using System.Data.OleDb;
using System.IO;
using CTixQBIntegration.Models;
using Newtonsoft.Json;
using System.Linq;
Here is my object:
public class InvoiceMapping
{
public string ID { get; set; }
public string RefNumber { get; set;}
public string CustomerRef { get; set; }
public string PONumber { get; set; }
public string ItemRef { get; set; }
public string Description { get; set; }
public string Quantity { get; set; }
public string UnitOfMeasure { get; set; }
public string Rate { get; set; }
public string Total { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Address3 { get; set; }
public string Address4 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string PostalCode { get; set; }
public string ServiceDate { get; set; }
public string TxnDate { get; set; }
public string Note { get; set; }
}
Here is my code:
DataTable dt = originalDataSource;
DataTable cl = new DataTable();
Invoice invoice = new Invoice();
List<SourceList> invoiceFields = (from prop in invoice.GetType().GetProperties()
select new SourceList()
{
FieldName = prop.Name
}).ToList();
//Check if column names have already been converted
try
{
//Converts Excel Column Names to Quickbooks Column Names
foreach (var item in invoiceFields)
{
cl.Columns.Add(item.FieldName);
}
foreach(var field in invoiceFields)
{
DataRow destRow = cl.NewRow();
foreach(DataRow sourcerow in dt.Rows)
{
foreach(DataColumn col in dt.Columns)
{
string colName = col.ColumnName;
var mappingField = mapping.GetType().GetProperty(field.FieldName).GetValue(colName);
destRow[field.FieldName] = sourcerow[mappingField.ToString()];
}
}
}
dataGridView1.DataSource = null;
dataGridView1.DataSource = cl;
}
I need to get value from the "mapping" object where the name of the field is equal to field.fieldName.
I hate adding so much code, but it may help someone to understand the problem. I have shortened it to show only the first two fields. This works with everything "hardcoded" as below, the problem, is that I need to make it all dynamic so that it works with ANY datsource:
DataTable dt = originalDataSource;
DataTable cl = new DataTable();
cl.Columns.Add("RefNumber");
cl.Columns.Add("CustomerRef");
string refnumber = mapping.RefNumber;
string customerRef = mapping.CustomerRef;
//Check if column names have already been converted
try
{
//Converts Excel Column Names to Quickbooks Column Names
foreach (DataRow sourcerow in dt.Rows)
{
DataRow destRow = cl.NewRow();
foreach (DataColumn col in dt.Columns)
{
if (refnumber != "" && refnumber != null)
{
destRow["RefNumber"] = sourcerow[refnumber];
}
if (customerRef != "" && customerRef != null)
{
destRow["CustomerRef"] = sourcerow[customerRef];
}
}
cl.Rows.Add(destRow);
}
}
Any assistance is greatly appreciated.