Hi,
I'm creating a grid programmatically based on an unknown number of columns of unknown type and wish to generate multi-line footers with aggregated values. I have looked at the help page, but still not sure how to proceed.
I'm currently working in the RadGrid1_ItemDataBound method where I'm stuck at "if (e.Item is GridFooterItem)". I still need to figure out how to create the footers, populate them with text boxes, and refer to the mysterious clientID mentioned in the help article.
Thanks,
David
I'm creating a grid programmatically based on an unknown number of columns of unknown type and wish to generate multi-line footers with aggregated values. I have looked at the help page, but still not sure how to proceed.
I'm currently working in the RadGrid1_ItemDataBound method where I'm stuck at "if (e.Item is GridFooterItem)". I still need to figure out how to create the footers, populate them with text boxes, and refer to the mysterious clientID mentioned in the help article.
using
System;
using
System.Data;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
Telerik.Web.UI;
using
System.Text.RegularExpressions;
public
partial
class
Spreadsheet : System.Web.UI.Page
{
protected
global::System.Web.UI.WebControls.Literal dataRow;
protected
void
Page_Init(
object
sender, System.EventArgs e)
{
DefineGridStructure();
}
protected
void
Page_Load(
object
sender, EventArgs e)
{
RadGrid grid = (RadGrid)PlaceHolder1.FindControl(
"RadGrid1"
);
RadAjaxManager1.AjaxSettings.AddAjaxSetting(grid, grid);
if
(Page.IsPostBack)
{
Label1.Text +=
"Post back!! <br>"
;
}
if
(!Page.IsPostBack)
{
Label1.Text +=
"Page_Load; "
+ DateTime.Now.ToString() +
"<br>"
;
}
}
private
void
DefineGridStructure()
{
RadGrid RadGrid1 =
new
RadGrid();
RadGrid1.ID =
"RadGrid1"
;
System.Drawing.Color disabledBackColor = System.Drawing.Color.AliceBlue;
System.Drawing.Color defaultBorderColor = System.Drawing.Color.Black;
RadGrid1.Width = Unit.Percentage(100);
RadGrid1.PageSize = 7;
RadGrid1.GridLines = GridLines.Both;
RadGrid1.AllowPaging =
true
;
RadGrid1.AllowSorting =
true
;
RadGrid1.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
RadGrid1.ShowFooter =
true
;
RadGrid1.AutoGenerateColumns =
false
;
RadGrid1.ShowStatusBar =
true
;
RadGrid1.DataSourceID =
"ObjectDataSource1"
;
RadGrid1.ItemDataBound += RadGrid1_ItemDataBound;
RadGrid1.UpdateCommand += RadGrid1_UpdateCommand;
RadGrid1.MasterTableView.PageSize = 7;
RadGrid1.MasterTableView.Height = Unit.Percentage(100);
RadGrid1.MasterTableView.DataKeyNames =
new
string
[] {
"report_id"
};
RadGrid1.MasterTableView.EditMode = GridEditMode.InPlace;
GridEditCommandColumn editColumn;
editColumn =
new
GridEditCommandColumn();
editColumn.UniqueName =
"EditCommandColumn"
;
editColumn.ItemStyle.BackColor = disabledBackColor;
editColumn.ItemStyle.BorderColor = defaultBorderColor;
editColumn.ItemStyle.BorderWidth = Unit.Pixel(1);
RadGrid1.MasterTableView.Columns.Add(editColumn);
GridColumnGroup headerGroupColumn;
GridBoundColumn boundColumn =
new
GridBoundColumn();
boundColumn.DataField =
"report_id"
;
boundColumn.HeaderText =
"report_id"
;
boundColumn.ItemStyle.BackColor = disabledBackColor;
boundColumn.ItemStyle.BorderColor = defaultBorderColor;
boundColumn.ItemStyle.BorderWidth = Unit.Pixel(1);
boundColumn.ReadOnly =
true
;
RadGrid1.MasterTableView.Columns.Add(boundColumn);
RadGrid1.MasterTableView.AllowMultiColumnSorting =
true
;
RadGrid1.MasterTableView.SortExpressions.Clear();
GridSortExpression expression =
new
GridSortExpression();
expression.FieldName =
"report_id"
;
expression.SortOrder = GridSortOrder.Ascending;
RadGrid1.MasterTableView.SortExpressions.AddSortExpression(expression);
DataTable GroupHeaders = WastewaterGrid.GetGroupHeaders();
DataTable Headers;
foreach
(DataRow groupRow
in
GroupHeaders.Rows)
{
String masterGroupName = groupRow[
"group_name"
].ToString();
String groupColumnHeader = groupRow[
"group_column_header"
].ToString();
Headers = WastewaterGrid.GetHeaders(masterGroupName);
headerGroupColumn =
new
GridColumnGroup();
headerGroupColumn.Name = masterGroupName;
headerGroupColumn.HeaderText = groupColumnHeader;
RadGrid1.MasterTableView.ColumnGroups.Add(headerGroupColumn);
foreach
(DataRow headerRow
in
Headers.Rows)
{
String groupName = headerRow[
"group_name"
].ToString();
String columnName = headerRow[
"column_name"
].ToString();
String columnHeader = headerRow[
"column_header"
].ToString();
String attributeType = headerRow[
"attribute_type"
].ToString();
String dataType = headerRow[
"data_type"
].ToString();
String targetValue = headerRow[
"target_value"
].ToString();
if
(!groupName.Equals(masterGroupName))
{
continue
;
}
boundColumn =
new
GridBoundColumn();
boundColumn.DataField = columnName;
boundColumn.HeaderText = columnHeader;
// where is the uniquename?
boundColumn.ColumnGroupName = masterGroupName;
if
(columnName.Equals(
"MISDnotes"
))
{
boundColumn.ItemStyle.Width = Unit.Percentage(100);
}
if
(!attributeType.Equals(
"ManualEntry"
))
{
boundColumn.ItemStyle.BackColor = disabledBackColor;
boundColumn.ReadOnly =
true
;
}
RadGrid1.MasterTableView.Columns.Add(boundColumn);
}
}
this
.PlaceHolder1.Controls.Add(RadGrid1);
}
public
class
FooterItem
{
private
String _columnName;
private
int
_count;
private
dynamic _min;
private
dynamic _max;
private
dynamic _sum;
public
FooterItem(String str)
{
_columnName = str;
_count = 0;
}
public
String columnName
{
get
{
return
_columnName;
}
set
{
_columnName = value;
}
}
public
int
count
{
get
{
return
_count;
}
set
{
_count = value;
}
}
public
dynamic min
{
get
{
return
_min;
}
set
{
_min = value;
}
}
public
dynamic max
{
get
{
return
_max;
}
set
{
_max = value;
}
}
public
dynamic sum
{
get
{
return
_sum;
}
set
{
_sum = value;
}
}
}
protected
void
RadGrid1_ItemDataBound(
object
sender, GridItemEventArgs e)
{
Int32 userRole = 2;
Int32 today =
int
.Parse(
"20110907"
);
//String today = DateTime.Today.ToString("yyyyMMdd");
String columnName;
String attributeType;
GridTableCell gridCell;
dynamic cellText;
Boolean cellPopped =
false
;
TextBox cellTextBox;
Dictionary<String, FooterItem> FooterAggregates =
new
Dictionary<String, FooterItem>();
FooterItem footerItem;
DataTable Headers = WastewaterGrid.GetHeaders();
foreach
(DataRow row
in
Headers.Rows)
{
columnName = row[
"column_name"
].ToString();
attributeType = row[
"attribute_type"
].ToString();
if
(!attributeType.Contains(
"varchar"
))
{
FooterAggregates.Add(columnName,
new
FooterItem(columnName));
}
}
if
(e.Item
is
GridDataItem)
{
GridDataItem gridRow = (GridDataItem)e.Item;
Int32 ReportID =
int
.Parse(gridRow.GetDataKeyValue(
"report_id"
).ToString());
System.Drawing.Color defaultBorderColor = System.Drawing.Color.Black;
System.Drawing.Color ableBackColor = System.Drawing.Color.White;
System.Drawing.Color disabledBackColor = System.Drawing.Color.AliceBlue;
System.Drawing.Color ableBorderColor = System.Drawing.Color.Lime;
foreach
(DataRow row
in
Headers.Rows)
{
columnName = row[
"column_name"
].ToString();
attributeType = row[
"attribute_type"
].ToString();
gridCell = (GridTableCell)gridRow[columnName];
//cellText = gridCell.Text;
cellText = ((DataRowView)gridRow.DataItem)[columnName];
cellPopped = !String.IsNullOrEmpty((String)cellText);
if
(cellPopped)
{
footerItem = FooterAggregates[columnName];
footerItem.count += 1;
if
(footerItem.min ==
null
)
{
footerItem.min = cellText;
}
else
{
if
(footerItem.min > cellText)
{
footerItem.min = cellText;
}
}
if
(footerItem.max ==
null
)
{
footerItem.max = cellText;
}
else
{
if
(footerItem.max < cellText)
{
footerItem.max = cellText;
}
}
if
(footerItem.sum ==
null
)
{
footerItem.sum = cellText;
}
else
{
footerItem.sum += cellText;
}
}
gridCell.BorderColor = defaultBorderColor;
gridCell.BorderWidth = Unit.Pixel(1);
if
(userRole == 1)
{
if
(attributeType.Equals(
"ManualEntry"
))
{
gridCell.BackColor = ableBackColor;
gridCell.BorderColor = ableBorderColor;
}
}
else
{
if
(attributeType.Equals(
"ManualEntry"
))
{
if
(!ReportID.Equals(today))
{
if
(String.IsNullOrEmpty(cellText))
{
gridCell.BackColor = ableBackColor;
gridCell.BorderColor = ableBorderColor;
}
else
{
gridCell.BackColor = disabledBackColor;
}
}
else
{
gridCell.BackColor = ableBackColor;
gridCell.BorderColor = ableBorderColor;
}
}
if
(e.Item.IsInEditMode && !ReportID.Equals(today) && cellPopped)
{
cellTextBox = (TextBox)gridCell.Controls[0];
cellTextBox.BorderStyle = BorderStyle.None;
cellTextBox.BackColor = disabledBackColor;
cellTextBox.ReadOnly =
true
;
//Label1.Text += "EditMode::ReportID=" + ReportID + "columnName=" + columnName + "::cellText=" + cellText + " <br>";
}
}
}
}
if
(e.Item
is
GridFooterItem)
{
GridFooterItem gridRow = (GridFooterItem)e.Item;
int
count = 0;
dynamic min;
dynamic max;
dynamic sum;
foreach
(DataRow row
in
Headers.Rows)
{
columnName = row[
"column_name"
].ToString();
attributeType = row[
"attribute_type"
].ToString();
gridCell = (GridTableCell)gridRow[columnName];
//cellText = gridCell.Text;
cellText = ((DataRowView)gridRow.DataItem)[columnName];
footerItem = FooterAggregates[columnName];
count += footerItem.count;
}
}
}
private
void
RadGrid1_UpdateCommand(
object
source, GridCommandEventArgs e)
{
Label1.Text +=
" Table to be updated: "
+ e.Item.OwnerTableView.DataMember +
"<br>"
;
GridEditableItem editedItem = e.Item
as
GridEditableItem;
GridEditManager editMan = editedItem.EditManager;
Int32 ReportID =
int
.Parse(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex][
"report_id"
].ToString());
String Login =
"David-T420s\\davidk"
;
foreach
(GridColumn column
in
e.Item.OwnerTableView.RenderColumns)
{
if
(column.UniqueName.Equals(
"report_id"
))
{
continue
;
}
if
(column
is
IGridEditableColumn)
{
Boolean isEditable = column.IsEditable;
String editColumnName = column.UniqueName.ToString();
String ResultString;
IGridEditableColumn editableCol = (column
as
IGridEditableColumn);
if
(editableCol.IsEditable)
{
IGridColumnEditor editor = editMan.GetColumnEditor(editableCol);
if
(editor
is
GridTextColumnEditor)
{
String Datum = (editor
as
GridTextColumnEditor).Text.Trim();
DataTable Headers = WastewaterGrid.GetHeaders();
foreach
(DataRow row
in
Headers.Rows)
{
String columnName = row[
"column_name"
].ToString();
String dataType = row[
"data_type"
].ToString();
if
(columnName.Equals(editColumnName))
{
ResultString = ValidateUserInput(dataType, Datum);
if
(!ResultString.Equals(
"valid"
))
{
RadAjaxManager1.Alert(ResultString);
e.Canceled =
true
;
break
;
}
else
{
try
{
WastewaterGrid.UpsertDatum(ReportID, editColumnName, Datum, Login);
}
catch
(Exception ex)
{
Label1.Text +=
"<strong>Unable to set value of column '"
+ column.UniqueName +
"'</strong> <br>"
+ ex.Message;
RadAjaxManager1.Alert(
"Unable to set value of column: "
+ column.UniqueName +
"; "
+ ex.Message);
e.Canceled =
true
;
break
;
}
}
}
}
}
}
}
}
try
{
WastewaterGrid.ReCalculate(ReportID);
}
catch
(Exception ex)
{
RadAjaxManager1.Alert(
"Unable to run ReCalculate: "
+ ex.Message);
e.Canceled =
true
;
}
}
private
String ValidateUserInput(String dataType, String Datum)
{
if
(String.IsNullOrEmpty(Datum))
{
return
"valid"
;
}
Match match;
String ResultString;
String TestString;
Int32 stringLength = Datum.Length;
String IntPattern = @
"^\d+$"
;
RadAjaxManager1.Alert(
"VALIDATEUSERINPUT: dataType=\""
+ dataType +
"\"; Datum=\""
+ Datum +
"\""
);
switch
(dataType)
{
case
"bigint"
:
match = Regex.Match(Datum, IntPattern);
if
(match.Success)
{
try
{
TestString = Int64.Parse(Datum).ToString();
}
catch
(OverflowException)
{
return
"Overflow Exception: improper "
+ dataType +
"format in \""
+ Datum +
"\""
;
}
}
else
{
return
"Improper format: Int64"
;
}
break
;
case
"decimal(10,2)"
:
ResultString = ValidateDecimalFormat(dataType, Datum, stringLength, 10, 2);
if
(!ResultString.Equals(
"valid"
))
{
return
ResultString;
}
else
{
try
{
TestString = Decimal.Parse(Datum).ToString();
}
catch
(OverflowException)
{
return
"Overflow Exception: improper "
+ dataType +
"format in \""
+ Datum +
"\""
;
}
}
break
;
case
"decimal(10,3)"
:
ResultString = ValidateDecimalFormat(dataType, Datum, stringLength, 10, 3);
if
(!ResultString.Equals(
"valid"
))
{
return
ResultString;
}
else
{
try
{
TestString = Decimal.Parse(Datum).ToString();
}
catch
(OverflowException)
{
return
"Overflow Exception: improper "
+ dataType +
"format in \""
+ Datum +
"\""
;
}
}
break
;
case
"decimal(4,1)"
:
ResultString = ValidateDecimalFormat(dataType, Datum, stringLength, 4, 1);
if
(!ResultString.Equals(
"valid"
))
{
return
ResultString;
}
else
{
try
{
TestString = Decimal.Parse(Datum).ToString();
}
catch
(OverflowException)
{
return
"Overflow Exception: improper "
+ dataType +
"format in \""
+ Datum +
"\""
;
}
}
break
;
case
"decimal(5,1)"
:
ResultString = ValidateDecimalFormat(dataType, Datum, stringLength, 5, 1);
if
(!ResultString.Equals(
"valid"
))
{
return
ResultString;
}
else
{
try
{
TestString = Decimal.Parse(Datum).ToString();
}
catch
(OverflowException)
{
return
"Overflow Exception: improper "
+ dataType +
"format in \""
+ Datum +
"\""
;
}
}
break
;
case
"decimal(6,1)"
:
ResultString = ValidateDecimalFormat(dataType, Datum, stringLength, 6, 1);
if
(!ResultString.Equals(
"valid"
))
{
return
ResultString;
}
else
{
try
{
TestString = Decimal.Parse(Datum).ToString();
}
catch
(OverflowException)
{
return
"Overflow Exception: improper "
+ dataType +
"format in \""
+ Datum +
"\""
;
}
}
break
;
case
"int"
:
match = Regex.Match(Datum, IntPattern);
if
(match.Success)
{
try
{
TestString = Int32.Parse(Datum).ToString();
//throw new CustomExceptions.
}
catch
(OverflowException)
{
return
"Overflow Exception: improper "
+ dataType +
"format in \""
+ Datum +
"\""
;
}
}
else
{
return
"Improper format: Int32"
;
}
break
;
case
"varchar(25)"
:
if
(stringLength > 25)
{
return
"Improper "
+ dataType +
"format in \""
+ Datum +
"\""
;
}
break
;
case
"varchar(255)"
:
if
(stringLength > 255)
{
return
"Improper "
+ dataType +
"format in \""
+ Datum +
"\""
;
}
break
;
default
:
RadAjaxManager1.Alert(
"Unhandled data type"
);
return
"Unhandled data type: \""
+ dataType +
"\""
;
}
return
"valid"
;
}
private
String ValidateDecimalFormat(String dataType, String Datum,
int
stringLength,
int
precision,
int
scale)
{
Match match;
String IntPattern = @
"^\d+$"
;
String ResultString;
Int32 decimalIndex = Datum.IndexOf(
"."
);
if
(decimalIndex > 0)
{
Int32 fractionalLength = stringLength - decimalIndex - 1;
if
(fractionalLength > scale)
{
return
"Improper "
+ dataType +
"format in \""
+ Datum +
"\""
;
}
Int32 integralLength = stringLength - fractionalLength - 1;
if
(integralLength > (precision - scale))
{
return
"Improper "
+ dataType +
"format in \""
+ Datum +
"\""
;
}
String integralString = Datum.Substring(0, decimalIndex);
String fractionalString = Datum.Substring(decimalIndex + 1, fractionalLength);
match = Regex.Match(integralString, IntPattern);
if
(match.Success)
{
match = Regex.Match(fractionalString, IntPattern);
if
(match.Success)
{
ResultString =
"valid"
;
}
else
{
ResultString =
"Improper "
+ dataType +
"format in \""
+ Datum +
"\""
;
}
}
else
{
ResultString =
"Improper "
+ dataType +
"format in \""
+ Datum +
"\""
;
}
}
else
if
(stringLength > (precision - scale))
{
ResultString =
"Improper "
+ dataType +
"format in \""
+ Datum +
"\""
;
}
else
{
match = Regex.Match(Datum, IntPattern);
if
(match.Success)
{
ResultString =
"valid"
;
}
else
{
ResultString =
"Improper "
+ dataType +
"format in \""
+ Datum +
"\""
;
}
}
return
ResultString;
}
}
Thanks,
David