dataGridView控件DateTime列插入DateTimePicker
2007-09-15 11:37
375 查看
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public Form1()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
InitializeComponent();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//定义一个DateTimePicker控件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private DateTimePicker dTimePicker = new DateTimePicker();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//定义一个DataSet
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private DataSet ds;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void Form1_Load(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//隐藏dTimePicker
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dTimePicker.Visible = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//dataGirdView1控件数据绑定
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Data_Bind();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//定义控件dTimePicker当Text变更时委托于事件dTimePicker_TextChanged
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dTimePicker.TextChanged += new EventHandler(dTimePicker_TextChanged);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//dataGridView1添加控件dTimePicker
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dataGridView1.Controls.Add(dTimePicker);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// dataGirdView绑定数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void Data_Bind()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string sqlstr = "select businessname,lastlogindate from business";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ds = SqlHelper.ExecuteDS(CommandType.Text, sqlstr);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dataGridView1.DataSource = ds.Tables[0].DefaultView;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//事件dTimePicker_TextChanged,当dTimePicker当Text变更时触发
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void dTimePicker_TextChanged(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//把dTimePicker的Text值赋给dataGridView1选中的单元格值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dataGridView1.CurrentCell.Value = dTimePicker.Text;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//当dataGridView1滚动条移动时dTimePicker控件隐藏
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dTimePicker.Visible = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void dataGridView1_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//当dataGridView1的列宽改变时dTimePicker控件隐藏
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dTimePicker.Visible = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//判断选中行时日期类型
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//dt = ds.Tables[0];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Type t = ds.Tables[0].Columns[dataGridView1.CurrentCell.ColumnIndex].DataType;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DateTime d = DateTime.Now;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (t== d.GetType() )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//获得dataGridView1选中单元格显示区域的矩形
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Rectangle Rect = this.dataGridView1.GetCellDisplayRectangle(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex, false);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//显示dTimePicker在dataGridView1选中单元格显示区域的矩形里面,即选中单元格内
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dTimePicker.Visible = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dTimePicker.Top = Rect.Top;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dTimePicker.Left = Rect.Left;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dTimePicker.Height = Rect.Height;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dTimePicker.Width = Rect.Width;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//dTimePicker控件获取初始值为dataGridView1选中单元格值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dTimePicker.Text=dataGridView1.CurrentCell.Value.ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
~Form1()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ds.Dispose();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ds.Clear();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.GC.Collect();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
}
相关文章推荐
- DataGridView控件DateTime列插入DateTimePicker
- dataGridView控件DateTime列插入DateTimePicker
- dataGridView控件DateTime列插入DateTimePicker
- [转]dataGridView控件DateTime列插入DateTimePicker
- C# winform DataGridView DateTimePicker 控件
- datagridview“当控件与数据源绑定后,无法插入行”解决办法
- 向DataGridView控件里插入行!
- datagridview控件插入空白行
- c#datagridview中添加时间控件DateTimePicker
- C#中解决插入数据库的某个字段类型为datetime 为空
- 【VB.net】DataGridView控件的使用
- 【C#机房重构】 DataGridView控件的使用
- 跨线程调用DataGridView控件
- VC 6.0画简单的ICON图片,并将ICON图片插入到LlistCtrl控件的第一行第一列中
- PB OLE控件插入报错-PB最大的敌人--支付宝
- 在列表视图控件中插入图标
- 由 datagridview 控件向 Microsoft Excel 表格中导出
- 向数据库中插入有DateTime字段的行
- 【分享】如何设计WinForm中DataGridView控件的自定义按钮列
- Textbox控件插入换行符的问题