NEWS

[DEVEXPRESS] Download multi file sử dụng progressbar trên cell gridview

[DEVEXPRESS] Download multi file sử dụng progressbar trên cell gridview
Đăng bởi: Thảo Meo - Lượt xem: 7918 11:29:27, 20/02/2020C#   In bài viết

Xin chào các bạn, bài viết hôm nay mình sẽ tiếp tục hướng dẫn các bạn cách download multi file sử dụng progressbar trên từng cell của Gridview devexpress C#.

[DEVEXPRESS] Download multi file with progressbar cell gridview c#

Các bạn có thể sử dụng bài viết này: để viết các dụng dạng cập nhật phần mềm...

Dưới đây là giao diện demo ứng dụng download file của mình:

progressbar_gridview_csharp

Ở bài viết này, sẽ cung cấp cho các bạn sử dụng được thêm các chức năng trong một bài ví dụ demo của Devexpress

  1. Sử dụng Progressbar trên Gridview
  2. Thay đổi giá trị trực tiếp realtime trên Grid
  3. Downlaod file 
  4. Lấy các thông số của download file: tốc độ, dung lượng file download.
  5. Download file Async Multi Task

Các bạn có thể Xem video demo ứng dụng của mình:

Source code C#:

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 fileURL2 = new FileDownload();           
            fileURL2.url = @"http://s3.amazonaws.com/calibrize/calibrize_2_setup.exe";
            fileURL2.progress = 0;
            fileURL2.data = "";
            fileURL2.speed = "";
            fileURL2.status = "Ready";
            fileURL2.filename = Path.GetFileName(fileURL2.url);
            listDownload.Add(fileURL2);

            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);
            }));
        }

        private void gridView1_RowCellClick(object sender, RowCellClickEventArgs e)
        {
            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()));
                  
                }

               

            }
        }
        

      
        public void DownloadFile(string urlAddress, string location, int rowHandle)
        {
            Stopwatch sw = new Stopwatch();
            using (WebClient webClient = new WebClient())
            {
                webClient.DownloadFileCompleted += new AsyncCompletedEventHandler((sender, e) => Completed(sender, e, rowHandle, sw));
                webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler((sender, e) => ProgressChanged(sender, e, rowHandle, sw));           
                Uri URL = new Uri(urlAddress);
                sw.Start();
                try
                {                   
                    webClient.DownloadFileAsync(URL, location);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
      
        private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e, int rowHandle, Stopwatch sw)
        {
            UpdateProgressByRowHandle(rowHandle, "progress", e.ProgressPercentage.ToString());
            UpdateProgressByRowHandle(rowHandle, "data", string.Format("{0} / {1} MB",
               (e.BytesReceived / 1024d / 1024d).ToString("0.00"),
               (e.TotalBytesToReceive / 1024d / 1024d).ToString("0.00")));
            UpdateProgressByRowHandle(rowHandle, "status", "Downloading...");
            UpdateProgressByRowHandle(rowHandle, "speed", string.Format("{0} Kb/s", (e.BytesReceived / 1024d / sw.Elapsed.TotalSeconds).ToString("0.00")));

        }


        private void Completed(object sender, AsyncCompletedEventArgs e, int rowHandle, Stopwatch sw)
        {          
            if (e.Cancelled == true)
            {
                UpdateProgressByRowHandle(rowHandle, "status", "Fails.");
                sw.Stop();
            }
            else
            {
                UpdateProgressByRowHandle(rowHandle, "status", "Finish.");
                UpdateProgressByRowHandle(rowHandle, "speed", "0 Kb/s");
                sw.Stop();
            }
        }
    }
    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; }

    }
}

Thanks for watching!

 

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[DEVEXPRESS] Download multi file sử dụng progressbar trên cell gridview
Đăng bởi: Thảo Meo - Lượt xem: 7918 11:29:27, 20/02/2020C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.