- [DEVEXPRESS] Hỗ trợ tìm kiếm highlight không dấu và không khoảng cách trên Gridview Filter
- [C#] Chia sẻ source code phần mềm Image Downloader tải hàng loạt hình ảnh từ danh sách link url
- [C#] Chụp hình và quay video từ camera trên winform
- [C#] Chia sẽ full source code tách file Pdf thành nhiều file với các tùy chọn
- Giới thiệu về Stock Tracker Widget - Công cụ theo dõi cổ phiếu và cảnh báo giá tăng giảm bằng C# và WPF
- [VB.NET] Chia sẻ công cụ nhập số tiền tự động định dạng tiền tệ Việt Nam
- [VB.NET] Hướng dẫn fill dữ liệu từ winform vào Microsoft word
- [VB.NET] Hướng dẫn chọn nhiều dòng trên Datagridview
- Hướng Dẫn Đăng Nhập Nhiều Tài Khoản Zalo Trên Máy Tính Cực Kỳ Đơn Giản
- [C#] Chia sẻ source code phần mềm đếm số trang tập tin file PDF
- [C#] Cách Sử Dụng DeviceId trong C# Để Tạo Khóa Cho Ứng Dụng
- [SQLSERVER] Loại bỏ Restricted User trên database MSSQL
- [C#] Hướng dẫn tạo mã QRcode Style trên winform
- [C#] Hướng dẫn sử dụng temp mail service api trên winform
- [C#] Hướng dẫn tạo mã thanh toán VietQR Pay không sử dụng API trên winform
- [C#] Hướng Dẫn Tạo Windows Service Đơn Giản Bằng Topshelf
- [C#] Chia sẻ source code đọc dữ liệu từ Google Sheet trên winform
- [C#] Chia sẻ source code tạo mã QR MOMO đa năng Winform
- [C#] Chia sẻ source code phần mềm lên lịch tự động chạy ứng dụng Scheduler Task Winform
- [C#] Hướng dẫn download file từ Minio Server Winform
[DEVEXPRESS] Tích hợp Pause/Resume vào download Progress Cell Gridview
Xin chào các bạn, hôm nay mình tiếp tục hướng dẫn các bạn download file tích hợp thêm chức năng pause và resume trên Gridview cell progress Devexpress.
[DEVEXPRESS] Download file with Pause/Resume Progressbar Cell C#
Trong bài viết trước, mình đã có viết hai bài về download.
- Bài download file trên Gridview cell với progress bar
- Và bài sử dụng thư viện AltoHttp
Trong bài này, mình sẽ tích hợp 2 chức năng này vào trong một app.
Giao diện ứng dụng:

Source code C#:
using AltoHttp;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace progressbar_GridView
{
    public partial class Form1 : DevExpress.XtraEditors.XtraForm
    {
        public Form1()
        {
            InitializeComponent();
        }
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            var listDownload = new List<FileDownload>();
            var fileURL = new FileDownload();
            fileURL.url = @"https://dl.teamviewer.com/download/version_15x/TeamViewer_Setup.exe";
            fileURL.progress = 0;
            fileURL.status = "Ready";
            fileURL.speed = "";
            fileURL.data = "";
            fileURL.filename = Path.GetFileName(fileURL.url);
            listDownload.Add(fileURL);
         
            var fileURL3 = new FileDownload();
            fileURL3.url = @"https://nodejs.org/dist/v13.9.0/node-v13.9.0-x64.msi";
            fileURL3.progress = 0;
            fileURL3.data = "";
            fileURL3.speed = "";
            fileURL3.status = "Ready";
            fileURL3.filename = Path.GetFileName(fileURL3.url);
            listDownload.Add(fileURL3);
            var fileURL4 = new FileDownload();
            fileURL4.url = @"https://download.microsoft.com/download/1/9/4/1949aa9c-6536-48f2-81fa-e7bb07410b36/SSMS-Setup-ENU.exe";
            fileURL4.progress = 0;
            fileURL4.data = "";
            fileURL4.speed = "";
            fileURL4.status = "Ready";
            fileURL4.filename = Path.GetFileName(fileURL4.url);
            listDownload.Add(fileURL4);
            gridControl1.DataSource = listDownload;
        }
        private void UpdateProgressByRowHandle(int rowHandle, string column, string message)
        {
            gridControl1.BeginInvoke((Action)(() =>
            {
                gridView1.SetRowCellValue(rowHandle, column, message);
            }));
        }
        Dictionary<int, HttpDownloader> listDownloader = new Dictionary<int, HttpDownloader>();
        private void gridView1_RowCellClick(object sender, RowCellClickEventArgs e)
        {
            if (e.Column.Name.Equals("grid_pause"))
            {
                var mDownloader = listDownloader.FirstOrDefault(x => x.Key == e.RowHandle).Value;
                if(mDownloader != null)
                {
                    mDownloader.Pause();
                    UpdateProgressByRowHandle(e.RowHandle, "status", "Paused");
                }
            }
            if (e.Column.Name.Equals("grid_resume"))
            {
                var mDownloader = listDownloader.FirstOrDefault(x => x.Key == e.RowHandle).Value;
                if (mDownloader != null)
                {
                    mDownloader.ResumeAsync();
                    UpdateProgressByRowHandle(e.RowHandle, "status", "Resume...");
                }
            }
            if (e.Column.Name.Equals("grid_Action"))
            {
                var selectedRow = gridView1.GetSelectedRows();
                var urls = string.Join(",", from r in selectedRow where gridView1.IsDataRow(Convert.ToInt32(r)) select gridView1.GetRowCellValue(Convert.ToInt32(r), "url"));
                if (string.IsNullOrEmpty(urls))
                {
                    var url_one = gridView1.GetRowCellValue(e.RowHandle, "url").ToString();
                    UpdateProgressByRowHandle(Convert.ToInt16(e.RowHandle.ToString()), "status", "Initializing...");
                    DownloadFile(url_one, Application.StartupPath + @"download" + Path.GetFileName(url_one), e.RowHandle);
                    return;
                }
                var list_Rowhandles = string.Join(",", from r in selectedRow where gridView1.IsDataRow(Convert.ToInt32(r)) select gridView1.GetRowHandle(Convert.ToInt32(r)));
                var url = urls.Split(',');
                var list_Rowhandle = list_Rowhandles.Split(',');
                for (int i = 0; i < url.Length; i++)
                {
                    UpdateProgressByRowHandle(Convert.ToInt16(list_Rowhandle[i].ToString()), "status", "Initializing...");
                    DownloadFile(url[i].ToString(), Application.StartupPath + @"download" + Path.GetFileName(url[i].ToString()), Convert.ToInt16(list_Rowhandle[i].ToString()));
                }
            }
        }
        private void downloadComplete(object sender, EventArgs e, int rowHandle)
        {
            listDownloader.Remove(rowHandle);
            UpdateProgressByRowHandle(rowHandle, "status", "Finish.");
        }
        private void downloadProgress(object sender, AltoHttp.DownloadProgressChangedEventArgs e, int rowHandle, HttpDownloader mDownloader)
        {
            UpdateProgressByRowHandle(rowHandle, "progress", e.Progress.ToString());
            UpdateProgressByRowHandle(rowHandle, "data", string.Format("{0} / {1} MB",
               (mDownloader.BytesReceived / 1024d / 1024d).ToString("0.00"),
               (mDownloader.ContentSize / 1024d / 1024d).ToString("0.00")));
          
            UpdateProgressByRowHandle(rowHandle, "status", "Downloading...");
            UpdateProgressByRowHandle(rowHandle, "speed", string.Format("{0} Kb/s", (e.Speed / 1024d).ToString("0.00")));
        }
        public void DownloadFile(string urlAddress, string location, int rowHandle)
        {
            var mDownloader = new HttpDownloader(urlAddress, location);
            try
            {
                listDownloader.Add(rowHandle, mDownloader);
            }
            catch (Exception)
            {
                UpdateProgressByRowHandle(rowHandle, "status", "File is Downloading...");
                //XtraMessageBox.Show("File is Downloading..", "Infomation", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
         
            mDownloader.DownloadProgressChanged += new AltoHttp.ProgressChangedEventHandler((s, e) => downloadProgress(s,e, rowHandle, mDownloader));
            mDownloader.DownloadCompleted += new EventHandler((s,e) => downloadComplete(s,e, rowHandle));            
            mDownloader.StartAsync();
           
        }
        private void btn_resumeall_Click(object sender, EventArgs e)
        {
            foreach (var item in listDownloader.Values)
            {
                item.ResumeAsync();
            }
            foreach (var rowHandle in listDownloader.Keys)
            {
             
                UpdateProgressByRowHandle(rowHandle, "status", "Resume...");
            }
        }
        private void btn_pauseall_Click(object sender, EventArgs e)
        {
            foreach(var item in listDownloader.Values)
            {
                item.Pause();
            }
            foreach (var rowHandle in listDownloader.Keys)
            {
                UpdateProgressByRowHandle(rowHandle, "status", "Paused");
            }
        }
    }
    public class FileDownload
    {
        public string url { get; set; }
        public int progress { get; set; }
        public string speed { get; set; }
        public string status { get; set; }
        public string filename { get; set; }
        public string data { get; set; }
    }
}
Video Demo:
Thanks for watching!

![[DEVEXPRESS] Tích hợp Pause/Resume vào download Progress Cell Gridview](https://laptrinhvb.net/uploads/users/9a8cb514e4428e85fb4ca07588e9103f.png)

![[DEVEXPRESS] Auto Demo Trainning ứng dụng  Sử dụng thư viện Tutorials](https://laptrinhvb.net/uploads/source/devexpress/auto_action_app.gif)
![[DEVEXPRESS] Hướng dẫn lọc dữ liệu Filter LookupEdit trên Gridview](https://laptrinhvb.net/uploads/source/devexpress/lookupedit_gridview.gif)
![[DEVEXPRESS] Hướng dẫn thiết kế phần mềm ứng dụng Fluent Design Windows 10](https://laptrinhvb.net/uploads/source/devexpress/fluent_design_thumb.png)
![[DEVEXPRESS] Hướng dẫn cách tạo progressbar overlay Gridview C#](https://laptrinhvb.net/uploads/source/devexpress/overlay_progressbar_devexpress_thumb.png)

![[DEVEXPRESS] Hướng dẫn thay đổi giao diện Palette trên Skin The Bezier](https://laptrinhvb.net/uploads/source/csharp/change_skin_theme.jpg)
![[DEVEXPRESS] Chia sẻ source code tạo báo cáo report in tem nhãn label trên C# winform](https://laptrinhvb.net/uploads/source/devexpress/label_report_thumb.png)
![[DEVEXPRESS] Hướng dẫn sử dụng Assembly Deployment Tool để đóng gói ứng dụng](https://laptrinhvb.net/uploads/source/devexpress/Assembly_Deployment_Tool.png)
![[DEVEXPRESS] Hướng dẫn Custom Summary in Footer trong Gridview C#](https://laptrinhvb.net/uploads/source/devexpress/custom_summary_thumb.png)
![[DEVEXPRESS] Hướng dẫn thay đổi giao diện sử dụng Look and Feel C#](https://laptrinhvb.net/uploads/source/image_baiviet/49636690f76e83a96aa8be5ea88b4f6b.png)
![[DEVEPXRESS] Hướng dẫn sử dụng Do not show message Again trong XtraMessageBox Winform](https://laptrinhvb.net/uploads/source/DATABASE/donot_show_message.png)
![[DEVEXPRESS] Hướng dẫn tìm kiếm không dấu tiếng việt  auto filter trên GridView](https://laptrinhvb.net/uploads/source/image_baiviet/930c4ad9a5c9ede690ea479dbb64c679.jpg)
![[DEVEXPRESS] Hướng dẫn tạo hiệu ứng transition hiển thị cài đặt nâng cao](https://laptrinhvb.net/uploads/source/devexpress/tramsition_advance_setting.png)
![[DEVEXPRESS] Format code T-SQL highlight in RichEditControl](https://laptrinhvb.net/uploads/source/new_image_baiviet/sql_format_richeditcontrol_devexpress.png)
![[DEVEXPRESS] Hướng dẫn thiết kế Form Setting Tùy chọn trong Winform](https://laptrinhvb.net/uploads/source/devexpress/form_setting.jpg)
![[DEVEXPRESS] Thiết kế giao diện mua sắm Shopping Fashion sử dụng WinExplorer Template HTML và CSS](https://laptrinhvb.net/uploads/source/devexpress/winexplorer_devexpress_demo.png)
![[DEVEXPRESS] Hướng dẫn ẩn hiển thị icon header column filter trên GridView](https://laptrinhvb.net/uploads/source/csharp/remove_filter_header_gridview_thumb.jpg)

![[DEVEXPRESS] Tìm dữ liệu trùng lắp Duplicate và tô màu trên Gridview](https://laptrinhvb.net/uploads/source/devexpress/find_duplicate_value_gridview.gif)
![[DEVEXPRESS] Hướng dẫn thêm checkbox Button in Band GridView](https://laptrinhvb.net/uploads/source/image_baiviet/d2ffc9cd5b058ea126b8aedf746c6800.png)
![[DEVEXPRESS] Hướng dẫn sử dụng Mail Merge trong Rich Edit Control VB.NET](https://laptrinhvb.net/uploads/source/image_baiviet/70358a4543901f51420ea03a97e0cef3.png)
![[DEVEXPRESS] Hướng dẫn sử dụng Color Mixer để thay đổi giao diện winform](https://laptrinhvb.net/uploads/source/image_baiviet/36eb9b8ac7dcca2a07898e02ca1b8b0e.jpg)
![[DEVEXPRESS] Hướng dẫn viết ứng dụng Infinite Scrolling trong GridView sử dụng VirtualServerModeSource](https://laptrinhvb.net/uploads/source/new_image_baiviet/Infinite_Scrolling.png)
![[DEVEXPRESS] Hướng dẫn chọn nhiều dòng multi select trong GridLookupEdit](https://laptrinhvb.net/uploads/source/devexpress/MultiSelectGridComboxLookup_thumb.png)

