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:
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.
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.