C# Winform Datagridview查询项目实例

在项目中,我们经常要遇到查询和展示内容,常用的做法是通过文本框,时间控件,按键和datagridview查询和展示内容。下面是一个常见的综合实例,并支持Excel(csv)导入导出,表格列动态调整的功能。

实例代码链接:https://download.csdn.net/download/lvxingzhe3/89436315

Excel(csv)导入到Datagridview:

cs 复制代码
        /// <summary>
        /// 将csv文件数据导入datagridview
        /// </summary>
        /// <param name="csvPath"></param>
        /// <returns></returns>
        public static void ImportCSV(DataGridView dgv)
        {
            string filePath;
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "CSV files (*.csv)|*.csv";
            openFileDialog.FilterIndex = 0;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                filePath = openFileDialog.FileName;
            }
            else
            {
                return;
            }
            DataTable dt = new DataTable();
            using (StreamReader sr = new StreamReader(filePath))
            {
                string[] headers = sr.ReadLine().Split(',');
                string headerValue = null;
                foreach (string header in headers)
                {
                    headerValue = header.Replace("\"", "");
                    dt.Columns.Add(headerValue);
                }
                while (!sr.EndOfStream)
                {
                    string[] rows = sr.ReadLine().Split(',');
                    DataRow dr = dt.NewRow();
                    for (int i = 0; i < headers.Length; i++)
                    {
                        dr[i] = rows[i].Replace("\"", "");
                    }
                    dt.Rows.Add(dr);
                }
            }
            dgv.DataSource = dt;
        }

Datagridview导出到excel(csv)

cs 复制代码
        /// <summary>
        /// DateGridView导出到csv格式的Excel,通用  
        /// </summary>
        /// <param name="dgv"></param>
        public static void ExportToCSV(DataGridView dgv)
        {
            string filePath;
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "CSV files (*.csv)|*.csv";
            saveFileDialog.FilterIndex = 0;
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                filePath = saveFileDialog.FileName;
            }
            else
            {
                return;
            }
            using (StreamWriter sw = new StreamWriter(filePath))
            {
                // 写入列标题
                string headers = string.Join(",", dgv.Columns.Cast<DataGridViewColumn>().Select(column => "\"" + column.HeaderText + "\"").ToArray());
                sw.WriteLine(headers);

                // 写入数据行
                foreach (DataGridViewRow row in dgv.Rows)
                {
                    string line = string.Join(",", row.Cells.Cast<DataGridViewCell>().Select(cell => "\"" + cell.Value?.ToString().Replace("\"", "\"\"") + "\"").ToArray());
                    sw.WriteLine(line);
                }
            }
        }

实例代码链接:https://download.csdn.net/download/lvxingzhe3/89436315

相关推荐
唐青枫6 分钟前
C#.NET Consul + Steeltoe 深入解析:服务注册发现、健康检查与微服务接入
c#·.net
酉鬼女又兒16 分钟前
零基础快速入门前端蓝桥杯Web备考:BOM与定时器核心知识点详解(可用于备赛蓝桥杯Web应用开发)
开发语言·前端·javascript·职场和发展·蓝桥杯
望眼欲穿的程序猿17 分钟前
MacOS自定义安装Rust
开发语言·macos·rust
wjs202421 分钟前
CSS 动画:深入浅出的探索与实践
开发语言
wjs202426 分钟前
二分搜索树
开发语言
沐知全栈开发31 分钟前
Memcached delete 命令详解
开发语言
lly20240632 分钟前
Lua 基本语法
开发语言
DowneyJoy32 分钟前
【Unity3D补充知识点】常用数据结构分析-集合(List<T>)
数据结构·unity·c#·list
格林威41 分钟前
Baumer相机铝型材表面划伤长度测量:实现损伤量化评估的 5 个关键技术,附 OpenCV+Halcon 实战代码!
开发语言·人工智能·数码相机·opencv·计算机视觉·c#·工业相机
AI科技星1 小时前
基于v≡c公设的理论优化方案
c语言·开发语言·算法·机器学习·数据挖掘