Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
186
Removing DatePicker from DateColumn
posted

Our xamwebgrid is set up to autogenerate columns as it binds dynamically to multiple datasets. When it generates columns of DateTime type it creates a DateColumn with the DatePicker. Our data is read only so we don't need the datepicker and we need the date/times to show in this format  "12/31/2009  13:35:00"

If we parse the dates to string, it messes up our date sort orders as well as the formatting we apply to current records. For instance 01/01/2010 will sort before 01/07/2010.

Is there a way to remove the DatePicker from the DateColumns when the datecolumns are autogenerated?

Parents
  • 21382
    Suggested Answer
    posted

    The XamWebGrid has a collection property off it called ColumnTypeMappings.  The objects in this collection define what type of column to use based on the DataType of the underlying data.  As of 9.2 a DateTime or DateTime? data field would be mapped to the Date column.

     

    There are two ways you could accomplish what you would like to do.

     

    The first would be to remove the entries in the ColumnTypeMappings table which control what column would be used.  

                      int count = this.grid.ColumnTypeMappings.Count;

                      for (int i = count - 1; i >= 0; i--)

                      {

                            if (this.grid.ColumnTypeMappings[i].DataType == typeof(DateTime))

                            {

                                  this.grid.ColumnTypeMappings.RemoveAt(i);

                            }

                            else if (this.grid.ColumnTypeMappings[i].DataType == typeof(DateTime?))

                            {

                                  this.grid.ColumnTypeMappings.RemoveAt(i);

                            }

                      }

     

    You could instead just modify the mappings as well:

                      int count = this.grid.ColumnTypeMappings.Count;

                      for (int i = count - 1; i >= 0; i--)

                      {

                            if (this.grid.ColumnTypeMappings[i].DataType == typeof(DateTime))

                            {

                                  this.grid.ColumnTypeMappings[i].ColumnType = typeof(TextColumn);

                            }

                            else if (this.grid.ColumnTypeMappings[i].DataType == typeof(DateTime?))

                            {

                                  this.grid.ColumnTypeMappings[i].ColumnType = typeof(TextColumn);

                            }

                      }

     

    Either should work.  I would do this in the Load of the page.

     

Reply Children