随笔-0  评论-34  文章-19  trackbacks-0

Winform下的Datagrid的列风格(3)—DataGridTimePickerColumn(转)

转载自msdn,显示datagrid中日期型列样式
class DataGridTimePickerColumn : DataGridColumnStyle 
    
{
        
private DateTimePicker myDateTimePicker = new DateTimePicker();
        
// The isEditing field tracks whether or not the user is
        
// editing data with the hosted control.
        private bool isEditing;

        
public DataGridTimePickerColumn() : base() 
        
{
            myDateTimePicker.Visible 
= false;
        }


        
protected override void Abort(int rowNum)
        
{
            isEditing 
= false;
            myDateTimePicker.ValueChanged 
-= 
                
new EventHandler(TimePickerValueChanged);
            Invalidate();
        }


        
protected override bool Commit
            (CurrencyManager dataSource, 
int rowNum) 
        
{
            myDateTimePicker.Bounds 
= Rectangle.Empty;
         
            myDateTimePicker.ValueChanged 
-= 
                
new EventHandler(TimePickerValueChanged);

            
if (!isEditing)
                
return true;

            isEditing 
= false;

            
try 
            
{
                DateTime value 
= myDateTimePicker.Value;
                SetColumnValueAtRow(dataSource, rowNum, value);
            }
 
            
catch (Exception) 
            
{
                Abort(rowNum);
                
return false;
            }


            Invalidate();
            
return true;
        }


        
protected override void Edit(
            CurrencyManager source, 
            
int rowNum,
            Rectangle bounds, 
            
bool readOnly,
            
string instantText, 
            
bool cellIsVisible) 
        
{
            DateTime value 
= (DateTime) 
                GetColumnValueAtRow(source, rowNum);
            
if (cellIsVisible) 
            
{
                myDateTimePicker.Bounds 
= new Rectangle
                    (bounds.X 
+ 2, bounds.Y + 2
                    bounds.Width 
- 4, bounds.Height - 4);
                myDateTimePicker.Value 
= value;
                myDateTimePicker.Visible 
= true;
                myDateTimePicker.ValueChanged 
+= 
                    
new EventHandler(TimePickerValueChanged);
            }
 
            
else 
            
{
                myDateTimePicker.Value 
= value;
                myDateTimePicker.Visible 
= false;
            }


            
if (myDateTimePicker.Visible)
                DataGridTableStyle.DataGrid.Invalidate(bounds);
        }


        
protected override Size GetPreferredSize(
            Graphics g, 
            
object value) 
        
{
            
return new Size(100, myDateTimePicker.PreferredHeight + 4);
        }


        
protected override int GetMinimumHeight() 
        
{
            
return myDateTimePicker.PreferredHeight + 4;
        }


        
protected override int GetPreferredHeight(Graphics g, 
            
object value) 
        
{
            
return myDateTimePicker.PreferredHeight + 4;
        }


        
protected override void Paint(Graphics g, 
            Rectangle bounds, 
            CurrencyManager source, 
            
int rowNum) 
        
{
            Paint(g, bounds, source, rowNum, 
false);
        }

        
protected override void Paint(
            Graphics g, 
            Rectangle bounds,
            CurrencyManager source, 
            
int rowNum,
            
bool alignToRight) 
        
{
            Paint(
                g,bounds, 
                source, 
                rowNum, 
                Brushes.Red, 
                Brushes.Blue, 
                alignToRight);
        }

        
protected override void Paint(
            Graphics g, 
            Rectangle bounds,
            CurrencyManager source, 
            
int rowNum,
            Brush backBrush, 
            Brush foreBrush,
            
bool alignToRight) 
        
{
            DateTime date 
= (DateTime) 
                GetColumnValueAtRow(source, rowNum);
            Rectangle rect 
= bounds;
            g.FillRectangle(backBrush,rect);
            rect.Offset(
02);
            rect.Height 
-= 2;
            g.DrawString(date.ToString(
"d"), 
                
this.DataGridTableStyle.DataGrid.Font, 
                foreBrush, rect);
        }


        
protected override void SetDataGridInColumn(DataGrid value) 
        
{
            
base.SetDataGridInColumn(value);
            
if (myDateTimePicker.Parent != null
            
{
                myDateTimePicker.Parent.Controls.Remove 
                    (myDateTimePicker);
            }

            
if (value != null
            
{
                value.Controls.Add(myDateTimePicker);
            }

        }


        
private void TimePickerValueChanged(object sender, EventArgs e) 
        
{
            
this.isEditing = true;
            
base.ColumnStartedEditing(myDateTimePicker);
        }

    }
posted on 2005-04-02 12:38 爱好 阅读(71241) 评论(5)  编辑 收藏

评论:
#1楼 2005-05-11 22:03 | allentao[未注册用户]
当数据源中的值为null时会报错“指定的类型转换无效”,后发现是两次地方的错误。
现提供给大家:
1.
protected override void Edit(
CurrencyManager source,
int rowNum,
Rectangle bounds,
bool readOnly,
string instantText,
bool cellIsVisible)
{
//Allen fixed it
DateTime value;
try
{
value=(DateTime)GetColumnValueAtRow(source, rowNum);
}
catch
{
value=DateTime.Today;
}
//fixing ends
// DateTime value = (DateTime)
// GetColumnValueAtRow(source, rowNum);
if (cellIsVisible)
{
myDateTimePicker.Bounds = new Rectangle
(bounds.X + 2, bounds.Y + 2,
bounds.Width - 4, bounds.Height - 4);
myDateTimePicker.Value = value;
myDateTimePicker.Visible = true;
myDateTimePicker.ValueChanged +=
new EventHandler(TimePickerValueChanged);
}
else
{
myDateTimePicker.Value = value;
myDateTimePicker.Visible = false;
}

if (myDateTimePicker.Visible)
DataGridTableStyle.DataGrid.Invalidate(bounds);
}

2.
protected override void Paint(
Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum,
Brush backBrush,
Brush foreBrush,
bool alignToRight)
{
//Allen fixed it
string str;
try
{
DateTime date= (DateTime)
GetColumnValueAtRow(source, rowNum);
str=date.ToShortDateString();
}
catch
{
str=String.Empty;
}
Rectangle rect = bounds;
g.FillRectangle(backBrush,rect);
rect.Offset(0, 2);
rect.Height -= 2;
g.DrawString(str,
this.DataGridTableStyle.DataGrid.Font,
foreBrush, rect);
//fixing ends

// DateTime date = (DateTime)
// GetColumnValueAtRow(source, rowNum);
// Rectangle rect = bounds;
// g.FillRectangle(backBrush,rect);
// rect.Offset(0, 2);
// rect.Height -= 2;
// g.DrawString(date.ToString("d"),
// this.DataGridTableStyle.DataGrid.Font,
// foreBrush, rect);
}

 回复 引用   
#2楼 2005-05-11 22:11 | allentao[未注册用户]
hzy_dl:
文章的开头中提到这个类的代码是来自msdn,但我在msdn中搜索不到,你能给出它的原始网址吗?
多谢!
我的邮箱是allentao@126.com

 回复 引用   
#3楼 2005-05-30 11:05 | hzy_dl
DataGridTimePickerColumn好象是vs.net2003中的,我一直以为其中的帮助和msdn中一样。
 回复 引用   
#4楼 2005-06-30 18:23 | 海天之间
能否给个可以编译的。呵呵。这个网页上的复制下来一堆的文本上要修改的。大侠,给个源码好么。感激不尽
simtel_006@126.com
顺便吧您那个插入一个TimePacker的也发个好么。谢谢1`

 回复 引用   
#5楼 2005-06-30 18:24 | 海天之间
看错了。是插入combobox的
 回复 引用   
昵称:爱好
园龄:6年10个月
粉丝:0
关注:0

搜索

 
 

文章分类

文章档案

最新评论