I'm trying to convert data table to smart-table in C#. Basically stored procedure returns dataTable and I need to translate it to JavaScript object, but I don't know what column names I will have. Smart-table is angular-framework which required specific format like below egample:
$scope.columnCollection = ["firstName", "lastName", "birthDate", "balance", "email"];
$scope.rowCollection = [
{ firstName: 'Laurent', lastName: 'Renard', birthDate: new Date('1987-05-21'), balance: 102, email: 'whatever@gmail.com' },
{ firstName: 'Blandine', lastName: 'Faivre', birthDate: new Date('1987-04-25'), balance: -2323.22, email: 'oufblandou@gmail.com' },
{ firstName: 'Francoise', lastName: 'Frere', birthDate: new Date('1955-08-27'), balance: 42343, email: 'raymondef@gmail.com' }
];
So I have created this code to translate it:
public class SmartTable
{
public IList<string> ColumnCollection { get; set; }
public IList<Object> RowCollection { get; set; }
}
public SmartTable GetTableData(string sp, int widgetId = 0)
{
var connection = ConfigurationManager.ConnectionStrings["EFDbContext"].ConnectionString;
using (var da = new SqlDataAdapter("exec " + sp, connection))
{
DataTable dt = new DataTable();
da.Fill(dt);
da.FillSchema(dt, SchemaType.Mapped);
IList<string> colColls = new List<string>();
foreach(DataColumn dc in dt.Columns)
{
colColls.Add(dc.Caption);
}
IList<Object> rowColls = new List<Object>();
foreach (DataRow dr in dt.Rows)
{
dynamic rowColl = new Object();
foreach (DataColumn dc in dt.Columns)
{
rowColl.Add(dc.Caption, dr[dc].ToString());
}
rowColls.Add(rowColl);
}
SmartTable smartTable = new SmartTable
{
ColumnCollection = colColls,
RowCollection = rowColls
};
return smartTable;
};
}
But when I run this I get error "'object' does not contain a definition for 'Add'" and the row below is highlighted:
rowColl.Add(dc.Caption, dr[dc].ToString());
Any idea how to resolve this?
EDIT
If I would expect a static data all the time I would do it like below:
public class Row
{
public string firstName { get; set; }
public string lastName { get; set; }
public string birthDate { get; set; }
...
}
but the problem is that I don't know what and how many columns I will have.
Your issue is here:
foreach (DataRow dr in dt.Rows)
{
dynamic rowColl = new Object();
foreach (DataColumn dc in dt.Columns)
{
rowColl.Add(dc.Caption, dr[dc].ToString());
}
rowColls.Add(rowColl);
}
I'm not sure why you're using dynamic -- just use a dictionary, like so:
var dictionary = new Dictionary<string, List<object>>();
foreach (DataColumn dataColumn in dataTable.Columns)
{
var columnValueList = new List<object>();
foreach (DataRow dataRow in dataTable.Rows)
{
columnValueList.Add(dataRow[dataColumn.ColumnName]);
}
dictionary.Add(dataColumn.ColumnName, columnValueList);
}
@returnsvoid answer doesn't give the correct data structure but It gave me the point how to do it. If anyone interested with the exact answer:
foreach(DataColumn dc in dt.Columns)
{
colColl.Add(dc.Caption);
}
IList<Dictionary<string, string>> rowColl = new List<Dictionary<string, string>>();
foreach (DataRow dr in dt.Rows)
{
Dictionary<string, string> row = new Dictionary<string, string>();
foreach (DataColumn dc in dt.Columns)
{
row.Add(dc.ColumnName, dr[dc].ToString());
}
rowColl.Add(row);
}
SmartTable smartTable = new SmartTable
{
ColumnCollection = colColl,
RowCollection = rowColl
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.