I want all the DataGridViews in my program to have nearly identical appearance behavior. What strategy accomplishes this in a way that maximizes code reuse and is easy to maintain? I think that I need some help expanding my fundamental understanding of how controls work in C#.
Here is a more specific description of my issue I have a rich client C# application with multiple datagridviews. All the datagridvews need to have nearly the same behavior and the same appearance. I have tried a few things to reduce the amount of duplicate code.
Initially I created a "Visual Appearance" class to handle the look and feel of row headers etc. I use it by sending the datagridview through a static "FormatDataGridview" method. That worked for awhile, but I began to notice I needed to doubleclick cells to access the drop downs. I only want my users to need to single click. I found code on the internet which gives me the behavior I want:
When user single clicks on a cell, DataGridView.CellEnter event is raised. Use the DataGridView.BeginEdit() method to put the current selected cell into EditMode.
private void dataGridView1_CellEnter(ob
ject sender, DataGridViewCellEventArgs e)
if (e.ColumnIndex == 0)// the combobox column index
gControl != null
Control is ComboBox)
ComboBox cmb = this.dataGridView1.Editing
Control as ComboBox;
cmb.DroppedDown = true;
I want all my DataGridView controls to have this behavior but I do not want to write this code 10x, so I put it into my local control library. I wont need to rewrite it, but I will need to manually add a reference to this function for each datagridview. My thought is that this approach will be difficult to maintain. I also thought about inheritance, but I was not sure how this would work in the context of controls. ( I did something like this before with unsatisfactory results)
Feeling ambitius, I decided to see if writing a custom control might accomplish what I want. So I went on the web and found a tutorial that I felt I could follow. This one seemed user friendly:
When I started to add my behaviors I found that all the methods called by the control are static, and the code I need to add is not static. So I feel stymied.
Please let me know if more information and/or code snippents would help you answer my questions.