Solved

I need help developing a Custom DataGridView control in C#

Posted on 2009-04-14
2
704 Views
Last Modified: 2013-12-17
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.

Code Snippet
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
    dataGridView1.BeginEdit(false);
    if (e.ColumnIndex == 0)// the combobox column index
    {
        if (this.dataGridView1.EditingControl != null
            && this.dataGridView1.EditingControl is ComboBox)
        {
            ComboBox cmb = this.dataGridView1.EditingControl 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:
http://knol.google.com/k/dave-andrews/creating-custom-controls-with-c-net/617czubfqh7e/2#

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.

Thanks!

Matt





0
Comment
Question by:mattjankowski
2 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 24148382
It sounds like you could use the DataGridView.EditMode:

DataGridView.EditMode
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.editmode.aspx

DataGridViewEditMode Enumeration
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridvieweditmode.aspx

EditOnEnter
---------------
Editing begins when the cell receives focus. This mode is useful when pressing the TAB key to enter values across a row, or when pressing the ENTER key to enter values down a column.  
0
 

Author Closing Comment

by:mattjankowski
ID: 31569989
Use of the DataGridView.EditMode resolved my issue, saving me a ton of programming. Thanks!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question