且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在C#中从另一个类访问Datagridview?

更新时间:2023-01-08 19:15:07

如果它是一个asp .net应用程序,我们有代码,所以你不能使用它,但如果你做一个用户控件并把你的gridview放在那里,有一种方法可以使用它。您可以在项目中的任何位置注册和使用它。
If It is an asp.net application, we have code behind so, you can not use it, but there is a way to use it if you make a user control and put your gridview in that. you can register and use it any where you want in your project.


您可以做的是

只需在类中创建一个方法,将gridview对象传递给参数并尝试使用该类中方法内的对象。这将是有意义的,因为您可以操作或对网格进行操作,其中数据显示在屏幕上。



如果您尝试访问gridview一个带有公共变量的类,您将获得一个Gridview对象的新实例。这是没用的。



我希望你理解。
what you can do is
just create a method in the class passing the gridview object as the parameter and try to use the object inside the method in that class. this will make some sense as you can manipulate or do something with the grid, in which the data is present on the screen.

if you try to access the gridview from a class with public variable, you will just get a new instance of the Gridview object. which is of no use.

i hope you understood.