I am working on asp.net web app .net version 4. I have a data table which look like:
FCTR_GRP CUST_TIER CUST_TIER
DSC SW Tier 1 1.000000
DSC SW Tier 2 1.000000
DSC SW Tier 3 1.000000
DSC SW Tier 4 1.000000
Using above datatable which I am reading from Netezza database I want to convert it into a datatable like:
FCTR_GRP TIER_1 TIER_2 TIER_3 TIER_4
DSC SW 1.000000 1.000000 1.000000 1.000000
and then bind it to gridview. As tier can be added later so gridview need to be dynamic. Till now I reached till here. I am able to convert first datatable like this:
FCTR_GRP TIER_1 TIER_2 TIER_3 TIER_4
DSC SW 1.000000
DSC SW 1.000000
DSC SW 1.000000
DSC SW 1.000000
Code Used as(Not Happy with code :( but no other option)
DataTable CustomerCurrentFactorDataTable = new DataTable();
CustomerCurrentFactorDataTable.Columns.Add("Factor Group", typeof(System.String));
foreach (DataRow datarow in dt.Rows)
{
CustomerCurrentFactorDataTable.Columns.Add(datarow["CUST_TIER"].ToString().ToUpper(), typeof(System.String));
}
GridViewSample.DataSource = CustomerCurrentFactorDataTable;
GridViewSample.DataBind();
#region
string query = "SELECT FCTR_GRP, CUST_TIER, CUST_FCTR FROM DBO.CUST_FCTR";
#endregion
dt = getData.GetDataTable(query);
DataRow dr = CustomerCurrentFactorDataTable.NewRow();
int columnCount = CustomerCurrentFactorDataTable.Columns.Count;
int internalCounter = 0;
int count = 0;
bool flag=false;
int loopoutcounter = 0;
foreach (DataRow dataRow in dt.Rows)
{
count = 0;
if (loopoutcounter == 2)
{
loopoutcounter = 0;
CustomerCurrentFactorDataTable.Rows.Add(dr);
dr = CustomerCurrentFactorDataTable.NewRow();
//dr.Delete();
}
foreach (var cell in dataRow.ItemArray)
{
if (count == 0)
{
dr[count] = cell.ToString();
loopoutcounter++;
count++;
}
if (flag)
{
dr[internalCounter] = cell.ToString();
flag = false;
loopoutcounter++;
}
foreach (DataColumn column in CustomerCurrentFactorDataTable.Columns)
{
if (column.ColumnName == cell.ToString().ToUpper())
{
flag = true;
internalCounter = column.Ordinal;
continue;
}
}
}
Any help will be useful if we can convert last datatable like 2nd datatable or any batter option.Thanks in Advance.