Hi.
I have a RadGrid (see attachment - 01 - Grid.jpg).
The first few columns (bold headers) have their columns declared declaratively.
RadGrid ID is GV_DailySales
<telerik:GridTemplateColumn
HeaderText="Gross Sales<br/> "
SortExpression="GrossSales">
and their data comes from a generic List<DailySales>. The Sort Expression value of "GrossSales"
is a property of this generic list and I am able to sort this column using the basic column sorting procedure.
This is true for the first few columns (from column "DAY" to "Average Check" because they are properties
defined in the DailySales object so no problems there. File attachment 01 - Grid.jpg shows an example
of the sorting that works fine.
--------------------------------------------------------------------------------------
For the next few columns they are of type GridTemplateColumn but had been added dynamically.
Here's the code snippet on how I added them dynamically.
GV_DailySales.DataSource = ds.getDailySalesForTheMonth(DailySales_Date); //source data for the columns that sorts fine
#region dynamically add GridTemplateColumns for ATP
List<PL_StoreSupport.Products> P = ds.getAllProductsForDailySales(); //my source data for the dynamic columns
for (int i = 0; i < P.Count; i++)
{
TL.GridTemplateColumn gridTemplateColumn = new TL.GridTemplateColumn();
TL.RadButton b = new TL.RadButton() { Width = Unit.Pixel(25) };
b.Icon.PrimaryIconUrl = "~/ScriptsStylesItems/Images/icon_trends2.png";
Label l = new Label() { Text = "<br/>" + P[i].Product_Name };
gridTemplateColumn.HeaderTemplate = new CreateItemTemplate(b, l);
gridTemplateColumn.HeaderStyle.Width = Unit.Pixel(85);
gridTemplateColumn.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
gridTemplateColumn.UniqueName = Guid.NewGuid().ToString(); //or whatever name?
//gridTemplateColumn.SortExpression = "SortExpression";//to determine which --> does not work
GV_DailySales.MasterTableView.Columns.Add(gridTemplateColumn);
}
#endregion
----------------------- here's the code snippet how i defined my ITemplate----------------------------
//just a matter of adding a label and a RadButton on the header
#region CreateItemTemplate
public class CreateItemTemplate : ITemplate
{
private TL.RadButton RadButton_viewStat;
private Label label;
public CreateItemTemplate()
{
}
public CreateItemTemplate(TL.RadButton b, Label l)
{
this.RadButton_viewStat = b;
this.label = l;
}
public void InstantiateIn(Control container)
{
container.Controls.Add(RadButton_viewStat);
container.Controls.Add(label);
}
}
#endregion
----------------------- I created a Label for the dynamically created columns to hold the row data ---------------------------
protected void GV_DailySales_ItemCreated(object sender, TL.GridItemEventArgs e)
{
TL.GridDataItem ITEM = e.Item as TL.GridDataItem;
switch (e.Item.ItemType)
{
#region add numberic text box | asp labels
case TL.GridItemType.Item:
case TL.GridItemType.AlternatingItem:
for (int i = 9; i < ITEM.Cells.Count; i++)
{
#region RAD NUMERIC TEXT BOXES & LABELS
//..generic controls - dataLabel - asp labels
Label dataLabel = new Label();
dataLabel.ID = "dataLabel_" + i.ToString();
dataLabel.Text = "85";
ITEM.Cells[i].Controls.Add(dataLabel);
//..adjust alignment
ITEM.Cells[i].HorizontalAlign = HorizontalAlign.Center;
#endregion
}
break;
#endregion
}
--------------------------------- I populated the rows with random integers to simulate data ---------------------------
protected void GV_DailySales_ItemDataBound(object sender, TL.GridItemEventArgs e)
{
if ((e.Item.DataItem != null) && (e.Item is TL.GridDataItem))
{
#region DYNAMIC COLUMNS
for (int i = 9; i < ITEM.Cells.Count; i++)
{
Label dataLabel = ITEM.FindControl("dataLabel_" + i.ToString()) as Label;
dataLabel.Text = (i * i * ITEM.RowIndex).ToString();
}
#endregion
}
}
------
I did not use the the GV_DailySales.Databind() method anymore because I am already handling this via the
GV_DailySales_NeedDataSource event handler as I've read this to be "recommended practice."
I'd like to ask for some help how I can possible sort these dynamically added columns.
I have a RadGrid (see attachment - 01 - Grid.jpg).
The first few columns (bold headers) have their columns declared declaratively.
RadGrid ID is GV_DailySales
<telerik:GridTemplateColumn
HeaderText="Gross Sales<br/> "
SortExpression="GrossSales">
and their data comes from a generic List<DailySales>. The Sort Expression value of "GrossSales"
is a property of this generic list and I am able to sort this column using the basic column sorting procedure.
This is true for the first few columns (from column "DAY" to "Average Check" because they are properties
defined in the DailySales object so no problems there. File attachment 01 - Grid.jpg shows an example
of the sorting that works fine.
--------------------------------------------------------------------------------------
For the next few columns they are of type GridTemplateColumn but had been added dynamically.
Here's the code snippet on how I added them dynamically.
GV_DailySales.DataSource = ds.getDailySalesForTheMonth(DailySales_Date); //source data for the columns that sorts fine
#region dynamically add GridTemplateColumns for ATP
List<PL_StoreSupport.Products> P = ds.getAllProductsForDailySales(); //my source data for the dynamic columns
for (int i = 0; i < P.Count; i++)
{
TL.GridTemplateColumn gridTemplateColumn = new TL.GridTemplateColumn();
TL.RadButton b = new TL.RadButton() { Width = Unit.Pixel(25) };
b.Icon.PrimaryIconUrl = "~/ScriptsStylesItems/Images/icon_trends2.png";
Label l = new Label() { Text = "<br/>" + P[i].Product_Name };
gridTemplateColumn.HeaderTemplate = new CreateItemTemplate(b, l);
gridTemplateColumn.HeaderStyle.Width = Unit.Pixel(85);
gridTemplateColumn.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
gridTemplateColumn.UniqueName = Guid.NewGuid().ToString(); //or whatever name?
//gridTemplateColumn.SortExpression = "SortExpression";//to determine which --> does not work
GV_DailySales.MasterTableView.Columns.Add(gridTemplateColumn);
}
#endregion
----------------------- here's the code snippet how i defined my ITemplate----------------------------
//just a matter of adding a label and a RadButton on the header
#region CreateItemTemplate
public class CreateItemTemplate : ITemplate
{
private TL.RadButton RadButton_viewStat;
private Label label;
public CreateItemTemplate()
{
}
public CreateItemTemplate(TL.RadButton b, Label l)
{
this.RadButton_viewStat = b;
this.label = l;
}
public void InstantiateIn(Control container)
{
container.Controls.Add(RadButton_viewStat);
container.Controls.Add(label);
}
}
#endregion
----------------------- I created a Label for the dynamically created columns to hold the row data ---------------------------
protected void GV_DailySales_ItemCreated(object sender, TL.GridItemEventArgs e)
{
TL.GridDataItem ITEM = e.Item as TL.GridDataItem;
switch (e.Item.ItemType)
{
#region add numberic text box | asp labels
case TL.GridItemType.Item:
case TL.GridItemType.AlternatingItem:
for (int i = 9; i < ITEM.Cells.Count; i++)
{
#region RAD NUMERIC TEXT BOXES & LABELS
//..generic controls - dataLabel - asp labels
Label dataLabel = new Label();
dataLabel.ID = "dataLabel_" + i.ToString();
dataLabel.Text = "85";
ITEM.Cells[i].Controls.Add(dataLabel);
//..adjust alignment
ITEM.Cells[i].HorizontalAlign = HorizontalAlign.Center;
#endregion
}
break;
#endregion
}
--------------------------------- I populated the rows with random integers to simulate data ---------------------------
protected void GV_DailySales_ItemDataBound(object sender, TL.GridItemEventArgs e)
{
if ((e.Item.DataItem != null) && (e.Item is TL.GridDataItem))
{
#region DYNAMIC COLUMNS
for (int i = 9; i < ITEM.Cells.Count; i++)
{
Label dataLabel = ITEM.FindControl("dataLabel_" + i.ToString()) as Label;
dataLabel.Text = (i * i * ITEM.RowIndex).ToString();
}
#endregion
}
}
------
I did not use the the GV_DailySales.Databind() method anymore because I am already handling this via the
GV_DailySales_NeedDataSource event handler as I've read this to be "recommended practice."
I'd like to ask for some help how I can possible sort these dynamically added columns.