Paps said:
Hi all,
I have populated my grid from a dataSet.
1) I want to enter certain values and in Caps in my second col. Like I want
to enter only 'PAT' or 'SAT'. is this possible.
2) When my Grid populatyes the fourth col is not fully visible. I will have
to increase the width each time when it displays. Is it possible to set the
width of the 4th col only.
3) In my fifth col I want to enter date in this format only 'MM/DD/YYYY' and
should not allow any charcaters in it but Date.
You'll need to use TableStyles and DataGridColumnStyles. Here is an example of a DataGrid that might display File Name, File Size, and Last Modified date:
/* The DataGridTableStyle AllowSorting property
* overrides the DataGrid AllowSorting property. */
DataGridTableStyle style = new DataGridTableStyle();
style.MappingName = dataSet.FileInfo.TableName;
style.AllowSorting = false;
style.AlternatingBackColor = Color.AliceBlue;
style.HeaderForeColor = Color.Blue;
style.DataGrid = this.dataGrid1;
/* This works for most text columns. I am displaying a a file name
* so I set the ReadOnly property to true. I also set the
* NullText property to get rid of the "null" that is displayed by
* default. */
DataGridTextBoxColumn textBoxColumn = new DataGridTextBoxColumn();
textBoxColumn.MappingName = "FileName";
textBoxColumn.Format = "";
textBoxColumn.FormatInfo = null;
textBoxColumn.HeaderText = "File Name";
textBoxColumn.NullText = "";
textBoxColumn.ReadOnly = true;
textBoxColumn.Width = 350;
style.GridColumnStyles.Add(textBoxColumn);
/* I use the Format property to display the File Size in KB
* rather than Bytes by adding that extra comma to the left
* of the period in my format string. */
DataGridTextBoxColumn numberColumn = new DataGridTextBoxColumn();
numberColumn.MappingName = "FileSize";
numberColumn.Alignment = HorizontalAlignment.Right;
numberColumn.Format = "#,###,. 'KB '";
numberColumn.FormatInfo = null;
numberColumn.HeaderText = "Size in KB";
numberColumn.NullText = "";
numberColumn.ReadOnly = true;
numberColumn.Width = 75;
style.GridColumnStyles.Add(numberColumn);
/* I use a custom date format in this Format property to display
* the date in a typically European style (e.g. 13 SEP 2004) and
* append the time in hours and minutes only. The 'tt' after the
* time will display 'am' or 'pm'. */
DataGridTextBoxColumn dateTimeColumn = new DataGridTextBoxColumn();
dateTimeColumn.MappingName = "LastModified";
dateTimeColumn.Alignment = HorizontalAlignment.Right;
dateTimeColumn.Format = "dd MMM yyyy hh:mm tt";
dateTimeColumn.FormatInfo = null;
dateTimeColumn.HeaderText = "Last Modified";
dateTimeColumn.NullText = "";
dateTimeColumn.ReadOnly = true;
dateTimeColumn.Width = 150;
style.GridColumnStyles.Add(dateTimeColumn);
/* Always add the the TableStyle to the DataGrid AFTER
* the ColumnStyles have been added to the TableStyle. */
this.dataGrid1.TableStyles.Add(style);