首页 > 编程语言 >循环可变化的集合 数组 datatable 等 || c# winfrom DataGridView 动态UI下载功能

循环可变化的集合 数组 datatable 等 || c# winfrom DataGridView 动态UI下载功能

时间:2024-02-19 14:25:38浏览次数:29  
标签:Status c# winfrom mainList uiDataGridView1 using DataGridView Id row

Gif演示

 

 

 

分解步骤

1,使用组件DataGridView

2,使用DataSource来控制表格展示的数据来源(注意:来源需要是DataTable类型)

3,需要用到异步线程。如果是不控制数据源的话,需要使用UI安全线程;(使用Control.Invoke或Control.BeginInvoke方法)

4,DataGridView的列如果设置图片,尽量代码设置

5,DataTable类型也是可以使用LINQ的,参考:AsEnumerable

完整代码

using Newtonsoft.Json;
using Sunny.UI.Win32;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinApp.i18n;
using WinApp.Until;
using WinApp.ViewModel;
using static System.Net.Mime.MediaTypeNames;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System.Security.Cryptography;

namespace WinApp.View
{
    public partial class DownloadList : UserControl
    {
        /// <summary>
        /// 开启任务的开关(作用:禁止重复启动任务)
        /// </summary>
        private static bool _taskSwitch = true;
        /// <summary>
        /// 任务中的小开关(作用:如果被外部干涉,则进行退出执行任务内容)
        /// </summary>
        private static bool _taskCondition = true;

        public DataTable _table;
        List<DownloadListDto> _mainList;

        public UILabel _lbNotData;

        public DownloadList()
        {
            InitializeComponent();
            var mainTitle = string.Empty;
            mainTitle = Language.GetLang("downloadTitle1");
            mainTitle += "\r" + Language.GetLang("downloadTitle2");
            this.uiPanel1.Text = mainTitle;

            uiDataGridView1.ColumnHeadersVisible = false;
            uiDataGridView1.RowTemplate.Height = 65;
            uiDataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;

            _lbNotData = new UILabel();
            _lbNotData.Text = "No more data available";
            _lbNotData.Cursor = Cursors.Hand;
            _lbNotData.TextAlign = ContentAlignment.MiddleCenter;
            _lbNotData.Location = new Point(450, 50);
            _lbNotData.Width = 200;
            _lbNotData.Visible = false;
            this.uiPanel2.Controls.Add(_lbNotData);
        }

        private void DownloadList_Load(object sender, EventArgs e)
        {
            QueryData();
        }

