lisa-marie mueller

lisa-marie mueller

bullet pont

WinForms DataGrid link

January 31, 2020

Laptop with code on screen, party hat, whistle, blue As you dive into creating plug-ins, you will most likely want to also utilize a user interface to show information or allow selections. WinForms allows you to provide pop-up interfaces to do just that. The benefit of using WinForms is that there are pre-defined interface elements - like buttons, drop-down menus, and more - that you can drag and drop into your form. You don’t have to code the visual features of your interface, you simply code the function.


I’m not going to cover how to get started with WinForms because it is well documented and there are lots of excellent resources. I’m going to jump into after you have created your WinForm with a DataGrid. Generally, you will also want at least two buttons as part of your interface - one to allow the user to accept and run your plug-in, the other to allow the user to cancel. Once you have created your WinForm, you will have a .cs file for your WinFrom and you will have a .cs file for the plug-in within the project. Generally, best practices recommend to have the code for your program in the program’s .cs file and to only have the methods you need for the interface within the WinForms .cs file.

I am going to walk through how I am using a DataGrid for the stand-alone Rename Interior Elevations plug-in I started. First, I will discuss the WinForms .cs file. When you create a WinForm, certain files are automatically created and updated. The class for the WinForms .cs file, which will contain the methods you write to add functionality to your interface, is a “partial” class. The other part of the "partial" class is the autogenerated designer file.

We want to think about what we are displaying and how to display it. The goal is to have a two-column chart that displays the old name of the interior elevation and the new name of the interior elevation. This way the user can review the proposed changes and either accept or cancel the rename action. To display the names for our Rename Interior Elevation tool, we are using a list of key-value pairs. These pairs are taken in as a parameter from our plug-in’s .cs file. We can write a method that takes these pairs of data and separates them into the OldName and the NewName properties. Then a second method converts each row to the displayable data type.

To have our dialog box display, we need a constructor for our interface. We will call this RenameElevationsDialog. We already discussed that we want to display our information in a table. For our DataGrid, we will want to create two columns. We defined this in the properties menu of the DataGrid so we do not need to define this in the code. We called one column “elevationName” and the other “renameTo”. In our code, we need to assign the appropriate field to bind to each column. To achieve this, we assign the property as a string. For example, we bind the OldName property by assigning the string “OldName” to the DataPropertyName of elevationName.

Then all we have left to add are cancel and accept buttons. To generate these methods, simply double click on the respective button, or you can assign these event handlers on the button’s properties. If you are using the methods, you can then fill in what each method does.

execute method

Now we have to add a few things to our execute method which is located in the plug-in’s .cs file. First, we need to create a list that will save the new names of the interior elevations to display. In the snippet below, we do this in line 1 where we create the List of KeyValuePairs and call it newNames. When we cycle through our interior elevation views, we add the new name and the old name to our list. In the snippet below, you can see this in line 11.

And those are the changes we had to make to add a dialog box with WinForms. All the code is available on my GitHub account and you can download the Rename Interior Elevations plug-in in the releases section. Next week I will take a look at using a ComboBox in WinForms (similar a SelectElement in HTML).


If you want to learn to code and don’t know where to start check out my posts about Steps to Learn to Code [for architects and designers] Part 1 and Part 2.

if you missed it:

Part 1: filtered element collector [c#]

Part 2: finding centroids and considering exceptions

Part 3: ViewFamilyTypeId

Part 4: ViewPlanId and Levels

Part 5: phases & goal #1 complete [includes GitHub link to the release]

Part 6: view tempaltes

Part 7: resizing CropBoxes

Part 8: creating FilledRegions & Goal #2 Complete [includes GitHub link to release]

Part 9: coordinate system utilities

Part 10: rename views & goal #3 complete [includes GitHub link to release]

Revit API Docs