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

相关推荐
外收内放1 分钟前
FastAPI猜字谜实战(8):收官
开发语言·python·学习·fastapi
爱敲代码的TOM2 分钟前
10.27 易购软件 Java开发线下一面
java·开发语言
学逆向的9 分钟前
C++ 别名命名空间
开发语言·c++·网络安全
沉下心来学鲁班10 分钟前
具身智能实战:让机器鸭爬梯子——双策略交接与接触力控制的深度解读
开发语言·人工智能·python·机器人
David猪大卫26 分钟前
【C++修炼】STL容器—stack与queue
c语言·开发语言·数据结构·c++·经验分享·笔记·学习
瑶池酒剑仙27 分钟前
C++变量初始化五法完全指南:默认/拷贝/直接/列表/值初始化与窄化转换陷阱(青云之志)
c语言·开发语言·数据结构·c++·算法·visual studio
raindayinrain28 分钟前
c++对象模型--指针
开发语言·c++
AA陈超43 分钟前
CN-Language中文编程语言:第03篇-语法基础
开发语言·cn·cn中文编程语言·cn-language
熊猫钓鱼>_>44 分钟前
CMP for OpenHarmony 实战:Coil 图片加载库鸿蒙化使用心得
运维·服务器·开发语言·华为·ai编程·harmonyos·openharmony
fengci.1 小时前
CTF2刷题记录(困难部分)
android·开发语言·前端·学习·php