        public void SetCondition(bool setValue)
        {
            _taskCondition = setValue;
        }
        public async Task DownloadAllAsync()
        {
            if (_taskSwitch)
            {
                if (_table.Rows.Count <= 0)
                {
                    UIMessageDialog.ShowMessageDialog("No more data available", UILocalize.WarningTitle, showCancelButton: false, UIStyle.Orange, false);
                    return;
                }


                //已经执行,请勿重复执行;
                _taskSwitch = false;


                foreach (DataRow row in _table.Rows)
                {
                    row["Status"] = "2";//设置为下载中的状态
                    uiDataGridView1.Refresh();
                }

                while (_table.Rows.Count > 0 && _taskCondition)
                {//如果列表有数据就一直循环进行下载删除
                    var firstRow = _table.Rows[0];
                    if (firstRow == null)
                    {//第一个元素等于NULL
                        return;
                    }

                    for (int j = 0; j <= 100; j++)//模拟进度条
                    {
                        if (_taskCondition)
                        {//如果没有暂停
                            await Task.Delay(10); // wait for 100 milliseconds
                            firstRow["DownloadProgress"] = j.ToString();

                        }
                        else
                        {//暂停
                            firstRow["Status"] = "1";
                        }
                    }

                    if (_taskCondition)
                    {
                        // 获取当前行的数据行                    
                        var _Id = (int)firstRow["Id"];
                        // 使用Linq查询匹配的行
                        var rowsToDelete = _table.AsEnumerable().FirstOrDefault(row => row.Field<int>("Id") == _Id);
                        _table.Rows.Remove(rowsToDelete);
                    }
                }

                //foreach (DataRow row in _table.Rows)
                //{
                //    row["Status"] = "2";

                //    for (int j = 0; j <= 100; j++)
                //    {
                //        if (_taskCondition)
                //        {
                //            await Task.Delay(10); // wait for 100 milliseconds
                //            row["DownloadProgress"] = j.ToString();
                //        }
                //        else
                //        {
                //            row["Status"] = "1";
                //        }
                //    }
                //    // 获取当前行的数据行                    
                //    var _Id = (int)row["Id"];
                //    // 使用Linq查询匹配的行
                //    var rowsToDelete = _table.AsEnumerable().FirstOrDefault(row => row.Field<int>("Id") == _Id);
                //    _table.Rows.Remove(rowsToDelete);
                //}


                //foreach (var item in _mainList)
                //{
                //    item.Status = 2;
                //    uiDataGridView1.Refresh();

                //    for (int i = 0; i < 100; i++)
                //    {
                //        if (_taskCondition)
                //        {
                //            await Task.Delay(100); // wait for 100 milliseconds
                //            item.DownloadProgress = i.ToString();
                //            uiDataGridView1.Refresh();
                //        }
                //        else
                //        {
                //            item.Status = 1;
                //            return;
                //        }
                //    }
                //}

                //执行完毕,则可以重新执行
                _taskSwitch = true;
            }
            else
            {
                //因为此次没有执行,下次允许执行;
                _taskSwitch = true;
                return;
            }
        }

        public void PauseAll()
        {
            SetCondition(false);

            //获取所有已经开始的数据

            var pauseList = _table.AsEnumerable().Where(row => row.Field<int>("Status") == 2);
            foreach (DataRow item in pauseList)
            {
                item["Status"] = "1";
                uiDataGridView1.Refresh();
            }

        }

        public void DeleteAll()
        {            
            SetCondition(false);
         
            // 清除所有行
            _table.Clear();
            uiDataGridView1.Refresh();
            this.uiDataGridView1.Refresh();
        }

        public void QueryData()
        {

            LoadingHelper.ShowLoadingScreen();

            _mainList = new List<DownloadListDto>();
            _mainList.Add(new DownloadListDto()
            {
                Id = 1,
                Title = "A1" + Environment.NewLine + "B1",
                Status = 1,
                DownloadProgress = "0"
            });
            _mainList.Add(new DownloadListDto()
            {
                Id = 2,
                Title = "A2" + Environment.NewLine + "B2",
                Status = 1,
                DownloadProgress = "0"
            });
            _mainList.Add(new DownloadListDto()
            {
                Id = 3,
                Title = "A3" + Environment.NewLine + "B3",
                Status = 1,
                DownloadProgress = "0"
            });
            _mainList.Add(new DownloadListDto()
            {
                Id = 4,
                Title = "A4" + Environment.NewLine + "B4",
                Status = 1,
                DownloadProgress = "0"
            });
            _mainList.Add(new DownloadListDto()
            {
                Id = 5,
                Title = "A5" + Environment.NewLine + "B5",
                Status = 1,
                DownloadProgress = "0"
            });


            _table = _mainList.ToDataTable();
            this.uiDataGridView1.DataSource = _table;

            LoadingHelper.CloseForm();
            uiDataGridView1.ClearSelection();
        }

        private void uiDataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            DataGridViewRow row = uiDataGridView1.Rows[e.RowIndex];

