国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

c# winfrom DataGridView 動(dòng)態(tài)UI下載功能(內(nèi)含GIF圖) || 循環(huán)可變化的集合 數(shù)組 datatable 等

這篇具有很好參考價(jià)值的文章主要介紹了c# winfrom DataGridView 動(dòng)態(tài)UI下載功能(內(nèi)含GIF圖) || 循環(huán)可變化的集合 數(shù)組 datatable 等。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

Gif演示

c# winfrom DataGridView 動(dòng)態(tài)UI下載功能(內(nèi)含GIF圖) || 循環(huán)可變化的集合 數(shù)組 datatable 等

?

分解步驟

1,使用組件DataGridView

2,使用DataSource來(lái)控制表格展示的數(shù)據(jù)來(lái)源(注意:來(lái)源需要是DataTable類型)

3,需要用到異步線程。如果是不控制數(shù)據(jù)源的話,需要使用UI安全線程;(使用Control.Invoke或Control.BeginInvoke方法)

4,DataGridView的列如果設(shè)置圖片,盡量代碼設(shè)置

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>
        /// 開(kāi)啟任務(wù)的開(kāi)關(guān)(作用:禁止重復(fù)啟動(dòng)任務(wù))
        /// </summary>
        private static bool _taskSwitch = true;
        /// <summary>
        /// 任務(wù)中的小開(kāi)關(guān)(作用:如果被外部干涉,則進(jìn)行退出執(zhí)行任務(wù)內(nèi)容)
        /// </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;
                }


                //已經(jīng)執(zhí)行,請(qǐng)勿重復(fù)執(zhí)行;
                _taskSwitch = false;


                foreach (DataRow row in _table.Rows)
                {
                    row["Status"] = "2";//設(shè)置為下載中的狀態(tài)
                    uiDataGridView1.Refresh();
                }

                while (_table.Rows.Count > 0 && _taskCondition)
                {//如果列表有數(shù)據(jù)就一直循環(huán)進(jìn)行下載刪除
                    var firstRow = _table.Rows[0];
                    if (firstRow == null)
                    {//第一個(gè)元素等于NULL
                        return;
                    }

                    for (int j = 0; j <= 100; j++)//模擬進(jìn)度條
                    {
                        if (_taskCondition)
                        {//如果沒(méi)有暫停
                            await Task.Delay(10); // wait for 100 milliseconds
                            firstRow["DownloadProgress"] = j.ToString();

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

                    if (_taskCondition)
                    {
                        // 獲取當(dāng)前行的數(shù)據(jù)行                    
                        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";
                //        }
                //    }
                //    // 獲取當(dāng)前行的數(shù)據(jù)行                    
                //    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;
                //        }
                //    }
                //}

                //執(zhí)行完畢,則可以重新執(zhí)行
                _taskSwitch = true;
            }
            else
            {
                //因?yàn)榇舜螞](méi)有執(zhí)行,下次允許執(zhí)行;
                _taskSwitch = true;
                return;
            }
        }

        public void PauseAll()
        {
            SetCondition(false);

            //獲取所有已經(jīng)開(kāi)始的數(shù)據(jù)

            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
                    {
                        // 創(chuàng)建一個(gè)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)
            {
                // 獲取當(dāng)前行的數(shù)據(jù)行
                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";//修改圖標(biāo)
                            uiDataGridView1.Refresh();

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

                            _taskCondition = false;//終止執(zhí)行任務(wù)
                            currentData["Status"] = "1";//修改圖標(biāo)
                            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)
        {
            // 取消默認(rèn)的錯(cuò)誤處理行為
            e.ThrowException = false;

            // 獲取出錯(cuò)的單元格
            DataGridViewCell errorCell = uiDataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];

            // 獲取出錯(cuò)的數(shù)據(jù)
            object errorValue = uiDataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;

            // 自定義錯(cuò)誤處理邏輯
            MessageBox.Show("數(shù)據(jù)錯(cuò)誤:" + e.Exception.Message);

            // 可以將出錯(cuò)的單元格的值重置為默認(rèn)值
            errorCell.Value = errorCell.DefaultNewRowValue;
        }
    }
}

?

結(jié)語(yǔ)

上面完整代碼是.cs的代碼。大家拷貝本地使用的時(shí)候需要在UI界面進(jìn)行拖拉組件。本例子用的是winform?SunnyUI 的框架 ??蚣芪臋n在這里:文檔預(yù)覽 - Gitee.com文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-830547.html

到了這里,關(guān)于c# winfrom DataGridView 動(dòng)態(tài)UI下載功能(內(nèi)含GIF圖) || 循環(huán)可變化的集合 數(shù)組 datatable 等的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • C# 給winfrom窗體添加皮膚控件

    C# 給winfrom窗體添加皮膚控件

    如何快速給C# winform添加好看的皮膚C# Winform中窗體的美化 SkinEngine的應(yīng)用 皮膚控件換膚素材包,IrisSkin2.dll皮膚素材資源下載? 壓縮包內(nèi)一共有22種皮膚素材,使用說(shuō)明:把控件拖到你的form上,只需一行代碼,即可實(shí)現(xiàn)整個(gè)form包括其所有控件的皮膚的更換,? 總共有幾十套皮膚

    2024年02月05日
    瀏覽(25)
  • WinFrom、C# 學(xué)習(xí)記錄五 開(kāi)發(fā)一個(gè)鼠標(biāo)自動(dòng)點(diǎn)擊小軟件

    WinFrom、C# 學(xué)習(xí)記錄五 開(kāi)發(fā)一個(gè)鼠標(biāo)自動(dòng)點(diǎn)擊小軟件

    ? ? ? ? 經(jīng)常會(huì)被問(wèn)到需要點(diǎn)擊軟件的,主要都是玩游戲的盆友,但是也有其它用途的。所以簡(jiǎn)單弄了一個(gè),打算每當(dāng)有時(shí)間,有需求,就加一些小功能。 ? ? ? ? 這里主要是要記錄一下相關(guān)開(kāi)發(fā)工作,也記錄一些使用/更新的信息。 ????????【2022/08/22】版本v1.0(初始版

    2024年02月16日
    瀏覽(94)
  • C# Winfrom實(shí)例:武漢智能安檢閘機(jī)數(shù)據(jù)接收和解析

    C# Winfrom實(shí)例:武漢智能安檢閘機(jī)數(shù)據(jù)接收和解析

    項(xiàng)目介紹: 本實(shí)例主要是接收安檢閘機(jī)的數(shù)據(jù)解析并顯示到界面上,只做功能實(shí)現(xiàn),不做界面美化 硬件:閘機(jī)一個(gè)、網(wǎng)線一根、電腦主機(jī) 開(kāi)發(fā)環(huán)境:vs2017 系統(tǒng):win10 涵蓋知識(shí)點(diǎn):tcp通訊、文件寫(xiě)入、多線程,委托、類型轉(zhuǎn)換等 軟件操作流程: 點(diǎn)擊開(kāi)始監(jiān)聽(tīng)按鈕,8999要是未

    2024年02月19日
    瀏覽(17)
  • C#使用DataGridView模擬繪圖

    C#使用DataGridView模擬繪圖

    接到一個(gè)需求,繪制一個(gè)水管線的圖片,這種管線可以有12種分段方法,最后將這12種分段方法合并后在一條水管線上展示,要求: ⒈支持分段的屬性展示; ⒉要求每個(gè)分段都能清晰展示,分段數(shù)在0(沒(méi)有分段)~100之間,水管線長(zhǎng)度不定; 3、每個(gè)分段的屬性都有值,可以更

    2024年02月16日
    瀏覽(21)
  • C# winfrom實(shí)例:四路激光測(cè)距雷達(dá)數(shù)據(jù)采集和波形圖繪制

    C# winfrom實(shí)例:四路激光測(cè)距雷達(dá)數(shù)據(jù)采集和波形圖繪制

    ? ?1.所述產(chǎn)品 產(chǎn)品型號(hào): TFmini Plus 相關(guān)資料下載地址:http://www.benewake.com/download 產(chǎn)品名稱:TFmini Plus激光雷達(dá)模組 制造商 公司:北醒(北京)光子科技有限公司? 2.產(chǎn)品功能:TFmini Plus是基于TFmini的升級(jí)項(xiàng)目,它是一款小型化,單點(diǎn)測(cè)距的產(chǎn)品,基于TOF(飛行 時(shí)間)原理,

    2024年02月19日
    瀏覽(21)
  • C#——表格開(kāi)發(fā)之DataGridView控件

    C#——表格開(kāi)發(fā)之DataGridView控件

    目錄 一、概要 二、手動(dòng)填充數(shù)據(jù) 1、如何手動(dòng)填充數(shù)據(jù) 2、如何插入一行數(shù)據(jù) 3、如何修改單元格值 三、DataGridView控件綁定數(shù)據(jù)源 1、概述 2、將DataGridView綁定到BindingSource 使用DataGridView控件,您可以顯示和編輯來(lái)自許多不同類型數(shù)據(jù)源的表格數(shù)據(jù)。 DataGridView控件為顯示數(shù)據(jù)提

    2024年02月03日
    瀏覽(23)
  • C# DataGridView數(shù)據(jù)導(dǎo)出Excel文件

    C# DataGridView數(shù)據(jù)導(dǎo)出Excel文件

    博主在做項(xiàng)目的時(shí)候需要把數(shù)據(jù)庫(kù)的數(shù)據(jù)用DataGridView展示,然后把展示的數(shù)據(jù)導(dǎo)出為Excel文件,很多時(shí)候我們做項(xiàng)目都會(huì)有一個(gè)下載文件的按鈕,我們需要用微軟的的接口,Microsoft.Office.Interop.Excel,我們需要導(dǎo)入這個(gè)引用對(duì)DataGridView數(shù)據(jù)進(jìn)行處理,利用Microsoft.Office.Interop.Exc

    2024年02月04日
    瀏覽(23)
  • C# DataGridView控件的基礎(chǔ)應(yīng)用實(shí)例

    C# DataGridView控件的基礎(chǔ)應(yīng)用實(shí)例

    ??DataGridView我把他叫做網(wǎng)格數(shù)據(jù)控件 。我們?cè)陲@示表格數(shù)據(jù)的時(shí)候,經(jīng)常會(huì)用想到用它, 他就像Excel表格一樣。我們知道只要是數(shù)據(jù)表,就一定逃不掉表的增刪查改操作。 ??該篇,我在VS2019的環(huán)境下通過(guò)demo實(shí)例來(lái)實(shí)現(xiàn)DataGridView控件的一系列功能,包括添加一行數(shù)據(jù)、切

    2023年04月09日
    瀏覽(29)
  • C# Winform DataGridView 數(shù)據(jù)刷新問(wèn)題

    C# Winform DataGridView 數(shù)據(jù)刷新問(wèn)題

    目錄 一、問(wèn)題 二、創(chuàng)建項(xiàng)目 三、綁定空的數(shù)據(jù)源 四、綁定有數(shù)據(jù)的數(shù)據(jù)源 五、修改綁定的數(shù)據(jù)源 六、解決數(shù)據(jù)源刷新問(wèn)題 七、解決刷新數(shù)據(jù)界面閃爍 DataGridView 是比較常用的表格控件,在 DataGridView 中顯示數(shù)據(jù),?一般使用 dataGridView1.DataSource = 數(shù)據(jù)源,來(lái)綁定數(shù)據(jù),數(shù)據(jù)

    2024年02月12日
    瀏覽(19)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包