翻譯|使用教程|編輯:周思宇|2023-05-22 10:17:39.027|閱讀 160 次
概述:網格是Telerik UI for WinForms中很常用的數據組件,本文介紹如何使用未綁定模式填充RadGridView數據,歡迎下載組件體驗!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
Telerik UI for WinForms擁有適用Windows Forms的110多個令人驚嘆的UI控件,所有的UI for WinForms控件都具有完整的主題支持,可以輕松地幫助開發人員在桌面和平板電腦應用程序提供一致美觀的下一代用戶體驗。
Telerik UI for WinForms組件為可視化任何類型的數據提供了非常豐富的UI控件,其中RadGridView是最常用的數據組件。在上文中(點擊這里回顧>>),我們主要介紹了如何添加層次結構中的多個子選項卡、嵌套多級層次結構等,本文繼續介紹如何使用未綁定模式填充RadGridView數據。
Telerik_KendoUI產品技術交流群:726377843 歡迎一起進群討論
在不同的場景中,開發人員不希望將源集合直接映射到網格控件。在這種情況下,使用非綁定模式填充RadGridView數據是合適的。
當使用非綁定模式時,在Telerik UI for WinForms中的RadGridView支持根據定義的列以編程方式添加行。因此,可以為每個單元格指定一個值并存儲合適的數據類型。
根據要存儲的數據類型,RadGridView提供了不同的數據列。
在設計器中選擇RadGridView控件后,點擊右上角的小箭頭打開智能標簽:
列瀏覽按鈕打開 GridViewDataColumn 集合編輯器
使用它,您可以添加特定場景所需的列類型。添加完列后,單擊OK按鈕,網格將被來自集合編輯器的列填充:
我們將從定義用于管理日期時間、十進制、字符串和圖像值的列開始。為此,我們將使用GridViewDateTimeColumn, GridViewDecimalColumn, GridViewTextBoxColumn, GridViewImageColumn和GridViewBrowseColumn。然后,將添加幾行并填充適當的數據。
瀏覽列將用于將圖像上傳到相應的列。當在瀏覽單元中選擇了一個有效的文件路徑時,整個程序邏輯在CellValueChange事件中執行:
public UnboundForm() { InitializeComponent(); this.radGridView1.TableElement.RowHeight = 40; this.radGridView1.AutoSizeRows = true; GridViewDateTimeColumn dateColumn = new GridViewDateTimeColumn("OrderDate"); dateColumn.FormatString = "{0:dd/MM/yyyy}"; //format the cell's text dateColumn.Format = DateTimePickerFormat.Custom; dateColumn.CustomFormat = "dd/MM/yyyy"; //format the cell's editor dateColumn.Width = 200; this.radGridView1.Columns.Add(dateColumn); GridViewDecimalColumn priceColumn = new GridViewDecimalColumn("Price"); priceColumn.HeaderText = "Unit Price"; priceColumn.DecimalPlaces = 2; priceColumn.FormatString = "{0:C}"; priceColumn.FormatInfo = new System.Globalization.CultureInfo("en-GB"); priceColumn.Width = 100; radGridView1.MasterTemplate.Columns.Add(priceColumn); GridViewTextBoxColumn textBoxColumn = new GridViewTextBoxColumn("ProductName"); textBoxColumn.MaxLength = 50; textBoxColumn.Width = 150; textBoxColumn.TextAlignment = ContentAlignment.MiddleCenter; radGridView1.MasterTemplate.Columns.Add(textBoxColumn); GridViewImageColumn imageColumn = new GridViewImageColumn("Photo"); imageColumn.Width = 100; imageColumn.ImageLayout = ImageLayout.Zoom; radGridView1.MasterTemplate.Columns.Add(imageColumn); GridViewBrowseColumn browseColumn = new GridViewBrowseColumn("Upload photo"); browseColumn.Width = 300; this.radGridView1.Columns.Add(browseColumn); this.radGridView1.CellValueChanged += RadGridView1_CellValueChanged; this.radGridView1.ValueChanged += RadGridView1_ValueChanged; this.radGridView1.Rows.Add(new DateTime(2023, 3,20),20.49, "Banana"); this.radGridView1.TableElement.RowHeight = 50; } private void RadGridView1_ValueChanged(object sender, EventArgs e) { GridBrowseEditor browseEditor = sender as GridBrowseEditor; if (browseEditor!=null && browseEditor.Value!=null) { this.radGridView1.EndEdit(); //commit the value directly after selecting a new image file } } private void RadGridView1_CellValueChanged(object sender, GridViewCellEventArgs e) { if (e.Column.Name == "Upload photo" && e.Value != null) { e.Row.Cells["Photo"].Value = Image.FromFile(e.Value.ToString()); } }
上面的示例只向網格中添加一行。如果我們增加添加的行數,例如100行,我們會注意到在執行添加操作時出現一些延遲。添加每行會觸發視覺元素的刷新操作。因此,添加的行越多,執行的刷新操作就越多。
重要的是:BeginUpdate和EndUpdate結構掛起所有的可視化更新,并允許你在向RadGridView添加大量行時提高性能。行集合:
private void radButton1_Click(object sender, EventArgs e) { AddRows(this.radCheckBox1.Checked); } private void AddRows(bool isSuspended) { Stopwatch sw = new Stopwatch(); sw.Start(); int n = 100; if (isSuspended) { this.radGridView1.BeginUpdate(); } int startIndex = this.radGridView1.Rows.Count; for (int i = startIndex; i < startIndex+ n; i++) { this.radGridView1.Rows.Add(DateTime.Now.AddHours(i), i * 0.25, i + ". " + Guid.NewGuid()); ; } if (isSuspended) { this.radGridView1.EndUpdate(); } sw.Stop(); RadMessageBox.Show(this.radGridView1,"Adding " + n + " rows took " + sw.ElapsedMilliseconds.ToString() + " milliseconds"); }
下面的動畫以更好的方式說明了兩者的區別:
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn