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

相关推荐
禹凕9 小时前
Python编程——进阶知识(多线程)
开发语言·爬虫·python
蜡笔小马9 小时前
10.Boost.Geometry R-tree 空间索引详解
开发语言·c++·算法·r-tree
IOsetting9 小时前
金山云主机添加开机路由
运维·服务器·开发语言·网络·php
唐梓航-求职中9 小时前
编程-技术-算法-leetcode-288. 单词的唯一缩写
算法·leetcode·c#
林开落L9 小时前
从零开始学习Protobuf(C++实战版)
开发语言·c++·学习·protobuffer·结构化数据序列化机制
牛奔9 小时前
Go 是如何做抢占式调度的?
开发语言·后端·golang
符哥20089 小时前
C++ 进阶知识点整理
java·开发语言·jvm
小猪咪piggy9 小时前
【Python】(4) 列表和元组
开发语言·python
難釋懷9 小时前
Lua脚本解决多条命令原子性问题
开发语言·lua
CoderCodingNo10 小时前
【GESP】C++ 二级真题解析,[2025年12月]第一题环保能量球
开发语言·c++·算法