- [C#] Hướng dẫn fix lỗi Visual Studio 2022 not Support Target Net Framework 4.5.2
- [C#] Giới thiệu thư viện Sunny UI thiết kế giao diện trên Winform
- [DATABASE] Hướng dẫn thêm và cập nhật Extended Property Column trong Table Sqlserver
- [DEVEXPRESS] Hướng dẫn sử dụng Vertical Gridview để hiển thị thông tin sản phẩm
- [C#] Hướng dẫn sử dụng Json Schema để Validate chuỗi string có phải json
- [C#] Hướng dẫn sử dụng công cụ Clean Code trên Visual Studio
- [C#] Hướng dẫn Drag and Drop File vào RichTextBox
- [C#] Hướng dẫn tạo hiệu ứng Karaoke Text Effect Winform
- [C#] Sử dụng thư viện ZedGraph vẽ biểu đồ Line, Bar, Pie trên Winform
- [DATABASE] Hướng dẫn sort sắp xếp địa chỉ IP trên sqlserver sử dụng hàm PARSENAME
- [C#] Theo dõi sử kiện process Start hay Stop trên Winform
- [ASP.NET] Chia sẻ source code chụp hình sử dụng camera trên website
- [C#] Chạy ứng dụng trên Virtual Desktop (màn hình ảo) Winform
- [C#] Mã hóa và giải mã Data Protection API trên winform
- [C#] Hướng dẫn tạo Gradient Background trên Winform
- [DATABASE] Hướng dẫn convert Epoch to DateTime trong sqlserver
- [DATABASE] Hướng dẫn sử dụng STRING_AGG và CONCAT_WS trong sqlserver 2017
- [C#] Hướng dẫn Copy With All Property in Class Object
- [DEVEXPRESS] Hướng dẫn load Json DataSource vào GridView
- [C#] Hướng dẫn tạo chữ ký trên winform Signature Pad
[DEVEXPRESS] Download multi file sử dụng progressbar trên cell gridview
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:
Ở 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
- Sử dụng Progressbar trên Gridview
- Thay đổi giá trị trực tiếp realtime trên Grid
- Downlaod file
- Lấy các thông số của download file: tốc độ, dung lượng file download.
- 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!