本文介绍了如何将ObservableCollection绑定到datagriview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在尝试将一个集合绑定到datagriview。 private void loadGridview(ObservableCollection < cLabTest > list) { Gridview.DataSource =列表; } i我确实在调试时调用了我的函数。 但它不起作用。 我基于这篇文章: http:// tech.pro/tutorial/776/csharp-tutorial-binding-a-datagridview-to-a-collection [ ^ ]。 它说: 将它绑定到DataGridView非常简单。只需将DataGridView的DataSource属性设置为List和,DataGridView将自动为Car对象中的每个属性创建列。 我是新手,我读了一些文章,但它们对我来说太高了。 有人可以告诉我为什么请。解决方案 验证autogeneratecolumns属性值。默认情况下,在这种情况下应该是真的 原因,我的代码没有解决,就是我忘了编写方法{set; get;} for class的属性 最近,我注意到调试仍然显示我的对象有值事件我没有声明{set; get;}; 谢谢'Rajesh Varma Buddaraju'和每个人都帮我。 非常感谢。 我试过这个样子,它对我来说很好。 private void Form1_Load(object sender,EventArgs e) { ObservableCollection < 汽车 > cars = new ObservableCollection < Car > (); cars.Add(新车(福特,野马,1967年)); cars.Add(新车(谢尔比AC,眼镜蛇,1965年)); cars.Add(新车(雪佛兰,Corvette Sting Ray,1965年)); loadGridview(cars); } private void loadGridview(ObservableCollection < Car > list) { dgGrid.DataSource = list; } public 类 Car { private string _make; private string _model; private int _year; public Car( string make, string 模型, int year) { _make = make; _model = model; _year =年; } public string 赚取 { get { return _make; } set {_ make = value ; } } public string 模型 { get { return _model; } set {_ model = value ; } } public int 年 { 获取 {返回 _year; } set {_ year = value ; } } } 我为你准备了整个代码。试试一次。 I am trying to bind a colletion to datagriview.private void loadGridview(ObservableCollection<cLabTest> list) { Gridview.DataSource = list; }i surely that i did call my function while i debugged.But it did not works.I based this article: http://tech.pro/tutorial/776/csharp-tutorial-binding-a-datagridview-to-a-collection[^]. it said:"Binding this to a DataGridView is painfully easy. Simply set the DataSource property of the DataGridView to the List" and "the DataGridView will automatically create columns for each property in the Car object".I am newbie, i read some article but they are too high for me.Could someone tell me why please. 解决方案 Verify the autogeneratecolumns property value. Default it should be true in this scenarioThe reason ,my code did not worked out , that is i forgot to write the method {set; get;} for class's propertiesRecently, i noticed the debug still show out that my object have values event i didn't declare {set;get;};Thank 'Rajesh Varma Buddaraju' and everyone for helping me.Thank a lots.I tried the sample, It is working fine for me.private void Form1_Load(object sender, EventArgs e) { ObservableCollection<Car> cars = new ObservableCollection<Car>(); cars.Add(new Car("Ford", "Mustang", 1967)); cars.Add(new Car("Shelby AC", "Cobra", 1965)); cars.Add(new Car("Chevrolet", "Corvette Sting Ray", 1965)); loadGridview(cars); } private void loadGridview(ObservableCollection<Car> list) { dgGrid.DataSource = list; }public class Car { private string _make; private string _model; private int _year; public Car(string make, string model, int year) { _make = make; _model = model; _year = year; } public string Make { get { return _make; } set { _make = value; } } public string Model { get { return _model; } set { _model = value; } } public int Year { get { return _year; } set { _year = value; } } }I placed the entire code for you. Try it once. 这篇关于如何将ObservableCollection绑定到datagriview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-04 02:38