Reputation: 33
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)
Upvotes: 0
Views: 1152
Reputation: 1072
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.
Upvotes: 2