翻譯|使用教程|編輯:龔雪|2023-04-21 09:48:34.120|閱讀 238 次
概述:網格是Telerik UI for WinForms中很常用的數據組件,本文介紹如何解析可用于數據傳輸的不同文件或文本格式,歡迎下載組件體驗!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
Telerik UI for WinForms擁有適用Windows Forms的110多個令人驚嘆的UI控件,所有的UI for WinForms控件都具有完整的主題支持,可以輕松地幫助開發人員在桌面和平板電腦應用程序提供一致美觀的下一代用戶體驗。
Telerik UI for WinForms組件為可視化任何類型的數據提供了非常豐富的UI控件,其中RadGridView是最常用的數據組件。本文主要深入研究數據綁定,它是用記錄填充網格的主要方法,數據記錄通常存儲在服務器上或文件中。
本文將重點分析可用于數據傳輸的不同文件或文本格式,從綁定到DataTable的一般情況開始。
Telerik_KendoUI產品技術交流群:726377843 歡迎一起進群討論
將一些數據綁定到RadGridView的最簡單方法是創建一個數據表,用相應的類型定義一些列,并添加一些帶有單元格內容的行:
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; }
RadGridView不支持直接綁定到JSON,但是可以將JSON內容轉換為數據表,然后將解析后的數據表設置為RadGridView控件的數據源。
注意:有必要添加對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)文件是存儲以表格結構格式保存的大量數據內容的一種簡單方法,它也可以很容易地解析為數據表,從而再次用作網格的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能夠顯示分層數據,其中主要場景可以列出如下:
數據內容由一個平面集合表示,其中的層次關系由ID和父級的ID字段定義。因此每個記錄都知道其父記錄的ID,可以構建嵌套結構。
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; }
數據內容由兩個(或根據層次結構深度最多N個)平面集合表示,其中每個層次結構級別需要一個單獨的數據集合和一個網格模板來存儲數據。
不同的網格級別用一個特定的關系連接,稱為GridViewRelation,分別指向父級和子級。它鏈接父級的一個字段和子級的一個字段,這與SQL表中的外鍵非常接近。
下面的代碼片段演示了如何構建Categories-Products對象關系層次結構:
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); }
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網