CDataGrid

VC DataGrid控件使用总结 MS CDataGrid控件用法详解 本文示例源代码或素材下载 。DataGrid控件是VC方便地用来显示数据的一个极好的网格控件,我不否认网上还有期它不少优秀的网格控件,但我总喜欢使用微软自已的东西 。除非它满足不了我的需要。不是我太依赖Microsoft,你想想,如果你最常用的开发工具是VC,VC是微软的主打开发工具,它与Windows系统的兼容性极好,连它的控件也不例外,你为何不 阅读详情
 enum DataMask { Num, Float, Date, NvChar, Bit };
    [ToolboxBitmap(typeof(System.Windows.Forms.DataGridView))]
    class CDataGrid : DataGridView
    {
        public DataMask val_type;
        public object val_default;
        public bool val_null;
        public CDataGrid()
        {
            val_type = new DataMask();
            val_default = null;
            val_null = true;
            this.CellValidated += new DataGridViewCellEventHandler(CDataGrid_CellValidated);
            this.RowsAdded += new DataGridViewRowsAddedEventHandler(CDataGrid_RowsAdded);
            this.RowsRemoved += new DataGridViewRowsRemovedEventHandler(CDataGrid_RowsRemoved);
            this.CellEndEdit += new DataGridViewCellEventHandler(CDataGrid_CellEndEdit);
        }//构造函数
        public string cValue
        {
            get
            {
                return "'" + this.CurrentCell.Value.ToString() + "'";
            }
        }
        public void PaintEachOtherGridItem(Color cor)
        {
            if (this.Rows.Count != 0)
            {
                for (int i = 0; i < this.Rows.Count; )
                {
                    this.Rows[i].DefaultCellStyle.BackColor = cor;
                    i += 2;
                }
            }
        }//隔行着色
        public void FillTableRow(System.Data.DataTable table)
        {
            this.Visible = false;
            this.Rows.Clear();
            for (int i = 0; i < table.Rows.Count; i++)      //显示到表格
            {
                this.Rows.Add();
                for (int j = 0; j < table.Columns.Count; j++)
                {
                    if (table.Rows[i][j].ToString().Length>0)
                        this.Rows[i].Cells[j + 1].Value = table.Rows[i][j].ToString();
                }
            }
            this.Visible = true;
        }//填充数据,行
        public void Fill(System.Data.DataTable table)
        {
            this.Visible = false;
            this.Rows.Clear();
            this.Columns.Clear();
                       
            //this.ColumnCount = table.Columns.Count + 1;
            this.Columns.Add("sn", "sn");
            this.Columns[0].Visible = false;
            
            foreach (DataColumn col in table.Columns)
                this.Columns.Add(col.ColumnName, col.ColumnName);
            
            FillTableRow(table);
            
            this.Visible = true;
        }
        public void AutosizeColumnsWidth()
        {
            this.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
            foreach (DataGridViewColumn col in this.Columns)
                col.Width = 12 * col.Name.Length + 32;
        }//自动调整列宽
        public void Export()
        {
            
            #region range
            int count = this.Columns.Count;
            if (count > 256||count==0) return;//超出Excel的最大行数,或者为零
            string cell = string.Empty;
            int first = count / 26;
            int second = count - (first * 26);
            if (first > 1)
            {
                if (second > 0)
                    cell = ((char)(64 + first)).ToString() + ((char)(64 + second)).ToString();
                else if (second == 0)
                    cell = ((char)(64 + first - 1)).ToString() + ((char)(90)).ToString();
                else
                {
                    MessageBox.Show("Interial Error", "Error Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            else if (first == 1)
            {
                if (second > 0)
                    cell = ((char)(64 + first)).ToString() + ((char)(64 + second)).ToString();
                else if (second == 0)
                    cell = ((char)(90)).ToString();
                else
                {
                    MessageBox.Show("Interial Error", "Error Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            else if (first == 0)
            {
                cell = ((char)(64 + second)).ToString();
            }
            else
            {
                MessageBox.Show("Interial Error", "Error Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            cell += "1";
            #endregion
            #region Start Excel
            Excel.Application app = new Excel.ApplicationClass();
            if (app == null)
            {
                MessageBox.Show("Excel无法启动,请确认本机已经正确安装Microsoft Excel。","启动错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
            }
            app.Visible = false;
            Excel.Workbooks wbs = app.Workbooks;
            Excel.Workbook wb = wbs.Add(Missing.Value);
            Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];
            #endregion
            Excel.Range r = ws.get_Range("A1", cell); //MessageBox.Show(cell);
            //r.Select();
            r.NumberFormatLocal = "@";                    
            #region header
            object[] objHeader = new object[this.Columns.Count];
            foreach (DataGridViewColumn c in this.Columns)
                objHeader[c.Index] = c.Name;
            #endregion
            #region write data
            r.Value2 = objHeader;
            if (this.Rows.Count > 1)
            {
                r = ws.get_Range("A2", Missing.Value);
                object[,] objData = new Object[this.Rows.Count, this.Columns.Count];
                for (int i = 0; i < this.Rows.Count - 1; i++)
                {
                    foreach (DataGridViewColumn col in this.Columns)
                    {
                        if (this.Rows[i].Cells[col.Index].Value != null)
                            objData[i, col.Index] = this.Rows[i].Cells[col.Index].Value;
                    }
                }
                
                r = r.get_Resize(this.Rows.Count, this.Columns.Count);
                r.Select(); //选中
                r.NumberFormatLocal = "@";  //文本格式
                r.Value2 = objData;
                r.EntireColumn.AutoFit();   //自动调整列宽
                r.HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;    //居中
            }
            #endregion
            app.Visible = true;
             
        }//导出到Microsoft Excel
        #region ///验证
        private void CDataGrid_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == this.Rows.Count - 1) return;
            if (CurrentCell.ReadOnly) return;//单元格只读,则返回
            #region default value
            if (CurrentCell.Value == null)//单元格为null
            {
                if (val_null) //允许为空
                {                    
                    CurrentCell.ErrorText =string.Empty;
                }
                else//不允许为空
                {
                    if (val_default != null)//默认值存在
                    {
                        CurrentCell.Value = val_default;
                        CurrentCell.ErrorText = string.Empty;
                    }//恢复为默认值
                    else    //默认值不存在
                        CurrentCell.ErrorText = "内容尚未填充!";
                }
                return;//返回
            }
            #endregion
            #region 验证
            switch (val_type)
            {
                case DataMask.Num:
                    {
                        if (!Regex.IsMatch(CurrentCell.Value.ToString(), @"^([0-9]*)$"))
                            CurrentCell.ErrorText = "错误的数字格式";
                        else CurrentCell.ErrorText = String.Empty;
                        break;
                    }
                case DataMask.Float:
                    {
                        if (!Regex.IsMatch(CurrentCell.Value.ToString().ToString(), @"^(-?"d+)("."d+)?$"))
                            CurrentCell.ErrorText = "错误的浮点数据格式";
                        else CurrentCell.ErrorText = String.Empty;
                        break;
                    }
                case DataMask.Date:
                    {
                        if (!Regex.IsMatch(CurrentCell.Value.ToString().ToString(), @"^(?<month>(0?[1-9])|1[0-2])(-|/)(?<day>(0?[1-9])|((1|2)[0-9])|30|31)(-|/)(?<year>"d{4})"))
                            CurrentCell.ErrorText = "数据格式: mm-dd-yyyy";
                        else CurrentCell.ErrorText = String.Empty;
                        break;
                    }
                case DataMask.Bit:
                    {
                        if (CurrentCell.Value.ToString() != "1" && CurrentCell.Value.ToString() != "0")
                            this.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "错误的Bool格式";
                        else this.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = String.Empty;
                        break;
                    }
                case DataMask.NvChar:
                    {
                        CurrentCell.ErrorText = String.Empty;
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
            #endregion

        }
        private void CDataGrid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            this.Rows[e.RowIndex].ErrorText = String.Empty;
        } // Clear the row error in case the user presses ESC.
        #endregion
        #region ///添加, 删除行
        private void CDataGrid_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            foreach (DataGridViewRow row in this.Rows)
            {
                row.Cells[0].Value = row.Index + 1;
            }
        }
        private void CDataGrid_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            foreach (DataGridViewRow row in this.Rows)
            {
                row.Cells[0].Value = row.Index + 1;
            }
        }
        #endregion
    } 

C++自定义的datagrid控件 用c++ 开发的datagrid控件,适用于想学习控件开的完美参考例子 立即下载

相关推荐

使用DataGrid控件进行数据查看,CDataGrid

使用DataGrid控件进行数据查看,CDataGrid

DataGridView详解(详细)

DataGridView控件提供一种强大而灵活的以表格形式显示数据的方式。可以使用DataGridView控件来显示少量数据的只读视图,也可以对其进行缩放以显示特大数据集的可编辑视图。使用DataGridView控件,可以显示和编辑来自多种不同类型的数据源的表格数据。将数据绑定到DataGridView控件非常简单和直观,在大多数情况下,只需设置DataSource属性即可。DataGridView控件具有极高的可配置性和可扩展性,它提供了大量的属性、方法和事件,可以用来对该控件的外观和行为进行自定义。

这个人好有趣....hh 1万+

datagrid

NULL 博文链接:https://baobeituping.iteye.com/blog/781987

DataGrid技巧大集合

引用自:http://www.cnblogs.com/iCeSnaker/archive/2004/07/31/29017.aspx DataGrid的正反双向排序 http://dev.youkuaiyun.com/develop/article/26/26817.shtm DataGrid删除确认及Item颜色交替 http://dev.youkuaiyun.com/develop/article/26/26768.

小人物的专栏 *^_^* 1963

海光(hygon)CPU ESXI vSphere 紫屏问题修复记录

海光服务器ESXI 紫屏问题修复

edison202302的博客 3557

Fortigate 在AWS中配置HA

方案1: 1、将两台服务器配置在相同可用区中,使用飞塔原生HA功能,可以实现配置同步功能。 2、配置SSLVPN,在主服务器不可用时,可以实现自动切换到备用服务器继续提供VPN服务。 当前问题: 1、主挂掉后可以自动切换到备,但是原主服务器修复后不会自动切换到主状态。 2、如下图所示切换中丢包明显,需要客户端开启“保持连接”功能。 配置过程如下: 1、整体架构 2、创建VPC和子网,如下图所示...

子苏蓝莓的博客 892

VS2010 Win7 64位 C++ MFC DataGrid绑定实例

纠结了几天的DataGrid绑定问题今天终于能跑出来了。我下载别人的代码,都可以跑, 因为我是用VS2010 Win7 64位 的环境来学习 C++ MFC的,网上找的资料大都是VC6.0正面的, 我下载的DataGrid例子都可以跑出来,一到我自己写就出错了。 后来我按照如下方法就可以了。文章最后有实例下载 1:新建一个MFC应用程序:DataGridDemo 2:选择基于对话

fddqfddq的专栏 1万+

别再被坑了!手把手教你从官网下载免费正版Notepad++(附最新网盘链接)

本文详细指导如何从Notepad++官网安全下载正版软件,避免山寨版本带来的安全风险。通过识别官网特征、推荐官方下载渠道、揭露山寨网站骗局及验证正版功能,帮助用户高效获取纯净的Notepad++代码编辑器,并分享实用插件和配置优化技巧。

weixin_42527654的博客 564

C++: DataGridView::DataSource

#pragma once #include "Form2.h" namespace cdemo { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace Syst...

weixin_34050389的博客 321

PhotovoltaicPanelCleanlinessDetector:基于YOLOv8的光伏板清洁状态监测系统

data.yamlyaml复制编辑names:0: clean1: dust3: leaves4: shadow✅ 高精度污染识别✅ 具备实时检测与UI展示✅ 可本地化部署应用未来方向增加污染程度分级(轻度、中度、严重)加入时间序列分析(污染变化趋势预测)无人机集成巡检模块。

YOLO 276

069、PCIE属性字段:缓存、顺序与安全

上个月调试一块自研的PCIE采集卡,遇到了一个诡异的问题:主机通过DMA从设备读取图像数据,大部分帧正常,但偶尔会出现局部数据错位,像是缓存没刷干净。用逻辑分析仪抓链路层报文,发现TLP(事务层包)本身完好,但内存中的数据就是不对。排查三天后,终于锁定在设备端TLP的**属性字段(Attribute Field)**配置上——那几位看似不起眼的缓存、顺序控制位,差点让项目延期。今天我们就拆解PCIE事务头中的这个关键区域,看看它如何影响系统性能与数据安全。

z534437748的博客 50

一个完整的C++程序SpreadSheet - 1) 类的声明和定义

1. SpreadsheetCell.h #pragma once #include <string> class SpreadsheetCell { public: void setValue(double inValue); double getValue() const; void setString(c...

weixin_33850890的博客 246

PyTorch 最新安装教程(2021-07-27)

PyTorch 最新安装教程(2021-07-27)前言1. 安装 Anaconda2. 检查显卡,更新驱动3. 创建PyTorch环境4. 配置清华TUNA镜像源5. 安装 PyTorch6. 测试 前言 万事开头难! 这句话又一次被我验证。 记得前不久刚陷入Tensorflow2.0的安装困境,这一次又被PyTorch 搞哭辽。 孩子太难了o(╥﹏╥)o,不过还好最终成功安装,感谢全网资源,感谢大佬们的博客!被我一次一次试了出来。 1. 安装 Anaconda Anaconda 是一个用于科学计算的 P

全网粉丝30W+,优快云、稀土掘金人工智能领域签约作者,人工智能、大模型、Python 爬虫,商务合作,请私信沟通。 23万+

VC++2005中用到的DataGrid控件的CDataGrid类成员

成员就这么多,至于每个的用法,慢慢补充吧 CDataGrid::CaptureImage CDataGrid::ClearFields CDataGrid::ClearSelCols CDataGrid::ColContaining CDataGrid::Create CDataGrid::Create CDataGrid::DECLARE_DYNCREATE CDat

K.Mong的学习笔记 2095

IIC软件协议及硬件知识汇总

IIC软件协议及硬件相关知识,真的是非常齐全了!

记得诚 1万+

日常工作记录---在虚拟机中进行slam建图

   今天是10月24号,首先祝各位程序员们节日快乐!!!    最近一周由于实习的原因,对ROS的探索学习,停滞了一段时间,所以今天也就没什么大的进展,本篇文章仅仅记录一下,我在虚拟机中进行slam建图可行性验证的过程,本篇文章同样使用古月学院《如何在Gazebo中实现阿克曼转向车的仿真》的课程资料来进行验证    下面各条之间并没有严格的顺序关系,每一条都是独立的,都是我在验证过程中的经验或者记录:    1、在运行roslaunch bringup racecar_gazebo_rviz.lau

慕羽★的博客 4828

解决]VC:CDataGrid控件响应鼠标双击事件

CDataGrid部分函数功能说明:GetRowHeight()得到行高GetRow得到光标所在行的行号(看来,上面的计算是多余的了,直接调用它就可以得到鼠标点击的行号了)GetCol得到光标所在列的列号GetVisibleRows得到当前窗口显示的行数GetFirstRow得到当前窗口第一行的绝对行号.......找到了,就是它!以下是实现代码: try {  i

shijilong_long的专栏 820

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

cdatagrid操作优化文档:提升DataGridView性能的解决方案

cdatagrid操作优化文档:提升DataGridView性能的解决方案 去发现同类优质开源项目:https://gitcode.com/ 在开发过程中,如何高效地操作 c# 中的 DataGridView 控件是许多开发者面临的挑战。本文将介绍一款开源项目——cdatagrid操作优化文档,帮助开发者解决DataGridView操作中的性能问题。 项目介绍 cdatagrid操作优化文档是一款...

gitblog_06747的博客 821

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

CDataGrid、CAdodc与C_RecordSet的使用细节

(百度和网页http://hi.baidu.com/guxue365/blog/item/34af638b08d1c119c9fc7aef.html的作者无关,不对其内容负责。百度快照谨为网络故障时之索引,不代表被搜索网站的即时页面。) CDataGrid、CAdodc与C_RecordSet的使用细节 2010-01-24 09:24 1.查询指定USE

autumn20080101的专栏 1507

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

重载CDataGrid的函数以响应消息

Ctrl+w在Wizard里面于class name设置datagrid所在的对话框,在Object IDs选择IDC_DATAGRID1,在messages里面选择需要响应的消息,Wizard会自动添加如下代码:BEGIN_EVENTSINK_MAP(CGridTestDlg, CDialog)    //{{AFX_EVENTSINK_MAP(CGridTestDlg)    ON_E

觀滄海 825

cdatagrid操作优化文档

c#datagrid操作优化文档 去发现同类优质开源项目:https://gitcode.com/ 本文档详细介绍了在使用c#进行datagridView操作时,如何解决datagridView直接绑定DataSource加载数据较慢的问题。经过作者在网络上查找大量资料并实践,总结出了一套有效的解决方案,旨在帮助开发者提高程序的性能和用户体验。 去发现同类优质开源项目:https://gitco...

gitblog_06762的博客 390

学习笔记:AOD 数据库访问杂锦

1.    在vc++里面添加ADO Data控件的变量(m_adodc)后可以直接用m_adodc.SetRecordSource("数据库访问命令")来设置ADO Data控件的数据源。 但在vs2008后,SetRecordSource()改为了put_RecordSource()。 2. 同上,CDataGrid::GetColumns()改为了CDataGrid::get_C

fantasypan的专栏 1539

vc++数据库编程中遇到的问题

error C2027: use of undefined type 'C_Recordset'        f:/program files/microsoft visual studio/myprojects/hrsys/adodc.h(14) : see declaration of 'C_Recordset'F:/Program Files/Microsoft Visual Studio/MyProjects/HrSys/EmpManDlg.cpp(126) : error C2228: left

v_love的专栏 1104

酒店点餐系统开发详解(六)

酒店点餐系统开发详解(六)——疑难解决在本系统的详细设计过程中遇到了很多的麻烦,经过分析、思考和网上搜索等方式将所有问题均给一一化解了,现与大家分享以共勉。1、CDataGrid的使用之所以使用CDataGrid完全是为了偷懒,不过它却让我差点崩溃,因为它并没有想象中的那么方便,关键是网上或书上关于它的资料都少得可怜。幸运的是本系统对其要求不高,因而被我巧妙地化解了。我看中

flytreeleft的专栏 1823

VC++或MFC中各控件用法举例:章节一

1 CImagelist 和CTreeCtrl举例:    CImagelist m_treeImageList;    CTreeCtrl   m_tree      m_treeImageList.Create(16,16,FALSE,1,0);  //创建Cimagelist控件        HICON hIcon = ::LoadIcon(AfxGetResourceHa

zhangyulin54321的专栏 1786

VS2003里添加控件

VS2003里添加CDataGrid控件   2009-06-15 12:33:19|  分类: VC|字号 订阅 右击工具箱中的“对话框编辑器”,选择“添加-移除项”,在弹出的“自定义工具箱”中选择“COM组件”,添加“Microsoft ADO Data Control 6.0(SP4) (OLEDB)”和“Microsoft DataGrid Cont

cddchina的专栏 1510
上一篇: 数值格式化参数收集
下一篇: ListView 到 Excel
WavesLine
博客等级 码龄12年 18粉丝 58原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值