Telerik UI for WinForms擁有適用Windows Forms的110多個(gè)令人驚嘆的UI控件,所有的UI for WinForms控件都具有完整的主題支持,可以輕松地幫助開發(fā)人員在桌面和平板電腦應(yīng)用程序提供一致美觀的下一代用戶體驗(yàn)。
Telerik UI for WinForms組件為可視化任何類型的數(shù)據(jù)提供了非常豐富的UI控件,其中RadGridView是最常用的數(shù)據(jù)組件。本文主要深入研究數(shù)據(jù)綁定,它是用記錄填充網(wǎng)格的主要方法,數(shù)據(jù)記錄通常存儲(chǔ)在服務(wù)器上或文件中。
本文將重點(diǎn)分析可用于數(shù)據(jù)傳輸?shù)牟煌募蛭谋靖袷剑瑥慕壎ǖ紻ataTable的一般情況開始。
獲取Telerik UI for ASP. NET MVC R1 2023下載(Q技術(shù)交流:726377843)
綁定到DataTable
將一些數(shù)據(jù)綁定到RadGridView的最簡(jiǎn)單方法是創(chuàng)建一個(gè)數(shù)據(jù)表,用相應(yīng)的類型定義一些列,并添加一些帶有單元格內(nèi)容的行:
private void BindToDataTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("CreatedOn", typeof(DateTime));
for (int i = 0; i < 10; i++)
{
dt.Rows.Add(i, "Item"+i, DateTime.Now.AddDays(i));
}
this.radGridView1.DataSource = dt;
}

綁定到JSON
RadGridView不支持直接綁定到JSON,但是可以將JSON內(nèi)容轉(zhuǎn)換為數(shù)據(jù)表,然后將解析后的數(shù)據(jù)表設(shè)置為RadGridView控件的數(shù)據(jù)源。
注意:有必要添加對(duì)Json. NET的引用:

public partial class RadForm1 : Telerik.WinControls.UI.RadForm
{
public RadForm1()
{
InitializeComponent();
BindToJson();
this.radGridView1.BestFitColumns();
}
private void BindToJson()
{
string json = @"[
{""id"":""10"",""name"":""User"",""add"":false,""edit"":true,""authorize"":true,""view"":true},
{ ""id"":""11"",""name"":""Group"",""add"":true,""edit"":false,""authorize"":false,""view"":true},
{ ""id"":""12"",""name"":""Permission"",""add"":true,""edit"":true,""authorize"":true,""view"":true}
]";
DataTable table = Newtonsoft.Json.JsonConvert.DeserializeObject<DataTable>(json);
this.radGridView1.DataSource = table;
}
}

綁定到CSV
逗號(hào)分隔值(CSV)文件是存儲(chǔ)以表格結(jié)構(gòu)格式保存的大量數(shù)據(jù)內(nèi)容的一種簡(jiǎn)單方法,它也可以很容易地解析為數(shù)據(jù)表,從而再次用作網(wǎng)格的DataSource集合。

