Rod Barrand
Top achievements
Rank 1
Rod Barrand
asked on 28 Jul 2010, 03:31 PM
Hi, i'm building a grid from inverted database table. I had success with your help on GridDateTimeColumn but cant get a numeric textbox in autogenerate insert/edit mode
I get the datatype from a field in the table like
Thanks for any help
I get the datatype from a field in the table like
case
"datetime":
case "date":
case "time":
oType =
typeof(DateTime);
break;
case "int":
case "numeric":
oType =
typeof(Int32);
break;
and in ColumnCreated
if
(e.Column is GridDateTimeColumn && e.Column.UniqueName.Contains("Time"))
{
(e.Column
as GridDateTimeColumn).PickerType = GridDateTimeColumnPickerType.TimePicker;
thats all good, but ..
if (e.Column is GridNumericColumn
e.column is never a GridNumericColumn, even though the column is built as a Int32(stored as int in the table "Datatype" fieldThanks for any help
7 Answers, 1 is accepted
0
Cori
Top achievements
Rank 2
answered on 28 Jul 2010, 07:39 PM
Hello Rod,
Did you set that column to a GridNumericColumn in your RadGrid definition? I would assume that it the reason it never goes into your if statement.
I hope that helps.
Did you set that column to a GridNumericColumn in your RadGrid definition? I would assume that it the reason it never goes into your if statement.
I hope that helps.
0
Rod Barrand
Top achievements
Rank 1
answered on 29 Jul 2010, 12:02 PM
Hi, no i don't set any of the columns. The grid is generic and i don't know what fields will be in the datatable. The data in the db is vertical. A field in the table tells me what data type should be applied to that field. So the datatable contains the column name ("StartTime") the datatype ("time"), or if i want a calendar datatype would be ("date"), RouteNumber("int")
The grid is autogenerated
Thanks
Rod
The grid is autogenerated
namespace FMCSA.Web.UserControls
{
public partial class ucRadGrid : System.Web.UI.UserControl
{
workflow wf = new workflow();
DataTable oNormalDT = new DataTable();
DataSet oDS;
Guid formDataId = Guid.Empty;
Guid formId = Guid.Empty;
Guid rgId = Guid.Empty;
public bool Enabled
{
get
{
object o = ViewState["Enabled"].ToString();
if (o.ToString() == "False")
{
return false;
}
else
{
return true;
}
}
set { ViewState["Enabled"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Params["formDataId"] == null)
{
formDataId = Guid.Empty;
}
else
{
formDataId = new Guid(Request.QueryString["formDataId"].ToString());
}
if (Request.Params["formnum"] == null)
{
formId = Guid.Empty;
}
else
{
formId = new Guid(Request.QueryString["formnum"].ToString());
}
if (Enabled)
{
RadGrid1.Enabled = true;
}
else
{
RadGrid1.Enabled = false;
RadGrid1.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.None;
}
}
private DataSet GetInvertedData()
{
SqlDatabase oDatabase;
DbCommand oDbCommand;
if (rgId == Guid.Empty)
{
rgId = wf.GetGridId(formId);
}
oDatabase = new SqlDatabase(ConfigurationManager.ConnectionStrings["WorkflowDBConnectionString"].ConnectionString);
object[] parameters = new object[] { formId, rgId, formDataId };
using (oDbCommand = oDatabase.GetStoredProcCommand("spc_GetInvertedData", parameters))
{
oDS = oDatabase.ExecuteDataSet(oDbCommand);
}
return oDS;
}
private Type ConvertType(string szSQLServerType)
{
Type oType = null;
string szDotNetType = string.Empty;
switch (szSQLServerType)
{
case "nvarchar":
case "nchar":
case "char":
case "varchar":
oType = typeof(string);
break;
case "datetime":
case "date":
case "time":
oType = typeof(DateTime);
break;
case "int":
case "numeric":
oType = typeof(Int32);
break;
case "money":
oType = typeof(decimal);
break;
}
return oType;
}
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
{
if (e.CommandName == RadGrid.EditCommandName)
{
e.Item.OwnerTableView.EditMode = GridEditMode.EditForms;
}
if (e.CommandName == RadGrid.InitInsertCommandName)
{
e.Item.OwnerTableView.EditMode = GridEditMode.EditForms;
}
}
protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
{
string s = string.Empty;
int i = wf.GetNextGridElement(formDataId);
if (rgId == Guid.Empty)
{
rgId = wf.GetGridId(formId);
}
GridEditableItem editedItem = e.Item as GridEditableItem;
Hashtable newValues = new Hashtable();
editedItem.ExtractValues(newValues);
wf.SaveRadGrid(i, rgId, formDataId, newValues);
}
protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
{
int i = wf.GetGridElement(formDataId);
if (rgId == Guid.Empty)
{
rgId = wf.GetGridId(formId);
}
GridEditableItem editedItem = e.Item as GridEditableItem;
Hashtable newValues = new Hashtable();
e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
wf.UpdateRadGrid(i, rgId, formDataId, newValues);
}
protected void RadGrid1_DeleteCommand(object source, GridCommandEventArgs e)
{
}
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
}
protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
oDS = GetInvertedData();
DataTable oStructureDT = oDS.Tables[0];
DataTable oInvertedDT = oDS.Tables[1];
DataRow oNewDR = null;
int iPrimaryKeyID;
//Create the normalized data structure
foreach (DataRow oDR in oStructureDT.Rows)
{
// get the column name and type of data to apply
oNormalDT.Columns.Add(new DataColumn(oDR["ColumnName"].ToString(),
ConvertType(oDR["DataType"].ToString())));
if (oInvertedDT.Rows.Count != 0)
{
Guid rgId = new Guid(oDS.Tables[1].Rows[0]["GridId"].ToString());
Session["rgId"] = rgId.ToString();
iPrimaryKeyID = 0;
//Iterate the inverted data set
foreach (DataRow oDR in oInvertedDT.Rows)
{
//When the primary key changes, we can add the new row...
if (iPrimaryKeyID != int.Parse(oDR["elementnumber"].ToString()))
{
//...except the first time through
if (iPrimaryKeyID != 0)
oNormalDT.Rows.Add(oNewDR);
//Create a new row object and set the primary key value
oNewDR = oNormalDT.NewRow();
iPrimaryKeyID = int.Parse(oDR["elementnumber"].ToString());
}
//Add the data to the named column
if (oDR["fieldvalue"].ToString() != string.Empty)
{
oNewDR[oDR["ColumnName"].ToString()] = oDR["fieldvalue"].ToString();
}
}
oNormalDT.Rows.Add(oNewDR);
}
this.RadGrid1.DataSource = oNormalDT;
this.RadGrid1.MasterTableView.DataSource = oNormalDT;
}
protected void RadGrid1_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
{
string s = string.Empty;
if (e.Column is GridNumericColumn && e.Column.UniqueName.Contains("Dir"))
{
s = "test";
}
if (e.Column is GridDateTimeColumn && e.Column.UniqueName.Contains("Time"))
{
(e.Column as GridDateTimeColumn).PickerType = GridDateTimeColumnPickerType.TimePicker;
(e.Column as GridDateTimeColumn).HtmlEncode = false;
(e.Column as GridDateTimeColumn).DataFormatString = "{0:t}";
}
if (e.Column is GridDateTimeColumn && e.Column.UniqueName.Contains("Date"))
{
(e.Column as GridDateTimeColumn).PickerType = GridDateTimeColumnPickerType.DatePicker;
(e.Column as GridDateTimeColumn).HtmlEncode = false;
}
}
}
}
Thanks
Rod
0
Cori
Top achievements
Rank 2
answered on 29 Jul 2010, 02:11 PM
Well if the column's are AutoGenerated I don't see how any of them would be one of those specific types, since they should all be GridBoundColumn. So in that case I think you should be looking up the DataType property of the GridBoundColumn, instead of checking for one of the special column types (i.e. GridNumericColumn).
I hope that helps.
I hope that helps.
0
Rod Barrand
Top achievements
Rank 1
answered on 29 Jul 2010, 02:45 PM
Yes, the are all GridBoundColumns except for the one that gets date or time from a , in that case the column type is GridDateTimeColumn, however, returning an Int32 doesnt give me a GridNumericColumn
So the question there is, why can't i get a numeric column for an int when i can get a datetime column for a date. So when adding a new record that field will only accept numbers
So the question there is, why can't i get a numeric column for an int when i can get a datetime column for a date. So when adding a new record that field will only accept numbers
private
Type ConvertType(
string
szSQLServerType)
{
Type oType =
null
;
string
szDotNetType =
string
.Empty;
switch
(szSQLServerType)
{
case
"datetime"
:
case
"date"
:
case
"time"
:
oType =
typeof
(DateTime);
// this does gives me a datetimecolumn
break
;
case
"int"
:
case
"numeric"
:
oType =
typeof
(Int32);
// this should give me a numericcolumn but doesn't
break
;
}
return
oType;
0
Bodevain Svensson
Top achievements
Rank 1
answered on 04 Aug 2010, 08:58 AM
I think that the Telerik grid creates numeric columns for decimal/float source fields only. To have a numeric editor for auto-generated column bound to number field, I use FormTemplate with textboxes managed by RadInputManager (my solution is based on this demo).
Not as simple as having auto-generated numeric editor, but it works and can be used with autogenerated and declarative columns.
Bodevain
Not as simple as having auto-generated numeric editor, but it works and can be used with autogenerated and declarative columns.
Bodevain
0
vamsi
Top achievements
Rank 1
answered on 11 Aug 2016, 06:51 AM
Hi,
In grid numeric colum I am getting a problem in displaying the zeros after the decimal values(i.e 1.0000 is displayed as 1 but 1.002 is displayed as 1.002) i want to get the zeros visible in my grid numeric colum
can any one help me out
thanks in advance
0
Hi Vamsi,
Please try setting DataFormatString in the NumericTextBox column to fit the required format and see how it goes.
Regards,
Maria Ilieva
Telerik by Progress
Please try setting DataFormatString in the NumericTextBox column to fit the required format and see how it goes.
Regards,
Maria Ilieva
Telerik by Progress
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.