db grid

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.


Can some one help .

Tnx
 
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);
 
Vagabond Software said:
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);

I tried adding the code with what you gave but I get all kinds of error:
Here is the code whic I put in my form Load event:

I have stuck a Datagrid on my form its called Datagrid1

DataGridTableStyle(style = New DataGridTableStyle())
'style.MappingName = DataSet.FileInfo.TableName
style.AllowSorting = False
style.AlternatingBackColor = Color.AliceBlue
style.HeaderForeColor = Color.Blue
style.DataGrid = this.dataGrid1

I get the error @
'DataGridTableStyle(Style
DatagridTableStyle is a Type only and cannot be used as an expression.
Am I missing out something here.

Tnx
 
Paps said:
I tried adding the code with what you gave but I get all kinds of error:
Here is the code whic I put in my form Load event:

I have stuck a Datagrid on my form its called Datagrid1

DataGridTableStyle(style = New DataGridTableStyle())
'style.MappingName = DataSet.FileInfo.TableName
style.AllowSorting = False
style.AlternatingBackColor = Color.AliceBlue
style.HeaderForeColor = Color.Blue
style.DataGrid = this.dataGrid1

I get the error @
'DataGridTableStyle(Style
DatagridTableStyle is a Type only and cannot be used as an expression.
Am I missing out something here.

I'm not that familiar Visual Basic.NET syntax, so double-check my syntax against other similar syntax used for similar declarations. For example:

Dim style As New DataGridTableStyle()

Also, it looks like you have the MappingName property assignment commented out. You'll need to map that to a TableName of an existing DataTable contained in your DataGrid1.DataSource.

Finally, this may simply be a typo in your post, but you say that you have a DataGrid control named "DataGrid1", but you assign the DataGrid property of the TableStyle to "this.dataGrid1" (with a small 'd').

Here is an example of using DataGridTableStyles with Visual Basic.NET from Microsoft:
http://msdn.microsoft.com/library/d...agridtablestyleclassgridcolumnstylestopic.asp

I hope that helps.

- carl
 
Has anyone gotten this done? Specifically we don't want very much. We can
sort of see how to do it from the various resources that have been
referenced, but it seems like jumping through alot more hoops and coding
than should be necessary. We just want to change the "default" format for
how the datagrid handles dates from mm/dd/yy to include the time (mm/dd/yy
hh:mm). We use the same datagrid to load results from various mssql
datasets and it all works fine, except we want the long date/time format.
Since we load various datasets into the same grid we don't know ahead of
time how many columns there will be, what the columns are, or which ones are
dates. The datagrid seems to handle all this fine filling in the column
headers, number of columns --- everything just fine based on the dataset,
BUT somehow when it puts in a date in does mm/dd/yy.

Paps said:
I tried adding the code with what you gave but I get all kinds of error:
Here is the code whic I put in my form Load event:

I have stuck a Datagrid on my form its called Datagrid1

DataGridTableStyle(style = New DataGridTableStyle())
'style.MappingName = DataSet.FileInfo.TableName
style.AllowSorting = False
style.AlternatingBackColor = Color.AliceBlue
style.HeaderForeColor = Color.Blue
style.DataGrid = this.dataGrid1

I get the error @
'DataGridTableStyle(Style
DatagridTableStyle is a Type only and cannot be used as an expression.
Am I missing out something here.

I'm not that familiar Visual Basic.NET syntax, so double-check my syntax
against other similar syntax used for similar declarations. For example:

Dim style As New DataGridTableStyle()

Also, it looks like you have the MappingName property assignment commented
out. You'll need to map that to a TableName of an existing DataTable
contained in your DataGrid1.DataSource.

Finally, this may simply be a typo in your post, but you say that you have a
DataGrid control named "DataGrid1", but you assign the DataGrid property of
the TableStyle to "this.dataGrid1" (with a small 'd').

Here is an example of using DataGridTableStyles with Visual Basic.NET from
Microsoft:
http://msdn.microsoft.com/library/d...agridtablestyleclassgridcolumnstylestopic.asp

I hope that helps.

- carl
 
are you using winforms or web forms? the action you would take is different
for each.
 
Back
Top