Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I want to auto enter values in many controls after data-binding.
The problem is explained in the comments in the code block.
I know that my approach wont work but I am out of ideas.

how to??

this is my code so far:

C#
private void dgvMydata_CellClick(object sender, DataGridViewCellEventArgs e)
       {
           List<Control> AllFoundControls = new List<Control>();

           int rowIndex = dgvMydata.CurrentRow.Index;
           foreach (DataGridViewCell cell in dgvMydata.CurrentRow.Cells)
           {
               if (cell.Value != null || string.IsNullOrEmpty(cell.Value as string))
               {
                   int columnIndex = cell.ColumnIndex;
                   string cName = dgvMydata.Columns[columnIndex].Name;
                   var fndControl = findControlByTag(cName);
                   if (fndControl != null)
                   {
                       Control foundControl = fndControl;
                       foundControl.Parent.Enabled = true;
                       foundControl.Enabled = true;
                       AllFoundControls.Add(foundControl);

                       string propertyName = "";
                       if (foundControl is TextBox) propertyName = "Text";
                       if (foundControl is CheckBox) propertyName = "CheckState";
                       if (foundControl is ComboBox) propertyName = "SelectedItem";
                       BindField(foundControl, propertyName, tableMydata, cName);//new databindings
                   }
               }

           }
           foreach (Control found in AllFoundControls)// or maybe Do While ????
           {
               //This is what I want to do:
               if (found.Focused)
               {
                   SendKeys.SendWait("{ENTER}");
                   //Wait until nextcontrol has focus( next control is focussed by program,
                   // and not always the next control in AllFoundControls
               }
           }
       }
Posted
Comments
CHill60 24-Mar-13 13:07pm    
What program will be affecting focus while this code is running?
GrooverFromHolland 24-Mar-13 13:41pm    
It is the same form, it is processing the value in the control
and selecting the next control.
[no name] 24-Mar-13 13:17pm    
Personally, I think this is a terrible idea. You are trying to do a bunch of UI related tasks inside a Click event handler. I don't think you would ever get this to work as is. You need to rethink what it is that you are trying to do.
GrooverFromHolland 24-Mar-13 13:46pm    
I know this is a terrible idea. But i do not know of another way to alter previous entered data in a form
and process the input in the same sequence.
[no name] 24-Mar-13 13:52pm    
I think that you would either need to perform this from a background thread or write anther program that enter information into your program. Quite frankly what you are wanting to do does not make much sense.

1 solution

First of all, read ThePhantomUpvoter's comments.
ThePhantomUpvoter wrote:
Quite frankly what you are wanting to do does not make much sense.

I agree with Him.

We do not understand what you're trying to achieve... Why to process through the collection of controls and binds data only on "Enter" key press event? Why to do that in CellClick event?

Probably, your problem is defined as: i do not know how to alter previous entered data in a form and process the input in the same sequence, so... in your case, using do.. while or for (each)... next, changes nothing!

You need to rethink and redesign your application.
 
Share this answer
 
Comments
GrooverFromHolland 24-Mar-13 15:18pm    
It is no option to redesign my application (4000+ lines of code in this form only).
this is what my application is supposed to do:
The user fills in up to 50 controls every value is stored in a database table when finished.
if the user wants to modify a previous assignment the user clicks a button named modify assignment and a datagridview is filled with all previous assignments.
the user clicks in the datagridview to select the assignment to modify.
In this click event all relevant controls get a value from the database.
So far there is no problem!
Now I want to process all controls as if the user would do when the form is first filled.
It is designed that if the first control is handled the next relevant control is automatically focused and so on.
Maciej Los 24-Mar-13 15:30pm    
You need to bind data to the control when control is being focused.
Ian A Davidson 24-Mar-13 17:41pm    
There are definitely problems with this. You shouldn't need to make your application give focus to every control in order to get the data. What a terrible user experience you're going to present to the user! What if they want to modify several items, they enter one piece of data, then suddenly find the focus flash through all the fields and, presumably, end on the last one. Now they need to click back to another item, and it happens all over again. And apart from that, you say "process all controls as if the user would do when the form is first filled". This is a GUI application - your application should never assume that the user will visit EVERY field or in the order you're presenting it to them. It's not a command line application where you ask for each item sequentially.
Seriously, you need to redesign this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900