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

相关推荐
Hello--_--World17 分钟前
ES16:Set 集合方法增强、Promise.try、迭代器助手、JSON 模块导入 相关知识
开发语言·javascript·json
StockTV32 分钟前
韩国市场API技术对接指南,涵盖实时行情、历史数据、指数信息、公司详情等功能
java·开发语言·python·php
penngo40 分钟前
用 Claude Code 开发多人猜拳游戏:Go 语言实践
开发语言·游戏·golang
xiaoshuaishuai81 小时前
C# 实现不掉线的CRM
开发语言·c#
YuanDaima20481 小时前
大语言模型生命周期全链路解析:从架构基石到高效推理
开发语言·人工智能·python·语言模型·架构·transformer
bike兔兔1 小时前
Python实现CSV文件转Excel,一键格式转换办公小脚本
开发语言·windows·python
XMYX-01 小时前
goroutine 为什么没有返回值?(Go 并发核心设计思想)
开发语言·golang
三棱球1 小时前
Java 基础教程 Day2:从数据类型到面向对象核心概念
java·开发语言
handler011 小时前
Linux: 基本指令知识点(3)
linux·服务器·c语言·开发语言·c++·笔记
fengci.1 小时前
ctfshow其他(web408-web432)
android·开发语言·前端·学习·php