We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Formatting DateTime columns in GridListControl

Hello,

For the life of me I can't get a DataTime column in a GridListControl to get formatted as anything other than "d".

I've looked over quite a few previous posts about DateTime formatting in other grid-based controls but haven't been able to adapt them to GridListControl.

All I'm doing is using a SqlCommand to return a SqlDataReader that I use to fill a DataTable that I then use as the DataSource of the GridListControl. The data in the DataTable contains the full DateTime string, but all I ever get in the control is "d"-formatted when what I want is "f"-formatted.

The column name in question is "LastUpdate"; could someone provide a code snippet showing how to properly set the format?

Thanks a lot!

Keith

P.S. Using 4.4

12 Replies

AD Administrator Syncfusion Team February 2, 2007 11:50 PM UTC

Hi Keith,

You can try catching the Grid.PrepareViewStyleInfo event and set the Style.Format for required field. Here is a code snippet.

private void Grid_PrepareViewStyleInfo(object sender, Syncfusion.Windows.Forms.Grid.GridPrepareViewStyleInfoEventArgs e)
{
if( e.RowIndex > 0 && e.ColIndex == 1)
e.Style.Format ="t";
}

Please try the attached sample and let me know if you are looking something different.
DateTimeFormatGridList.zip

Best Regards,
Haneef


AD Administrator Syncfusion Team February 7, 2007 08:04 PM UTC

Haneef,

Thanks for the reply. Your suggestion works, and I'm also using it in a MultiColumnComboBox but am having trouble getting the columns to auto resize properly. I'm also unable to manually resize the colums when the ListBox is shown. I can't seem to find a property that allows manual resize, although the sample you provided clearly permits it.

Could you suggest how to enable proper auto and manual resize of columns in a MultiColumnComboBox?

Thanks!

Keith


AD Administrator Syncfusion Team February 7, 2007 08:58 PM UTC

Hi Keith,

You can use the ColWidths property to set the column of the GridListControl.

this.gridListControl1.Grid.Model.ColWidths[2] = 200;

If you want to resize the column depends on its content then you can call ColWidths.ResizeToFit method

this.gridListControl1.Grid.Model.ColWidths.ResizeToFit(GridRangeInfo.Table());

Please refer to the attached sample for more details.
DateTimeFormatGridList.zip

Best regards,
Haneef


AD Administrator Syncfusion Team February 8, 2007 06:05 PM UTC

Haneef,

Thanks for the reply. Perhaps I was unclear when I described my problem with resizing columns. I would like them to resize based on the content of the data in the cells NOT the length of the column header label.

If you change the format string in your sample to "f" you can clearly see that the DateTime information is truncated both before and after ResizeToFit is called.

Is there a way to autoresize columns based on the width of data in the cells? If so, can this also be used with a fixed minimum column width?

Thanks!

Keith


AD Administrator Syncfusion Team February 8, 2007 06:52 PM UTC

Hi Keith,

Thanks for your update.