public RadForm1()
{
InitializeComponent();
BindToCsv();
}
private void BindToCsv()
{
bool isFirstRowHeader = true;
string path = @"..\..\sampleData.csv";
string header = isFirstRowHeader ? "Yes" : "No";
string pathOnly = System.IO.Path.GetDirectoryName(path);
string fileName = System.IO.Path.GetFileName(path);
string sql = @"SELECT * FROM [" + fileName + "]";
using (System.Data.OleDb.OleDbConnection connection = new System.Data.OleDb.OleDbConnection(
@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly +
";Extended Properties=\"Text;HDR=" + header + "\""))
using (System.Data.OleDb.OleDbCommand command = new System.Data.OleDb.OleDbCommand(sql, connection))
using (System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(command))
{
DataTable dataTable = new DataTable();
dataTable.Locale = CultureInfo.CurrentCulture;
adapter.Fill(dataTable);
this.radGridView1.DataSource = dataTable;
}
}
RadGridView能夠顯示分層數(shù)據(jù),其中主要場(chǎng)景可以列出如下:
- 自引用層次結(jié)構(gòu)
- 對(duì)象-關(guān)系層次結(jié)構(gòu)(主細(xì)節(jié))
- 按需加載
綁定到自引用層次結(jié)構(gòu)
數(shù)據(jù)內(nèi)容由一個(gè)平面集合表示,其中的層次關(guān)系由ID和父級(jí)的ID字段定義。因此每個(gè)記錄都知道其父記錄的ID,可以構(gòu)建嵌套結(jié)構(gòu)。
private void BindSelfReferenceHierarchy()
{
DataTable selfRefTable = new DataTable();
selfRefTable.Columns.Add("Id", typeof(int));
selfRefTable.Columns.Add("ParentId", typeof(int));
selfRefTable.Columns.Add("Name", typeof(string));
selfRefTable.Rows.Add(1, 0, "My Computer");
selfRefTable.Rows.Add(2, 1, @"C:\");
selfRefTable.Rows.Add(3, 2, "Program Files");
selfRefTable.Rows.Add(4, 3, "Microsoft");
selfRefTable.Rows.Add(5, 3, "Telerik");
selfRefTable.Rows.Add(6, 2, "WINDOWS");
selfRefTable.Rows.Add(7, 1, @"D:\");
this.radGridView1.Relations.AddSelfReference(this.radGridView1.MasterTemplate, "Id", "ParentId");
this.radGridView1.DataSource = selfRefTable;
}
綁定到對(duì)象關(guān)系數(shù)據(jù)
數(shù)據(jù)內(nèi)容由兩個(gè)(或根據(jù)層次結(jié)構(gòu)深度最多N個(gè))平面集合表示,其中每個(gè)層次結(jié)構(gòu)級(jí)別需要一個(gè)單獨(dú)的數(shù)據(jù)集合和一個(gè)網(wǎng)格模板來(lái)存儲(chǔ)數(shù)據(jù)。
不同的網(wǎng)格級(jí)別用一個(gè)特定的關(guān)系連接,稱為GridViewRelation,分別指向父級(jí)和子級(jí)。它鏈接父級(jí)的一個(gè)字段和子級(jí)的一個(gè)字段,這與SQL表中的外鍵非常接近。
下面的代碼片段演示了如何構(gòu)建Categories-Products對(duì)象關(guān)系層次結(jié)構(gòu):文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-426690.html
private void BindToObjectRelational()
{
Random rand = new Random();
DataTable categories = new DataTable();
categories.Columns.Add("CategoryID", typeof(int));
categories.Columns.Add("Title", typeof(string));
categories.Columns.Add("CreatedOn", typeof(DateTime));
for (int i = 0; i < 5; i++)
{
categories.Rows.Add(i, "Master" + i, DateTime.Now.AddDays(i));
}
DataTable productsTable = new DataTable();
productsTable.Columns.Add("ProductID", typeof(int));
productsTable.Columns.Add("CategoryID", typeof(int));
productsTable.Columns.Add("Name", typeof(string));
productsTable.Columns.Add("UnitPrice", typeof(decimal));
for (int i = 0; i < 30; i++)
{
productsTable.Rows.Add(i, rand.Next(0, 5), "Product" + i, 1.25 * i);
}
this.radGridView1.MasterTemplate.DataSource = categories;
this.radGridView1.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
GridViewTemplate productsLevel = new GridViewTemplate();
productsLevel.DataSource = productsTable;
productsLevel.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
this.radGridView1.MasterTemplate.Templates.Add(productsLevel);
GridViewRelation relation = new GridViewRelation(radGridView1.MasterTemplate);
relation.ChildTemplate = productsLevel;
relation.RelationName = "CategoriesProducts";
relation.ParentColumnNames.Add("CategoryID");
relation.ChildColumnNames.Add("CategoryID");
this.radGridView1.Relations.Add(relation);
}

? 文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-426690.html
到了這里,關(guān)于界面控件Telerik UI for WinForms使用指南 - 數(shù)據(jù)綁定 & 填充(一)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!