• 大小: 81KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-13
  • 语言: C#
  • 标签:

资源简介

DataGridView中有文本框列,下拉框列,图像列,链接列,却没有DateTime列,这里实现在编辑DataGridView时可以录入DateTime类型数据。

资源截图

代码片段和文件信息

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;

namespace CRMControlDemo
{
    public partial class Form1 : Form
    {
        public Form1()
       {
            InitializeComponent();
            this.Load+=new EventHandler(Form1_Load);
            dataGridView1.Scroll+=new ScrollEventHandler(dataGridView1_Scroll);
            dataGridView1.ColumnWidthChanged+=new DataGridViewColumnEventHandler(dataGridView1_ColumnWidthChanged);
            dataGridView1.CellClick+=new DataGridViewCellEventHandler(dataGridView1_CellClick);
        }

      

        //定义一个DateTimePicker控件
        private DateTimePicker dTimePicker = new DateTimePicker();

        //定义一个DataSet
        private DataSet ds;
       
        private void Form1_Load(object sender EventArgs e)
       { 
            //隐藏dTimePicker
            dTimePicker.Visible = false;
            dTimePicker.Format = DateTimePickerFormat.Custom;
            dTimePicker.CustomFormat = “yyyy-MM-dd HH:mm“;
            
            //dataGirdView1控件数据绑定
            Data_Bind();


            //定义控件dTimePicker当Text变更时委托于事件dTimePicker_TextChanged
            dTimePicker.TextChanged += new EventHandler(dTimePicker_TextChanged);

            //dataGridView1添加控件dTimePicker
            dataGridView1.Controls.Add(dTimePicker);
        }

        /**//// 
        /// dataGirdView绑定数据
        /// 

        private void Data_Bind()
       {

           ds = new DataSet();
           string constr = “server=.;database=Model;uid=sa;pwd=“;
           SqlConnection con = new SqlConnection(constr);
           SqlDataAdapter ad = new SqlDataAdapter(“Select [name] s.createdate From sysusers s“ con);
           ad.Fill(ds);
           dataGridView1.DataSource = ds.Tables[0].DefaultView;
        }


        //事件dTimePicker_TextChanged当dTimePicker当Text变更时触发
        private void dTimePicker_TextChanged(object sender EventArgs e)
       {
            //把dTimePicker的Text值赋给dataGridView1选中的单元格值
            dataGridView1.CurrentCell.Value = dTimePicker.Text;
        }
        
        private void dataGridView1_Scroll(object sender ScrollEventArgs e)
       {
            //当dataGridView1滚动条移动时dTimePicker控件隐藏
            dTimePicker.Visible = false;
        }

        private void dataGridView1_ColumnWidthChanged(object sender DataGridViewColumnEventArgs e)
       {
            //当dataGridView1的列宽改变时dTimePicker控件隐藏
            dTimePicker.Visible = false;

        }

        private void dataGridView1_CellClick(object sender DataGridViewCellEventArgs e)
       {
           //判断选中行时日期类型
           //dt = ds.Tables[0];
           Type t = ds.Tables[0].Columns[dataGridView1.CurrentCell.ColumnIndex].DataType;
           DateTime d = DateTime.Now;
           if (t == d.GetTyp

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       3693  2011-10-18 11:12  ControlDemo\CRMControlDemo\CRMControlDemo.csproj

     文件       4008  2011-10-18 11:11  ControlDemo\CRMControlDemo\Form1.cs

     文件       2279  2011-10-18 10:57  ControlDemo\CRMControlDemo\Form1.Designer.cs

     文件       5814  2011-10-18 10:57  ControlDemo\CRMControlDemo\Form1.resx

     文件        473  2011-10-18 10:47  ControlDemo\CRMControlDemo\Program.cs

     文件        899  2011-10-14 16:01  ControlDemo\CRMControlDemo\Resources\active_close.png

     文件       1200  2011-10-14 14:40  ControlDemo\CRMControlDemo\Properties\AssemblyInfo.cs

     文件       3113  2011-10-14 16:01  ControlDemo\CRMControlDemo\Properties\Resources.Designer.cs

     文件       6211  2011-10-14 16:01  ControlDemo\CRMControlDemo\Properties\Resources.resx

     文件       1097  2011-10-14 14:40  ControlDemo\CRMControlDemo\Properties\Settings.Designer.cs

     文件        249  2011-10-14 14:40  ControlDemo\CRMControlDemo\Properties\Settings.settings

     文件        359  2011-10-18 11:12  ControlDemo\CRMControlDemo\obj\CRMControlDemo.csproj.FileList.txt

     文件        946  2011-10-18 10:57  ControlDemo\CRMControlDemo\obj\Debug\CRMControlDemo.csproj.GenerateResource.Cache

     文件      24576  2011-10-18 11:12  ControlDemo\CRMControlDemo\obj\Debug\CRMControlDemo.exe

     文件        180  2011-10-18 10:57  ControlDemo\CRMControlDemo\obj\Debug\CRMControlDemo.Form1.resources

     文件        180  2011-10-18 10:40  ControlDemo\CRMControlDemo\obj\Debug\CRMControlDemo.Form2.resources

     文件      52736  2011-10-18 11:12  ControlDemo\CRMControlDemo\obj\Debug\CRMControlDemo.pdb

     文件       1366  2011-10-18 10:40  ControlDemo\CRMControlDemo\obj\Debug\CRMControlDemo.Properties.Resources.resources

     文件       4608  2011-10-14 16:01  ControlDemo\CRMControlDemo\obj\Debug\TempPE\Properties.Resources.Designer.cs.dll

     文件      24576  2011-10-18 11:12  ControlDemo\CRMControlDemo\bin\Debug\CRMControlDemo.exe

     文件      52736  2011-10-18 11:12  ControlDemo\CRMControlDemo\bin\Debug\CRMControlDemo.pdb

     文件      41984  2005-11-11 22:25  ControlDemo\CRMControlDemo\bin\Debug\CRMControlDemo.vshost.exe

     文件        931  2011-10-14 14:40  ControlDemo\CRMControlDemo.sln

     目录          0  2011-10-18 11:12  ControlDemo\CRMControlDemo\obj\Debug\TempPE

     目录          0  2011-10-18 11:12  ControlDemo\CRMControlDemo\obj\Debug\Refactor

     目录          0  2011-10-18 11:12  ControlDemo\CRMControlDemo\obj\Debug

     目录          0  2011-10-18 11:12  ControlDemo\CRMControlDemo\bin\Debug

     目录          0  2011-10-18 11:12  ControlDemo\CRMControlDemo\Resources

     目录          0  2011-10-18 11:12  ControlDemo\CRMControlDemo\Properties

     目录          0  2011-10-18 11:12  ControlDemo\CRMControlDemo\obj

............此处省略6个文件信息

评论

共有 条评论

相关资源