The reason for getting this behavior is that you are using the PrepareViewStyleinfo event to set the DataTime format. The PrepareviewStyleInfo event does not store any styleInfo properties in a grid. It just set the visual apperence of the grid. ResizeToFit method resizes a range of rows or columns to optimally fit stored contents of the specified range of cells in grid. It depends on the data stored in a grid. If the grid doesn''''t have any data, ResizeToFit method doesn't make any changes in a grid. Please refer to the modified attached sample that shows you "How to set the date time cell format in QueryCellInfo event?" and also it uses the ResizeToFit methods to resize the column based on its content.

Sample : DateTimeFormatGridList.zip

Best regards,
Haneef


AD Administrator Syncfusion Team February 9, 2007 06:31 PM UTC

Thanks Haneef!

That's just what I was looking for.

Keith


AD Administrator Syncfusion Team February 9, 2007 10:14 PM UTC

Haneef,

Here are some additional issues I'm having with the MultiColumnComboBox.

First, when I set the MultiColumnComboBox.ReadOnly property to true I can no longer make the GridListView appear by clicking on the widget. It is equivalent behavior to setting Enabled == false;

My understanding was the setting the ReadOnly property to true would disallow editing of the value displayed in the combobox. In all other respects the behavior would be the same. Is my understanding incorrect or is this a bug?

Second, if the width of the GridListControl is slightly greater than the width of the combobox, a righthand scrollbar appears when the GridListControl is exposed, regardless of if it is actually needed or not (for example, only 3 items in the list when max items is 8). Increasing the width of the combobox or increasing the width of the GridListControl (adding a column for example) eliminates the scrollbar in question.

Manually setting the width of the GridListControl works, but setting FillLastColumn == true often doesn't behave as expected.

Please let me know if you need any additional information or code to help reproduce these issues.

Thanks!

Keith


AD Administrator Syncfusion Team February 12, 2007 11:53 PM UTC

Bump


AD Administrator Syncfusion Team February 13, 2007 07:42 PM UTC

Hi Keith,

Issue 1:
You can use the TextBox.TextChanged event of the MultiColumnComboBox and set the old text to the TextBox present in the MultiColumnComboBox control.

//Load event.
OldValue = this.multiColumnComboBox1.TextBox.Text;
this.multiColumnComboBox1.TextBox.TextChanged +=new EventHandler(TextBox_TextChanged);

private void TextBox_TextChanged(object sender, EventArgs e)
{
this.multiColumnComboBox1.TextBox.Text =OldValue ;
}

Issue 2:

If you want to change the dropdown size of the MultiColumnComboBox then set .PopupContainer.Size property in a BeforePopup event of the PopupContainer.

private void PopupContainer_BeforePopup(object sender, CancelEventArgs e)
{
this.multiColumnComboBox1.PopupContainer.Size = yourSize;
}

Here is a minimal sample that works fine with FillLastColumn property.
DateTimeFormatGridList.zip

Please try setting the following property to disable to horizontal and vertical scrollbar in a dropdown of the multicolumn combobox.

this.multiColumnComboBox1.ListBox.Grid.HScrollBehavior = Syncfusion.Windows.Forms.Grid.GridScrollbarMode.Disabled;
this.multiColumnComboBox1.ListBox.Grid.VScrollBehavior = Syncfusion.Windows.Forms.Grid.GridScrollbarMode.Disabled;
this.multiColumnComboBox1.ListBox.Grid.VScroll = false;
this.multiColumnComboBox1.ListBox.Grid.HScroll = false;

Best regards,
Haneef


AD Administrator Syncfusion Team February 14, 2007 08:33 PM UTC

Haneef,

Thanks for the reply. I have some additional comments regarding your suggestions.

First, using the TextChanged event to reset the Text of the ListBox works to simulate readonly behavior, although I have to somehow change the background color of the textbox to make it LOOK like it is readonly; in addition to having to store previous values which, since I have custom display values, is a bit of a pain. Perhaps my expectations are off, but this seems a long way to go for what I thought I'd get for setting a simple property. Meaning, if ReadOnly is true, the user can still expose the grid to make selections but cannot handedit the value shown in the textbox.

Second, using BeforePopup works fine to resize the listbox. However, FillLastColumn does NOT work if the underlying grid has hidden columns. This is easy to see if you add the following code to Form1_Load in your sample:
this.multiColumnComboBox1.ListBox.Grid.Cols.Hidden["Col3"] = true;
this.multiColumnComboBox1.ListBox.Grid.Cols.Hidden["Col2"] = true;

The number of hidden columns, the number of total columns, and the width of the PopupContainer all seem to affect when the last column is or isn't filled. Could you verify this issue, and give me an idea how to calculate the visible width of the grid so I can use that size to set the PopupContainer's width?

Finally, regarding the scrollbars; turning them off works but the fact that I need to suggests this is an issue with the behavior of the control itself. Meaning, there are times when it can't correctly figure out when it should display scroll bars or not. I can certainly track the number of rows in the data source and turn the scrollbars on/off depending on the number of max display items in the list, but that sounds like the control's job to me.

In any case, I appreciate your assistance so far and hope to hear your opinion on these other matters.

Ragards,

Keith


AD Administrator Syncfusion Team February 22, 2007 07:58 PM UTC

bump


AD Administrator Syncfusion Team February 22, 2007 09:53 PM UTC

Hi Keith,

Regarding 2nd Issue :

You can set the value of the last visible colum width to remaining client area of the grid in BeforePopup event of the Popupcontainer of the grid. Here is a code snippet.

int width = this.multiColumnComboBox1.ListBox.Grid.ColWidths.GetTotal(0,iMaxColIndex -1);
this.multiColumnComboBox1.ListBox.Grid.ColWidths[iMaxColIndex] = this.multiColumnComboBox1.ListBox.Grid.ClientSize.Width - width;

Please refer to the attached sample for more details.
DateTimeFormatGridList.zip

Regarding the 1st & 3rd issue:
I am investigating on this issue. I will update you soon.

Best regards,
Haneef

Loader.
Live Chat Icon For mobile
Up arrow icon