Hello,
I used a tableStyle to format my columns in my datagrid.
DataGridTextBoxColumn dcFinalDate = new DataGridTextBoxColumn(); dcFinalDate.HeaderText = "Final date"; dcFinalDate.MappingName = "UGFNDT"; dcFinalDate.Width=100; dcFinalDate.NullText=""; dcFinalDate.Format="####/##/##"; tableStyle.GridColumnStyles.Add(dcFinalDate);
It works good if there is a string entered, fe: 20060202, it will be 2006/02/02. But when there is no string (because the field is not required), then I'll get // .... How can I get rid of that ?
Greets,
Dirk
| | Dirk Haest Friday, September 29, 2006 6:05 AM | You could use a regular expression to see what kind of data you have
| |
protected override object GetColumnValueAtRow(CurrencyManager source, int rowNum) { DateTime dt; String strIn; String strOut =""; System.Text.RegularExpressions.Regex regDate; regDate = new System.Text.RegularExpressions.Regex(@"\d{8}$"); if (base.GetColumnValueAtRow(source, rowNum).Equals(DBNull.Value)) { return this.NullText; } strIn= base.GetColumnValueAtRow(source, rowNum).ToString(); if (regDate.IsMatch(strIn)) { try { dt = DateTime.ParseExact(strIn, "yyyyddMM", new System.Globalization.CultureInfo("en-US")); strOut = dt.ToShortDateString(); } catch { } } else { strOut = strIn; } return strOut; }
|
| | Ken Tucker Sunday, October 08, 2006 5:12 PM | I would use a DateTimeFormatInfo to set the date format.
| |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; using System.Globalization; namespace DataGrid { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { String strConn = "Server = .\\SQLEXPRESS;Database = Pubs;Integrated Security = SSPI;"; DataTable dt = new DataTable(); SqlConnection conn = new SqlConnection(strConn); SqlDataAdapter da = new SqlDataAdapter("Select Title, Pubdate from titles", conn); da.Fill(dt); dataGrid1.DataSource = dt; CurrencyManager cm = this.BindingContext[dt] as CurrencyManager; PropertyDescriptor pd = cm.GetItemProperties()["pubdate"]; DateTimeFormatInfo df = new DateTimeFormatInfo(); df.ShortDatePattern = "yyyy/MM/dd"; DataGridTableStyle ts = new DataGridTableStyle(); ts.MappingName = dt.TableName; DataGridTextBoxColumn tcTitle = new DataGridTextBoxColumn(); tcTitle.MappingName = "Title"; tcTitle.HeaderText = "Title"; tcTitle.Width = 250; DataGridTextBoxColumn tcDate = new DataGridTextBoxColumn(pd, "d"); tcDate.MappingName = "pubdate"; tcDate.HeaderText = "Pub Date"; tcDate.Width = 150; tcDate.FormatInfo=df; tcDate.NullText = ""; ts.GridColumnStyles.Add(tcTitle); ts.GridColumnStyles.Add(tcDate); dataGrid1.TableStyles.Add(ts); DataRow dr = dt.NewRow(); dr["Title"] = "My new book"; dr["pubdate"] = DBNull.Value; dt.Rows.Add(dr); } } }
|
| | Ken Tucker Monday, October 02, 2006 12:02 AM | I tried it, but the problem is that I use a derived class of DataGridTextBox.
Perhaps i'm missing something, because I can't do the following: DataGridTextBoxColumn tcDate = new DataGridTextBoxColumn(pd, "d");
using System; using System.Data; using System.Windows.Forms; using System.Drawing;
namespace Pfizer.EMEA.Toolkit.Controls.DataGridColumns {
/// <summary> /// Custom grid Textboxcolumn to provide rowselection. /// </summary> public class DataGridTextBoxColumnNoActive : DataGridTextBoxColumn { #region "Private variables" private int _selectedRow; #endregion
#region "Overrides" protected override void Edit(CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible) { try { if ((_selectedRow > - 1) && (_selectedRow < source.List.Count + 1) ) this.DataGridTableStyle.DataGrid.UnSelect(_selectedRow); _selectedRow = rowNum; DataGridTableStyle.DataGrid.Select(_selectedRow); } catch(Exception ex) {} } #endregion }
/// <summary> /// Custom grid BoolColumn to provide rowselection. /// </summary>
}
| | Dirk Haest Tuesday, October 03, 2006 11:25 AM | If you created a custom column style to begin with. Try overriding the GetColumnValueAtRow procedure for the datagridtextbox column you can format the value better from there | | Ken Tucker Tuesday, October 03, 2006 4:26 PM | Perhaps a usefull remark. The data resides on a AS/400 where the data is stored as : 20061008 (decimal value).
If possible I want to show it as: 08/10/2006 or 2006/10/08 | | Dirk Haest Friday, October 06, 2006 8:31 AM | You are going to have to use DateTime.ParseExact to convert the integer to a date. Try something like this.
| |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; using System.Globalization; namespace DataGrid { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add("pubdate", typeof (Int32)); dataGrid1.DataSource = dt;
DataGridTableStyle ts = new DataGridTableStyle(); ts.MappingName = dt.TableName; DataGridTextBoxColumn tcDate = new DataGridTextBoxColumnNoActive(); tcDate.MappingName = "pubdate"; tcDate.HeaderText = "Pub Date"; tcDate.Width = 150; tcDate.NullText = ""; ts.GridColumnStyles.Add(tcDate); dataGrid1.TableStyles.Add(ts); DataRow drNew = dt.NewRow(); drNew["pubdate"] = 20061008; DataRow dr = dt.NewRow(); dr["pubdate"] = DBNull.Value; dt.Rows.Add(drNew); dt.Rows.Add(dr); } } public class DataGridTextBoxColumnNoActive : DataGridTextBoxColumn { #region "Private variables" private int _selectedRow; #endregion #region "Overrides" protected override void Edit(CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible) { try { if ((_selectedRow > -1) && (_selectedRow < source.List.Count + 1)) this.DataGridTableStyle.DataGrid.UnSelect(_selectedRow); _selectedRow = rowNum; DataGridTableStyle.DataGrid.Select(_selectedRow); } catch (Exception ex) { } } protected override object GetColumnValueAtRow(CurrencyManager source, int rowNum) { DateTime dt; String strIn; String strOut = this.NullText; strIn= base.GetColumnValueAtRow(source, rowNum).ToString(); try { dt = DateTime.ParseExact(strIn,"yyyyddMM", new System.Globalization.CultureInfo("en-US")); strOut = dt.ToShortDateString(); } catch { } return strOut; } #endregion } }
|
| | Ken Tucker Saturday, October 07, 2006 5:59 PM | protected override object GetColumnValueAtRow(CurrencyManager source, int rowNum) { DateTime dt; String strIn; String strOut = this.NullText; strIn= base.GetColumnValueAtRow(source, rowNum).ToString(); try { dt = DateTime.ParseExact(strIn,"yyyyddMM", new System.Globalization.CultureInfo("en-US")); strOut = dt.ToShortDateString(); } catch { } return strOut; }
Is there a way to determin which column we're formatting then ? Because the result is very dynamic. The end-user can choose which field he wants to see in it's output. Some are text-values, other numeric values and some are date-values... | | Dirk Haest Sunday, October 08, 2006 7:39 AM | You could use a regular expression to see what kind of data you have
| |
protected override object GetColumnValueAtRow(CurrencyManager source, int rowNum) { DateTime dt; String strIn; String strOut =""; System.Text.RegularExpressions.Regex regDate; regDate = new System.Text.RegularExpressions.Regex(@"\d{8}$"); if (base.GetColumnValueAtRow(source, rowNum).Equals(DBNull.Value)) { return this.NullText; } strIn= base.GetColumnValueAtRow(source, rowNum).ToString(); if (regDate.IsMatch(strIn)) { try { dt = DateTime.ParseExact(strIn, "yyyyddMM", new System.Globalization.CultureInfo("en-US")); strOut = dt.ToShortDateString(); } catch { } } else { strOut = strIn; } return strOut; }
|
| | Ken Tucker Sunday, October 08, 2006 5:12 PM | I understand that, but it's complexer than that.
Some values in decimal fields, are meant to be as a decimal field (fe it contains a item-number, ...) but other contain dates. It's always depending of the column-name. | | Dirk Haest Sunday, October 08, 2006 6:12 PM | | |
if(this.MappingName == "pubDate") { etc.... }
|
| | Ken Tucker Sunday, October 08, 2006 6:20 PM |
|