This is a migrated thread and some comments may be shown as answers.

Dynamic column creation and use

1 Answer 266 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Stan
Top achievements
Rank 1
Stan asked on 12 Dec 2014, 10:49 PM
I'm fetching column names and column types (Text, Note, DateTime, Number, and Currency) from another system in my c# code behind.

I use this to create a query to return back a datatable. I also use this to create columns for a RadGrid control my my ASPX page.

This is what I'm using to add fields to the grid:

private void AddFieldToRadGrid(string internalName, string type, string title)
        {
            if (rgStudyInfo.MasterTableView.Columns.FindByUniqueNameSafe(internalName) == null)
            {
                if (string.IsNullOrEmpty(title)) title = internalName;
                GridBoundColumn column = new GridBoundColumn();
                column.HeaderText = title;
                column.AllowSorting = true;
                column.AllowFiltering = true;
                column.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
                column.DataField = internalName;
                column.EmptyDataText = "N/A";
                column.UniqueName = internalName;
                column.SortExpression = internalName;
                column.ItemStyle.VerticalAlign = VerticalAlign.Top;
                Type typ;
                switch (type)
                {
                    case "DateTime":
                        typ = typeof(System.DateTime);
                        break;
                    case "Number":
                        typ = typeof(System.Int32);
                        break;
                    case "Currency":
                        typ = typeof(System.Decimal);
                        break;
                    default:
                        typ = typeof(System.String);
                        break;
                }
                column.DataType = typ;
                rgStudyInfo.MasterTableView.Columns.Add(column);
            }
        }

I read online that if the grid itself is delcared via markup, the new columns should be added during Page_Load within a !Page.IsPosback test. I can't get the dataformatstring to work if I add it here (using "{0:dd-MMM-yyyy}" for it in the case "DateTime" above). So I was attempting to format date fields during the itemdatabound event. But when I check to see if a field is type DateTime, I find that all fields are String. I checked if e.Item is GridDataItem, then instantiated a GridDataItem variable (e.Item as GridDataItem) to get the row.

The only place I found a referenced data type was in ((DataRowView)item.DataItem).Row.Table.Columns[col].DataType (col is an int index). And as I said, it's always System.String. Dates are in the columns, as strings that include date as well as time.

Also, since I programmatically added columns, do I need to do something special to handle sorting when users click column headers? When I click on them, the column headers' tex disappears and the grid isn't sorted.  Filtering breaks the page, but I haven't looked into that yet. Do I need to do something special for that since the columns were added programatically?

Data is loading fine during ItemDataBound event, and column headers are properly shown.


1 Answer, 1 is accepted

Sort by
0
Pavlina
Telerik team
answered on 17 Dec 2014, 08:53 PM
Hello,

Could you confirm that the data type of the column you are accessing on ItemDataBound is DateTime in the database?

Regarding the sorting issue, could you confirm that you are not using simple databinding calling DataBind() to populate the grid. If this is the case switch to advanced data binding and see if any issues arise:
http://demos.telerik.com/aspnet-ajax/grid/examples/data-binding/simple-vs-advanced/defaultcs.aspx


Regarding


Regards,
Pavlina
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Stan
Top achievements
Rank 1
Answers by
Pavlina
Telerik team
Share this question
or