简体   繁体   中英

how to store datagridview name in a variable c#

I used c# to start a project. In main form there is a datagridview, and I used the name of this datagrid in project repeatedly.

now I want to put the name of this datagrid in a variable and use that variable instead of datagrid name, so in the future, if datagrid target change,I just need to change the variable.

I try somthing like this:

string myDataGridName = originalDataGridName

myDataGridName.DataSource = null;//clear dataGrid

but this is wrong. then I search to find an answer and just find this link: enter link description here this link answer in .net, and I don't know about that... so please answer in c#

thank you

(sorry about my bad English)

It's better to store a reference to the datagrid object itself. Then you can just change this in one place like you want. The example you posted is actually already very close.

DataGridView myDataGrid = originalDataGridViewObject;
myDataGrid.DataSource = null; //clear dataGrid

Of course, depending on your use case, this may not really be needed. In Visual Studio if you rename an object in the designer the associated code should be automatically renamed as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM