- [C#] Sử dụng DotNetBrowser nhân Chromium giải pháp thay thế WebBrowser trên Winform
- [C#] Viết ứng dụng xem và dò kết quả xổ số kiến thiết miền nam
- [C#] Hướng dẫn viết ứng dụng theo dõi máy in bao nhiêu trang (Monitor Printer)
- [C#] Lấy thông tin cấu hình máy tính xuất ra text file winform
- [C#] Chia sẽ class Install, Uninstall, Start và Stop Services Winform
- [C#] Tìm kiếm tập tin file nhanh chóng trên Winform sử dụng thư viện FastSearchLibrary
- [C#] Giới thiệu thư viện Fluent FTP Awesome dùng để làm việc với FTP
- [C#] Sử dụng thư viện Mini Profiler Integrations ghi log thực hiện các câu lệnh SQL
- [DEVEXPRESS] Thiết kế Dropdown ButtonBarItem trên Form Ribbon
- [C#] Lưu trạng thái các control trên Winform vào Registry Windows
- [C#] Ứng dụng ví dụ Simple Observer Pattern tăng giảm số lượng trên winform
- [C#] Hướng dẫn lấy thời gian thực server time trên winform
- [DEVEXPRESS] Hướng dẫn bật tính năng Scroll Pixcel in Touch trên GridView
- [DEVEXPRESS] Hướng dẫn sử dụng TileBar viết ứng dụng duyệt hình ảnh Winform
- [DEVEXPRESS] Tô màu border TextEdit trên Winform
- [C#] Lấy dữ liệu từ Console Write hiển thị lên textbox Winform
- [C#] Hiển thị Progress bar trên Window Console
- [C#] Di chuyển control Runtime và lưu layout trên winform
- [SQLSERVER] Sử dụng hàm NULL IF
- [C#] Chia sẽ source code mã đi tuần bằng giao diện Winform
[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!