dataset
을 피벗 한 다음 그리드보기에 바인딩하고 있습니다. 모든 itemtemplates
값이 피벗 테이블에있는 경우 잘 작동하지만 열이 피벗 테이블에 없으면 Gridview를 바인딩 할 때 오류가 발생합니다. 제발 날 어떤 열이 존재하지 않는 경우 그리드보기에서 그것을 처리 할 것입니다 있지만 그리드보기에 바인딩 오전 itemtemplate
. 아래는 내 격자보기 코드입니다. 사실 "L & D Training%"
데이터 테이블에 열이 없지만 항목 템플릿에 바인딩이있는 경우 gridview를 바인딩하십시오.
<asp:GridView ID="gvCoreUtilization" runat="server" BackColor="White" BorderColor="#cEcFcE"
BorderStyle="Solid" BorderWidth="1px" CellPadding="4" ForeColor="Black" OnRowCreated="grdPivot3_RowCreated"
AutoGenerateColumns="false" OnRowDataBound="grdCoreUtilization_RowDataBound">
<RowStyle BackColor="#F7F7DE" />
<FooterStyle BackColor="#CCCC99" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblRoleID" Text='<%#Eval("RoleId") %>' runat="server" Visible="false"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
SupervisorName
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblSupervisorName" Text='<%#Eval("SupervisorName") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
UserECode
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblUserECode" Text='<%#Eval("UserECode") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
UserName
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblUserName" Text='<%#Eval("UserName") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Designation
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblDesignation" Text='<%#Eval("Designation") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
L & D Training%
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblLDTraining" Text='<%#Eval("L & D Training%") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Non Production%
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblNonProduction" Text='<%#Eval("Non Production%") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Process Support%
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblProcessSupport" Text='<%#Eval("Process Support%") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Process Training%
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblProcessTraining" Text='<%#Eval("Process Training%") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Production%
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblProduction" Text='<%#Eval("Production%") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
System Downtime%
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblSystemDowntime" Text='<%#Eval("System Downtime%") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Grand Total%
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblGrandTotal" Text='<%#Eval("Grand Total%") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
, "Non Production%"
, "Process Support%
", "Process Training%"
, "Production%"
, "System Downtime%"
, "Grand Total%"
내가이 칼럼의 일부 사용자 몇 .FOR ItemTemplate을 바인딩하고있는 피벗 열이 존재하지 않습니다하고 그리드를 바인딩하는 동안 나는 .
private DataTable _SourceTable = new DataTable();
private IEnumerable<DataRow> _Source = new List<DataRow>();
public Pivot(DataTable SourceTable)
{
_SourceTable = SourceTable;
_Source = SourceTable.Rows.Cast<DataRow>();
}
public DataTable PivotData(string DataField, AggregateFunction Aggregate, string[] RowFields, string[] ColumnFields)
{
DataTable dt = new DataTable();
string Separator = ".";
var RowList = _SourceTable.DefaultView.ToTable(true, RowFields).AsEnumerable().ToList();
for (int index = RowFields.Count() - 1; index >= 0; index--)
RowList = RowList.OrderBy(x => x.Field<object>(RowFields[index])).ToList();
// Gets the list of columns .(dot) separated.
var ColList = (from x in _SourceTable.AsEnumerable()
select new
{
Name = ColumnFields.Select(n => x.Field<object>(n))
.Aggregate((a, b) => a += Separator + b.ToString())
})
.Distinct()
.OrderBy(m => m.Name);
//dt.Columns.Add(RowFields);
foreach (string s in RowFields)
dt.Columns.Add(s);
foreach (var col in ColList)
dt.Columns.Add(col.Name.ToString()); // Cretes the result columns.//
foreach (var RowName in RowList)
{
DataRow row = dt.NewRow();
string strFilter = string.Empty;
foreach (string Field in RowFields)
{
row[Field] = RowName[Field];
strFilter += " and " + Field + " = '" + RowName[Field].ToString() + "'";
}
strFilter = strFilter.Substring(5);
foreach (var col in ColList)
{
string filter = strFilter;
string[] strColValues = col.Name.ToString().Split(Separator.ToCharArray(), StringSplitOptions.None);
for (int i = 0; i < ColumnFields.Length; i++)
filter += " and " + ColumnFields[i] + " = '" + strColValues[i] + "'";
row[col.Name.ToString()] = GetData(filter, DataField, Aggregate);
}
dt.Rows.Add(row);
}
return dt;
}
public DataTable PivotAllocationData(string DataField, AggregateFunction Aggregate, string[] RowFields, string[] ColumnFields)
{
DataTable dt = new DataTable();
string Separator = ".";
var RowList = _SourceTable.DefaultView.ToTable(true, RowFields).AsEnumerable().ToList();
for (int index = RowFields.Count() - 1; index >= 0; index--)
RowList = RowList.OrderBy(x => x.Field<object>(RowFields[index])).ToList();
// Gets the list of columns .(dot) separated.
var ColList = (from x in _SourceTable.AsEnumerable()
select new
{
Name = ColumnFields.Select(n => x.Field<object>(n))
.Aggregate((a, b) => a += Separator + b.ToString())
})
.Distinct()
.OrderBy(m => m.Name);
//dt.Columns.Add(RowFields);
foreach (string s in RowFields)
dt.Columns.Add(s);
foreach (var col in ColList)
dt.Columns.Add(col.Name.ToString()); // Cretes the result columns.//
foreach (var RowName in RowList)
{
DataRow row = dt.NewRow();
string strFilter = string.Empty;
foreach (string Field in RowFields)
{
row[Field] = RowName[Field];
strFilter += " and " + Field + " = '" + RowName[Field].ToString() + "'";
}
strFilter = strFilter.Substring(5);
foreach (var col in ColList)
{
string filter = strFilter;
string[] strColValues = col.Name.ToString().Split(Separator.ToCharArray(), StringSplitOptions.None);
for (int i = 0; i < ColumnFields.Length; i++)
filter += " and " + ColumnFields[i] + " = '" + strColValues[i] + "'";
row[col.Name.ToString()] = GetAllocationData(filter, DataField, Aggregate);
}
dt.Rows.Add(row);
}
return dt;
}
/// <summary>
/// Retrives the data for matching RowField value and ColumnFields values with Aggregate function applied on them.
/// </summary>
/// <param name="Filter">DataTable Filter condition as a string</param>
/// <param name="DataField">The column name which needs to spread out in Data Part of the Pivoted table</param>
/// <param name="Aggregate">Enumeration to determine which function to apply to aggregate the data</param>
/// <returns></returns>
private object GetData(string Filter, string DataField, AggregateFunction Aggregate)
{
try
{
DataRow[] FilteredRows = _SourceTable.Select(Filter);
object[] objList = FilteredRows.Select(x => x.Field<object>(DataField)).ToArray();
switch (Aggregate)
{
case AggregateFunction.Average:
return GetAverage(objList);
case AggregateFunction.Count:
return objList.Count();
case AggregateFunction.Exists:
return (objList.Count() == 0) ? "False" : "True";
case AggregateFunction.First:
return GetFirst(objList);
case AggregateFunction.Last:
return GetLast(objList);
case AggregateFunction.Max:
return GetMax(objList);
case AggregateFunction.Min:
return GetMin(objList);
case AggregateFunction.Sum:
return GetSum(objList);
default:
return null;
}
}
catch (Exception ex)
{
return "#Error";
}
}
/// <summary>
/// Retrives the data for matching RowField value and ColumnFields values with Aggregate function applied on them.
/// </summary>
/// <param name="Filter">DataTable Filter condition as a string</param>
/// <param name="DataField">The column name which needs to spread out in Data Part of the Pivoted table</param>
/// <param name="Aggregate">Enumeration to determine which function to apply to aggregate the data</param>
/// <returns></returns>
private object GetAllocationData(string Filter, string DataField, AggregateFunction Aggregate)
{
try
{
DataRow[] FilteredRows = _SourceTable.Select(Filter);
object[] objList = FilteredRows.Select(x => x.Field<object>(DataField)).ToArray();
switch (Aggregate)
{
case AggregateFunction.Average:
return GetAverage(objList);
case AggregateFunction.Count:
return objList.Count();
case AggregateFunction.Exists:
return (objList.Count() == 0) ? "False" : "True";
case AggregateFunction.First:
return GetFirst(objList);
case AggregateFunction.Last:
return GetLast(objList);
case AggregateFunction.Max:
return GetMax(objList);
case AggregateFunction.Min:
return GetMin(objList);
case AggregateFunction.Sum:
return GetAllocationSum(objList);
default:
return null;
}
}
catch (Exception ex)
{
return "#Error";
}
}
private object GetAverage(object[] objList)
{
return objList.Count() == 0 ? null : (object)(Convert.ToDecimal(GetSum(objList))/objList.Count());
}
private object GetSum(object[] objList)
{
return objList.Count() == 0 ? null : (object)(objList.Aggregate(new decimal(), (x, y) => x += Convert.ToDecimal(y)) + "%");
}
private object GetAllocationSum(object[] objList)
{
return objList.Count() == 0 ? null : (object)(objList.Aggregate(new decimal(), (x, y) => x += Convert.ToDecimal(y)));
}
private object GetFirst(object[] objList)
{
return (objList.Count() == 0) ? null : objList.First();
}
private object GetLast(object[] objList)
{
return (objList.Count() == 0) ? null : objList.Last();
}
private object GetMax(object[] objList)
{
return (objList.Count() == 0) ? null : objList.Max();
}
private object GetMin(object[] objList)
{
return (objList.Count() == 0) ? null : objList.Min();
}
public enum AggregateFunction
{- : 점점 오류 아래
내가 선회를하고있는 중이 야 내 피벗 클래스의 코드 실제 데이터베이스 후로 카운트 = 1 합계 = 2 우선 = 3 마지막 = 4 는 보통 = 5 최대 = 6 분 = 7, 는 = 8 }
모든 시나리오에 기본 키가 있습니까? – Ratna
아니요, 실제로 "L & D 교육 %", "비 프로덕션 %", "프로세스 지원 %", "프로세스 교육 %", "프로덕션 %", "시스템 중단 시간 %", "총 합계 %"는 피벗 된 열입니다 어떤 나는 itemtemplate 바인딩입니다. 몇 가지 사용자에 대한 몇 가지 이러한 열의 존재하지 않는 및 표 배열 바인딩 오류가 발생합니다. \ –
데이터베이스 쿼리에서'ifnull (columnname, '') abc' 여기 열 이름을 시도하십시오. 당신의 테이블 컬럼 이름과 ''는 기본적으로이 컬럼을 디폴트 값으로 할당합니다. 널 값을 반환하면 abc는 column.hope의 별칭 이름입니다. 그냥 시도하십시오 ... – Rahul