So i have the need to create programmatically a grid so I began to dig around to see how this could be done, and found i needed to implement the ITemplate interface. So the following classes is those implementations. One for the header template, one for the itemtemplate and one for the footer template.
My problem is, for the first column, all works out great, with the numeric RadNumericTextBox validating good, the footer sums the values i put on the items RadNumericTextBox and it's perfect. But for the other columns, the it seems like it's a normal textbox. No validation is made, the the footer doesnt sum up the values. In attachment is the imagem of the final grid.
And sorry for the long post :)
The is the Header template. I have the need to divide the header into 2 columns, One will have the values from the datasource and the other will contain RadNumericTextBox with the type of currency. (see attachment for a visualization of the final result)
This is the Item Template, where i fill the values. One of the cells will have the values from the datasource, the other will contain a RadNumericTextBox (see attachment for a visualization of the final and for an example of the datasource)
Footer template for my column. On the footer, I want to have the sum of all values of the textboxes. For that I use javascript, indicated on the line
This final code, is where i do the assembling of the grid
My problem is, for the first column, all works out great, with the numeric RadNumericTextBox validating good, the footer sums the values i put on the items RadNumericTextBox and it's perfect. But for the other columns, the it seems like it's a normal textbox. No validation is made, the the footer doesnt sum up the values. In attachment is the imagem of the final grid.
And sorry for the long post :)
The is the Header template. I have the need to divide the header into 2 columns, One will have the values from the datasource and the other will contain RadNumericTextBox with the type of currency. (see attachment for a visualization of the final result)
public
class
RadGridHeaderTemplate : ITemplate
{
private
string
_columName;
public
string
ColumName
{
get
{
return
_columName; }
set
{ _columName = value; }
}
public
RadGridHeaderTemplate(
string
cName)
{
_columName = cName;
}
public
RadGridHeaderTemplate()
{
}
public
void
InstantiateIn(Control container)
{
Table table =
new
Table();
TableRow row1 =
new
TableRow();
TableRow row2 =
new
TableRow();
TableCell cell11 =
new
TableCell();
TableCell cell21 =
new
TableCell();
TableCell cell22 =
new
TableCell();
cell11.ColumnSpan = 2;
cell11.BackColor = System.Drawing.Color.Aqua;
cell11.Text = ColumName;
row1.Cells.Add(cell11);
cell21.Text =
"Valores Actuais"
;
cell21.BackColor = System.Drawing.Color.Aqua;
row2.Cells.Add(cell21);
cell22.Text =
"Valores Novos"
;
cell22.BackColor = System.Drawing.Color.Aqua;
row2.Cells.Add(cell22);
table.Rows.Add(row1);
table.Rows.Add(row2);
container.Controls.Add(table);
}
}
This is the Item Template, where i fill the values. One of the cells will have the values from the datasource, the other will contain a RadNumericTextBox (see attachment for a visualization of the final and for an example of the datasource)
public
class
RadGridItemTemplate: ITemplate
{
protected
LiteralControl lControl;
protected
RangeValidator validatorTextBox;
protected
RadNumericTextBox textBox;
private
string
_columnName;
private
string
_headerName;
private
bool
_isOrigin;
public
string
ColumnName
{
get
{
return
_columnName; }
set
{ _columnName = value; }
}
public
string
HeaderName
{
get
{
return
_headerName; }
set
{ _headerName = value; }
}
public
bool
IsOrigin
{
get
{
return
_isOrigin; }
set
{ _isOrigin = value; }
}
public
RadGridItemTemplate(
string
cName,
string
hName,
bool
isOrigin)
{
_columnName = cName;
_headerName = hName;
_isOrigin = isOrigin;
}
public
RadGridItemTemplate()
{
}
public
void
InstantiateIn(Control container)
{
Table table =
new
Table();
table.BorderColor = System.Drawing.Color.Black;
table.Style.Add(HtmlTextWriterStyle.BorderWidth,
"1px"
);
TableRow row1 =
new
TableRow();
textBox =
new
RadNumericTextBox();
textBox.ID =
"templateColumnTextBox"
;
textBox.Type = NumericType.Currency;
textBox.ClientEvents.OnBlur =
"Blur"
;
textBox.ClientEvents.OnFocus =
"Focus"
;
textBox.Width = Unit.Pixel(60);
if
(IsOrigin)
{
lControl =
new
LiteralControl();
lControl.ID =
"lControl"
;
lControl.DataBinding += lControl_DataBinding;
TableCell cell11 =
new
TableCell();
TableCell cell12 =
new
TableCell();
cell11.Width = Unit.Pixel(50);
cell11.Style.Add(HtmlTextWriterStyle.TextAlign,
"center"
);
cell11.Controls.Add(lControl);
cell12.Controls.Add(textBox);
row1.Cells.Add(cell11);
row1.Cells.Add(cell12);
}
else
{
TableCell cell11 =
new
TableCell();
cell11.Controls.Add(textBox);
row1.Cells.Add(cell11);
}
table.Rows.Add(row1);
container.Controls.Add(table);
}
public
void
lControl_DataBinding(
object
sender, EventArgs e)
{
LiteralControl l = (LiteralControl)sender;
GridDataItem container = (GridDataItem)l.NamingContainer;
l.Text = String.Format(
"{0:C}"
, Convert.ToDouble(((DataRowView) container.DataItem)[ColumnName]));
}
Footer template for my column. On the footer, I want to have the sum of all values of the textboxes. For that I use javascript, indicated on the line
textBox.ClientEvents.OnLoad =
"Load"
;
public
class
RadGridFooterTemplate : ITemplate
{
protected
RadNumericTextBox textBox;
private
bool
_isOrigin;
public
bool
IsOrigin
{
get
{
return
_isOrigin; }
set
{ _isOrigin = value; }
}
public
RadGridFooterTemplate(
bool
isOrigin)
{
_isOrigin = isOrigin;
}
public
RadGridFooterTemplate()
{
}
public
void
InstantiateIn(Control container)
{
Table table =
new
Table();
TableRow row1 =
new
TableRow();
textBox =
new
RadNumericTextBox();
textBox.ID =
"footerColumnTextBox"
;
textBox.Type = NumericType.Currency;
textBox.ClientEvents.OnLoad =
"Load"
;
textBox.Enabled =
false
;
if
(IsOrigin)
{
TableCell cell11 =
new
TableCell();
TableCell cell12 =
new
TableCell();
cell11.Text =
"Total"
;
cell12.Controls.Add(textBox);
row1.Cells.Add(cell11);
row1.Cells.Add(cell12);
}
else
{
TableCell cell11 =
new
TableCell();
cell11.Controls.Add(textBox);
row1.Cells.Add(cell11);
}
table.Rows.Add(row1);
container.Controls.Add(table);
}
}
This final code, is where i do the assembling of the grid
RadGrid radGrid =
new
RadGrid();
radGrid.ID =
"gvSetValues"
;
radGrid.DataSource = buildDataTable;
radGrid.AllowPaging =
false
;
radGrid.AllowSorting =
false
;
radGrid.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
radGrid.AutoGenerateColumns =
false
;
radGrid.ShowStatusBar =
true
;
radGrid.ShowFooter =
true
;
radGrid.MasterTableView.ShowFooter =
true
;
GridBoundColumn boundColumn =
new
GridBoundColumn();
boundColumn.DataField =
"Meses"
;
boundColumn.UniqueName =
"Meses"
;
boundColumn.HeaderText =
"Meses/PPI"
;
radGrid.MasterTableView.Columns.Add(boundColumn);
foreach
(DataColumn column
in
buildDataTable.Columns)
{
//Add origins Columns
if
(column.ColumnName.StartsWith(
"O_"
))
{
GridTemplateColumn templateColumn =
new
GridTemplateColumn();
templateColumn.HeaderTemplate =
new
RadGridHeaderTemplate(column.ColumnName.Split(
'_'
)[1]);
templateColumn.ItemTemplate =
new
RadGridItemTemplate(column.ColumnName, column.ColumnName.Split(
'_'
)[1],
true
);
templateColumn.FooterTemplate =
new
RadGridFooterTemplate(
true
);
radGrid.MasterTableView.Columns.Add(templateColumn);
templateColumn.UniqueName =
"OldValues"
;
}
//Add Destinations Columns
if
(column.ColumnName.StartsWith(
"D_"
))
{
GridTemplateColumn templateColumn =
new
GridTemplateColumn();
templateColumn.HeaderText = column.ColumnName.Split(
'_'
)[1];
templateColumn.ItemTemplate =
new
RadGridItemTemplate(column.ColumnName, column.ColumnName.Split(
'_'
)[1],
false
);
templateColumn.FooterTemplate =
new
RadGridFooterTemplate(
false
);
radGrid.MasterTableView.Columns.Add(templateColumn);
}
}
radGrid.DataBind();
phGvMoveValues.Controls.Add(radGrid);