Click here to Skip to main content
15,903,719 members
Home / Discussions / C#
   

C#

 
AnswerRe: Presentation Help..... Pin
Christo66718-May-10 23:57
Christo66718-May-10 23:57 
QuestionComboBox selection generating list for another comboBox question Pin
mprice21418-May-10 6:24
mprice21418-May-10 6:24 
AnswerRe: ComboBox selection generating list for another comboBox question Pin
William Winner18-May-10 7:18
William Winner18-May-10 7:18 
AnswerRe: ComboBox selection generating list for another comboBox question Pin
Henry Minute18-May-10 7:23
Henry Minute18-May-10 7:23 
GeneralRe: ComboBox selection generating list for another comboBox question Pin
mprice21418-May-10 8:18
mprice21418-May-10 8:18 
GeneralRe: ComboBox selection generating list for another comboBox question [modified] Pin
Henry Minute18-May-10 8:40
Henry Minute18-May-10 8:40 
GeneralRe: ComboBox selection generating list for another comboBox question Pin
mprice21418-May-10 10:43
mprice21418-May-10 10:43 
GeneralRe: ComboBox selection generating list for another comboBox question Pin
Henry Minute18-May-10 11:43
Henry Minute18-May-10 11:43 
OK I have looked at your picture and I have to say that I am surprised that you can even reach the keyboard, still let's press on.

The

ComboBox cb = e.Control as ComboBox;


line is not instantiating a new ComboBox, it is declaring a variable of the ComboBox type (on the left hand side of the '=') and making it reference (point to) the ComboBox in the cell that you are currently editing. The event that is being handled is the EditingControlShowing event and the DataGridViewEditingControlShowingEventArgs passed to it contain a reference to the editing control being used (e.Control). Because the column we are talking about is a DataGridViewComboBoxColumn, when you start to edit one of its cells, the DataGridView control pops up a real live ComboBox for you to do the edit with (hence the as ComboBox part). (If it were a DataGridViewTextBoxColumn the DGV would give you a TextBox or if it was a DataGridViewCheckBoxColumn the DGV would give you a CheckBox.)

So the line is saying to the compiler "Please reserve enough space to hold a reference to a ComboBox which I'll be calling 'cb' and put a copy of the pointer from e.Control in there. Thank you!" (please note that it is a very polite line). Basically it just makes it easier to refer to cb.SelectedIndexChanged for example, rather than:
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    if ((ComboBox)e.Control != null)
    {
        // first remove event handler to keep from attaching multiple:
        ((ComboBox)e.Control).SelectedIndexChanged -= cb_SelectedIndexChanged;
        // now attach the event handler
        ((ComboBox)e.Control).SelectedIndexChanged += cb_SelectedIndexChanged;
    }
}

which we would have to do otherwise. I hope that you'll agree that the first version is easier to read than this one.

If it helps, think of cb as being a 'magic' PhotoCopy of the ComboBox from the cell you are editing. It is magic because any thing you do to the copy also happens to the original. So when the code says to hook up column1DataGridViewTextBoxColumn_SelectedIndexChanged to the copy it is also hooking it up to the real one in your DataGridView

When dataGridView1_EditingControlShowing ends, cb is thrown away. It is what is referred to as a Local Variable because it is local to the method it is declared in.

If you select the DataGridView on your form, go to the events page of the Properties Window and double-click on the EditingControlShowing you will be able to paste the code below directly into the empty handler that VS provides for you.
and so long as you haven't changed the name of your SelectedIndexChanged handler since your first post in this thread, all should work with no changes.

ComboBox cb = e.Control as ComboBox;

if (cb != null)
{
    // first remove event handler to keep from attaching multiple:
    cb.SelectedIndexChanged -= column1DataGridViewTextBoxColumn_SelectedIndexChanged;
    // now attach the event handler
    cb.SelectedIndexChanged += column1DataGridViewTextBoxColumn_SelectedIndexChanged;
}


I really do hope I made it clearer, not muddier.

Good luck! Smile | :)
Henry Minute

Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
Why do programmers often confuse Halloween and Christmas?
Because 31 Oct = 25 Dec.

GeneralRe: ComboBox selection generating list for another comboBox question Pin
mprice21418-May-10 12:10
mprice21418-May-10 12:10 
GeneralRe: ComboBox selection generating list for another comboBox question Pin
mprice21418-May-10 17:29
mprice21418-May-10 17:29 
GeneralRe: ComboBox selection generating list for another comboBox question Pin
Henry Minute19-May-10 1:15
Henry Minute19-May-10 1:15 
GeneralRe: ComboBox selection generating list for another comboBox question Pin
mprice21419-May-10 3:49
mprice21419-May-10 3:49 
GeneralRe: ComboBox selection generating list for another comboBox question [modified] Pin
Henry Minute19-May-10 4:22
Henry Minute19-May-10 4:22 
GeneralRe: ComboBox selection generating list for another comboBox question Pin
mprice21419-May-10 4:43
mprice21419-May-10 4:43 
GeneralRe: ComboBox selection generating list for another comboBox question Pin
Henry Minute19-May-10 5:07
Henry Minute19-May-10 5:07 
GeneralRe: ComboBox selection generating list for another comboBox question Pin
mprice21419-May-10 5:29
mprice21419-May-10 5:29 
GeneralRe: ComboBox selection generating list for another comboBox question Pin
mprice21419-May-10 18:46
mprice21419-May-10 18:46 
GeneralRe: ComboBox selection generating list for another comboBox question Pin
Henry Minute20-May-10 1:35
Henry Minute20-May-10 1:35 
GeneralRe: ComboBox selection generating list for another comboBox question [modified] Pin
mprice21420-May-10 3:44
mprice21420-May-10 3:44 
GeneralRe: ComboBox selection generating list for another comboBox question Pin
Henry Minute20-May-10 5:23
Henry Minute20-May-10 5:23 
GeneralRe: ComboBox selection generating list for another comboBox question Pin
mprice21426-May-10 19:10
mprice21426-May-10 19:10 
AnswerRe: ComboBox selection generating list for another comboBox question Pin
William Winner18-May-10 7:31
William Winner18-May-10 7:31 
GeneralRe: ComboBox selection generating list for another comboBox question Pin
mprice21418-May-10 7:58
mprice21418-May-10 7:58 
GeneralRe: ComboBox selection generating list for another comboBox question Pin
William Winner18-May-10 11:12
William Winner18-May-10 11:12 
GeneralRe: ComboBox selection generating list for another comboBox question Pin
mprice21418-May-10 12:16
mprice21418-May-10 12:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.