            if (uiDataGridView1.Columns[e.ColumnIndex].Name == "clTitle")
            {
                if (row.Cells["clStatus"].Value is int)
                {
                    var intStatus = (int)row.Cells["clStatus"].Value;
                    if (intStatus == 1)
                    {
                        row.Cells["clOpDown"].Value = FileHelper.loadImageFromLocalPath(@"FileFolder/Icon/downLoad.png");
                        row.Cells["clOpDelete"].Value = FileHelper.loadImageFromLocalPath(@"FileFolder/Icon/delete1.png");
                    }
                    else if (intStatus == 2)
                    {
                        row.Cells["clOpDown"].Value = FileHelper.loadImageFromLocalPath(@"FileFolder/Icon/pause.png");
                        row.Cells["clOpDelete"].Value = FileHelper.loadImageFromLocalPath(@"FileFolder/Icon/delete1.png");
                        //row.Cells["clOpDelete"].Value = null;
                    }
                    else
                    {
                        // 创建一个1x1像素的透明图像
                        Bitmap transparentImage = new Bitmap(1, 1);
                        transparentImage.SetPixel(0, 0, Color.Transparent);
                        row.Cells["clOpDown"].Value = transparentImage;
                        row.Cells["clOpDelete"].Value = transparentImage;
                    }
                }

            }
        }

        private void uiDataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //uiDataGridView1.ClearSelection();
        }

        private async void uiDataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (uiDataGridView1.Columns[e.ColumnIndex] is DataGridViewImageColumn && e.RowIndex >= 0)
            {
                // 获取当前行的数据行
                var currentRow = uiDataGridView1.Rows[e.RowIndex];
                var _Id = (int)currentRow.Cells["clId"].Value;
                if (uiDataGridView1.Columns[e.ColumnIndex].Name == "clOpDown")
                {

                    //var currentData = _mainList.Find(x => x.Id == _Id);
                    var currentData = _table.AsEnumerable().FirstOrDefault(x => x.Field<int>("Id") == _Id);
                    if (currentData != null)
                    {
                        if (currentData["Status"].ToString() == "1")
                        {//1代表 未下载

                            currentData["Status"] = "2";//修改图标
                            uiDataGridView1.Refresh();

                        }
                        else
                        {//2代表 正在下载

                            _taskCondition = false;//终止执行任务
                            currentData["Status"] = "1";//修改图标
                            uiDataGridView1.Refresh();

                        }
                        //currentData.Status = 1;
                        //_taskCondition = false;
                        //uiDataGridView1.Refresh();
                    }
                }

                if (uiDataGridView1.Columns[e.ColumnIndex].Name == "clOpDelete")
                {

                    // 使用Linq查询匹配的行
                    var rowsToDelete = _table.AsEnumerable().FirstOrDefault(row => row.Field<int>("Id") == _Id);
                    _table.Rows.Remove(rowsToDelete);
                }
            }
        }
        public void DeleteMainData(int Id)
        {
            var currentData = _mainList.Find(x => x.Id == Id);
            if (currentData != null)
            {
                _mainList.Remove(currentData);
                uiDataGridView1.DataSource = _mainList;
                uiDataGridView1.Refresh();
            }
        }

        private void uiDataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            uiDataGridView1.Visible = true;
            _lbNotData.Visible = false;
            DataGridView dataGridView = (DataGridView)sender;
            if (dataGridView.Rows.Count == 0)
            {
                uiDataGridView1.Dock = DockStyle.None;
                uiDataGridView1.Visible = false;

                _lbNotData.Visible = true;
            }
        }

        private void uiDataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            // 取消默认的错误处理行为
            e.ThrowException = false;

            // 获取出错的单元格
            DataGridViewCell errorCell = uiDataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];

            // 获取出错的数据
            object errorValue = uiDataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;

            // 自定义错误处理逻辑
            MessageBox.Show("数据错误:" + e.Exception.Message);

            // 可以将出错的单元格的值重置为默认值
            errorCell.Value = errorCell.DefaultNewRowValue;
        }
    }
}

 

结语

上面完整代码是.cs的代码。大家拷贝本地使用的时候需要在UI界面进行拖拉组件。本例子用的是winform SunnyUI 的框架 。框架文档在这里:文档预览 - Gitee.com

标签:Status,c#,winfrom,mainList,uiDataGridView1,using,DataGridView,Id,row
From: https://www.cnblogs.com/BFMC/p/18020935

