[DEVEXPRESS] Hướng dẫn sử dụng Unbound Column trên GridView C#
Xin chào các bạn bài viết hôm nay mình sẽ hướng dẫn các bạn cách sử dụng Unbound Column trên Gridview Devexpress C# winform.
[DEVEXPRESS] USING UNBOUND COLUMN GRIDVIEW C#
Ví dụ: các bạn theo dõi hình ảnh demo dưới đây.
Khi thiết lập đơn hàng hay xuất hàng, các bạn sẽ có 2 cột dữ liệu: Quantity
và Price
(số lương và đơn giá)
Còn thành tiền, thì mình sẽ không có lưu dưới cơ sở dữ liệu database.
Vậy khi hiển thị mình mong muốn hiển thị thêm cột Thành Tiền tự động được tính bằng Số lượng * Đơn giá.
Thì ở Column Total Money: các bạn không điền filedname, và setup Unbound column như hình dưới đây.
Do kiểu Total Money nên mình chọn là kiểu Decimal nhé.
Hoặc bạn có thể chỉnh Unbound column runtime ở đây:
Full source code Unbound Column C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace unBoundColumn
{
public partial class Form1 : DevExpress.XtraEditors.XtraForm
{
public Form1()
{
InitializeComponent();
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
var table = new DataTable();
table.Columns.Add("ID", typeof(int));
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Quantity", typeof(decimal));
table.Columns.Add("Price", typeof(decimal));
table.Rows.Add(1, "Apple", 5, 12.5);
table.Rows.Add(1, "Banana", 8, 5.25);
table.Rows.Add(1, "Tomato", 3, 4.23);
table.Rows.Add(1, "Oranges", 7, 15.28);
table.Rows.Add(1, "Lemon", 15, 12.36);
table.Rows.Add(1, "Water Lemon", 23, 9.63);
table.Rows.Add(1, "Cherry", 9, 7.5);
table.Rows.Add(1, "Coconuts", 18, 20.1);
table.Rows.Add(1, "Kiwi", 2, 15.23);
gridControl1.DataSource = table;
}
private void btn_expression_Click(object sender, EventArgs e)
{
gridView1.ShowUnboundExpressionEditor(colTotalMoney);
}
private void labelControl1_Click(object sender, EventArgs e)
{
}
}
}
Happy coding :)