翻譯|使用教程|編輯:周思宇|2023-05-22 10:17:39.027|閱讀 160 次
概述:網(wǎng)格是Telerik UI for WinForms中很常用的數(shù)據(jù)組件,本文介紹如何使用未綁定模式填充RadGridView數(shù)據(jù),歡迎下載組件體驗(yàn)!
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
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ù)組件。在上文中(點(diǎn)擊這里回顧>>),我們主要介紹了如何添加層次結(jié)構(gòu)中的多個(gè)子選項(xiàng)卡、嵌套多級(jí)層次結(jié)構(gòu)等,本文繼續(xù)介紹如何使用未綁定模式填充RadGridView數(shù)據(jù)。
Telerik_KendoUI產(chǎn)品技術(shù)交流群:726377843 歡迎一起進(jìn)群討論
在不同的場(chǎng)景中,開發(fā)人員不希望將源集合直接映射到網(wǎng)格控件。在這種情況下,使用非綁定模式填充RadGridView數(shù)據(jù)是合適的。
當(dāng)使用非綁定模式時(shí),在Telerik UI for WinForms中的RadGridView支持根據(jù)定義的列以編程方式添加行。因此,可以為每個(gè)單元格指定一個(gè)值并存儲(chǔ)合適的數(shù)據(jù)類型。
根據(jù)要存儲(chǔ)的數(shù)據(jù)類型,RadGridView提供了不同的數(shù)據(jù)列。
在設(shè)計(jì)器中選擇RadGridView控件后,點(diǎn)擊右上角的小箭頭打開智能標(biāo)簽:
列瀏覽按鈕打開 GridViewDataColumn 集合編輯器
使用它,您可以添加特定場(chǎng)景所需的列類型。添加完列后,單擊OK按鈕,網(wǎng)格將被來自集合編輯器的列填充:
我們將從定義用于管理日期時(shí)間、十進(jìn)制、字符串和圖像值的列開始。為此,我們將使用GridViewDateTimeColumn, GridViewDecimalColumn, GridViewTextBoxColumn, GridViewImageColumn和GridViewBrowseColumn。然后,將添加幾行并填充適當(dāng)?shù)臄?shù)據(jù)。
瀏覽列將用于將圖像上傳到相應(yīng)的列。當(dāng)在瀏覽單元中選擇了一個(gè)有效的文件路徑時(shí),整個(gè)程序邏輯在CellValueChange事件中執(zhí)行:
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()); } }
上面的示例只向網(wǎng)格中添加一行。如果我們?cè)黾犹砑拥男袛?shù),例如100行,我們會(huì)注意到在執(zhí)行添加操作時(shí)出現(xiàn)一些延遲。添加每行會(huì)觸發(fā)視覺元素的刷新操作。因此,添加的行越多,執(zhí)行的刷新操作就越多。
重要的是:BeginUpdate和EndUpdate結(jié)構(gòu)掛起所有的可視化更新,并允許你在向RadGridView添加大量行時(shí)提高性能。行集合:
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"); }
下面的動(dòng)畫以更好的方式說明了兩者的區(qū)別:
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn