C#设计在屏幕上显示数据的问题 [英] C# Design questions displaying data on screen

查看:96
本文介绍了C#设计在屏幕上显示数据的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我构建了一个应用程序,可以从Web源抓取数据,将数据处理成锯齿状数组,然后最终在各个标签中显示数据。



数据按感兴趣的字段分组。说:



组A由一个字符串组成[3] [3]。我以某种方式给出了标签名称,以便我可以遍历数组,然后执行(Label)Controls.Find(lb_DataA_+ index1 +_+ index2)等等。



我觉得这是一种非常低效且麻烦的显示数据的方式 - 特别是因为我在标签上也有textChangedEvents让背景颜色插入淡入淡出.I有大约50个标签来管理这种方式。



我对Visual Studio的内置功能不太熟悉。有没有办法将锯齿状数组绑定到数据网格(以某种方式将其用作数据源?)并对该数据网格的所有单元具有相同的OnTextChangedEvent?



感谢您的帮助

干杯



Daniel

Hi everyone,

I built an application that would scrape data from a web source, process that data to a jagged array and then finally display the data in individual labels.

The data is grouped by fields of interest. Say:

group A consists of a string[3][3]. I gave the labels names in a way so that I could iterate through the array and then do a (Label)Controls.Find("lb_DataA_" + index1 + "_" + index2) and so on.

I feel like this is a really inefficient and troublesome way of displaying the data - especially since I also have textChangedEvents on the labels to have the background colour interpolate fade.I have about 50 labels to manage that way.

I am not very familiar with the built in functionality of Visual Studio. Is there a way to bind a jagged array to a data grid (using it as a datasource somehow?) and have the same OnTextChangedEvent for all "Cells" of that data grid ?

Thanks for your help
Cheers

Daniel

推荐答案

我认为你是从错误的方向接近这个。



首先,让您的基础数据结构运行你想要的方式。使用INotifyPropertyChanged,检查属性的setter中是否更改了值。

然后,使用BindingSource提供给您的事件来响应更改(ListChanged,ItemChanged等)

基于这些事件,更新您的网格。



或者,只是像您所描述的那样将数据重新绑定到网格,并且不要使用CellValueChanged但是像CellFormatting事件一样应用条件格式。
I think you're approaching this from the wrong direction.

First, get your underlying data structure working the way you want it. Use INotifyPropertyChanged, check if a value is changed in the setter of a property.
Then, use the events provided to you by the BindingSource to respond to changes (ListChanged, ItemChanged etc.)
Based on those events, update your grid.

Or, just rebind the data to the grid like you described, and don't use CellValueChanged but something like CellFormatting events to apply the conditional formatting.


DataSource将使数组的每个属性成为一列。我拿了现有的字符串[] []并对其进行了以下的linq:



DataSource will make every property of the array a column . I took the existing string[][] and performend the following linq on it:

dataGridView1.DataSource = (from arr in test 
select new 
{ 
   Data = arr[0], Dog = arr[1] +
}
).ToArray();





结果是一个包含2列的数据网格视图(数据和狗)来自字符串[] []的相应值。



关于我的第二个问题 - 我将为此打开一个新线程。



The result was a datagridview with 2 columns (Data and Dog) with the respective values originating from the string[][].

Regarding my second question - I will open a new thread for that.


您总是可以在一个 DataGridView 的实例中感受到您想要的任何形式的数据,逐个单元格。如果你需要填充锯齿状数组,关注的是:锯齿状数组不会假设内部数组的元素与列到列一一对应,所以不清楚是什么意思在网格视图中排列数据,其中该对应关系在视觉上是明确的:用户合理地倾向于认为该列表示不同数据元素的相同属性水平排列。此外,阵列的锯齿状特性假定你无论如何都会有任意数量的空单元格。



如果你仍然想要按原样处理你的数据我的担忧对你来说并不重要(好吧,我仍然不知道你的目标),而且你不希望用逐个单元格的数据填充控件,其中一个可能的想法是填写具有空值的数组具有NxM数据矩阵。然后你可以简单地将你的数据作为数据源与控件绑定。



然而,一个锯齿状的数组无论如何都不适合,主要是因为数组没有允许添加元素。您可以绑定列表列表等数据。即使您可以使用阵列作为数据源填充列表列表,但它不太可能是最佳解决方案。如果您首先将网站划分为列表列表,那会好得多。我希望你这很容易。



-SA
You can always feel in an instance of DataGridView with data in any form you want, cell by cell. If you need to fill in the jagged array, by concern would be: jagged array does not assume that there is a column-to-column one-to-one correspondence of the element of the inner array, so it's not clear what's the point of arranging the data in a grid view, where this correspondence is visually pronounced: the user reasonably tends to think that the column represents identical attributes of different data element arranges horizontally. Besides, the jagged nature of the array assumes that you will have any number of empty cells anyway.

If you still want to deal with your data as is and thing that my concerns are not important to you (well, I still don't know your goals), and you don't want to populate the control with data on cell-to-cell basis, one of the possible ideas is to fill in the arrays with null values to have an NxM matrix of data. Then you could simply bind your data as the data source with the control.

But then, a jagged array is not suitable structure anyway, mostly because the arrays don't allow adding elements. You can bind the data like list of lists. Even though you can populate list of list using your array as a data source, it's unlikely the best solution. It would be much better if you scribe the Web sites into an list of lists in first place. I hope it's easy for you.

—SA


这篇关于C#设计在屏幕上显示数据的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