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

相关推荐
圆滚滚肉肉3 分钟前
后端MVC(控制器与动作方法的关系)
后端·c#·asp.net·mvc
北方有星辰zz17 分钟前
数据结构:栈
java·开发语言·数据结构
我是唐青枫40 分钟前
C#.NET NLog 详解
开发语言·c#·.net
向宇it1 小时前
【unity游戏开发——网络】网络游戏通信方案——强联网游戏(Socket长连接)、 弱联网游戏(HTTP短连接)
网络·http·游戏·unity·c#·编辑器·游戏引擎
Mr_Xuhhh1 小时前
网络基础(1)
c语言·开发语言·网络·c++·qt·算法
旺旺大力包1 小时前
【JS笔记】JS 和 noodjs 的常见操作(十)
开发语言·javascript·node.js·ecmascript
背影疾风2 小时前
C++之路:类基础、构造析构、拷贝构造函数
linux·开发语言·c++
Ting-yu2 小时前
Java中Stream流的使用
java·开发语言·windows
一线码农2 小时前
MinHook 如何对 .NET 母体 CoreCLR 进行拦截
c#·.net·代码注入
【ql君】qlexcel2 小时前
Notepad++ 复制宏、编辑宏的方法
开发语言·javascript·notepad++··宏编辑·宏复制