相关文章

  • 前端知识回顾概览--JavaScript 高级
     掌握JS语言,针对闭包、原型链等有深入理解对typescript静态化工具熟练掌握精通常见设计模式了解函数式编程 1.this指针/闭包/作用域this指针详解闭包的概念及应用场景作用域(全局作用域/函数作用域)默认绑定、显式绑定、隐式绑定存储空间、执行上下文2.面向对象编......
  • 前端知识回顾概览--React 17+18
    react目前最火的前端框架之一状态管理、路由等一定要重点掌握熟悉常见API,并且有使用经验1.react.js基础react.js简介jsx模版语法及babel编译配置事件/条件渲染/列表渲染等基础用法react.js组件化及生命周期refs及ReactAPI详解create-react-appcli的......
  • python-mock接口测试
    什么是mock?测试桩,模拟被测对象的返回,用于测试通常意义的mock指的就是mockserver,模拟服务端返回的接口数据,用于前端开发,第三方接口联调为什么要mock?1.解决依赖问题:当我们测试一个接口或者功能模块的时候,如果这个接口或者功能模块依赖其他接口或其他模块,那么如果所依赖的......
  • centos7下安装的1.8jdk包
    1.安装目录选择你jdk需要放置的安装目录,我这里选择的是/usr/local/2.解压把在jdk官网下载的jdk包放入指定的文件夹下并解压,注意不同linux发行版下的jdk包是不一样的,不同系统下的包是不能使用的,比如我之前在centos7下安装的jdk包,在ubuntu18.04下就用不了。进入jdk目录下,解压命......
  • 微软 Office 2021 专业增强版,安装完自动激活
    123盘下载地址  MicrosoftOffice2021VL官方版下载丨最新版下载丨绿色版下载丨APP下载-123云盘(123pan.com)安装前先关闭windows系统自带的 病毒  微软办公软件套件MicrosoftOfficeLTSC2021专业增强版2024年01月批量许可版更新推送!Office2021正式版和Windows11......
  • 使用Echarts绘图
    案例1参考代码如下<!-- 此示例下载自https://echarts.apache.org/examples/zh/editor.html?c=bar-histogram--><!DOCTYPEhtml><htmllang="en"style="height:100%"><head><metacharset="utf-8"></head&g......
  • H3C OSPF+NAT+DHCP+PPP综合实验
    H3COSPF+NAT+DHCP+PPP综合实验实验拓扑​​实验需求1.按照图示配置IP地址2.SW1和SW2之间的直连链路配置链路聚合3.公司内部业务网段为Vlan10和Vlan20;Vlan10是市场部,Vlan20是技术部,要求对Vlan进行命名以便识别;PC1属于Vlan10,PC2属于Vlan20,Vlan30用于SW1和SW2建......
  • Qt 哈希加密 QCryptographicHash
    QCryptographicHash类提供了生成密码散列的方法。该类可以用于生成二进制或文本数据的加密散列值。目前支持MD4、MD5、SHA-1、SHA-224、SHA-256、SHA-384和SHA-512。共有类型枚举QCryptographicHash::Algorithm:公共函数voidaddData(constchar*data,intlength)......
  • docker打包镜像
    Docker容器镜像打成tar包前言本文记录docker保存镜像、打包tar、加载tar镜像。简述需求:在现在容器镜像上保存镜像进行打包,在另一台服务上使用;或现有的容器安装了一些库,配置了开发环境,需要保存下载,下次加载后直接使用一、docker保存镜像1、首先查看下现有要打tar包的容器(目的......
  • 通过CMD命令行登陆交换机
    前提条件:Windows系统、交换机已经配置telnet服务、本机IP被允许登陆按下键盘win+R,输入cmd开启命令行,输入telnetIP,提示'telnet'不是内部或外部命令,也不是可运行的程序或批处理文件。此时输入ipconfig,发现其他网络指令正常使用。此时可断定不是cmd出现软件问题,以防万一,退出界面......