Click here to Skip to main content
13,056,229 members (71,252 online)
Rate this:
Please Sign up or sign in to vote.
See more: , +
I have Build a user control with textBox and dynamic code generated DataGridView
I have set all its Properties and working according to my requirement well. I just want to click the textbox and it will display help as datagridview and after lost focus of textbox to hide datagridview.

But the problem is that DatagridView data are not Coming above the other parent form control. All its event working fine .How to bring it on front of the screen.
usercontrol textbox working and display fine ?

I am posting my code below Plz Help me ?
only i am one step behind my user control..

namespace textGrid
    public partial class CustText : UserControl
        #region Variable
        DataTable dtfil = new DataTable();
        DataTable dtstr = new DataTable();
        BindingSource bSource = new BindingSource();
        Point GvPos;
       DataGridView gv = new DataGridView();
        Boolean Req = false, Act_Code = true;
        string _SelVal;
        string _DisplayMember;
        string _ValueMember;
        string _text;
        string filterFld = "";
            string valfld = "";
            int num = 0;
        public CustText()
            this.gv.Visible = false;
            gv.TabIndex = TabIndex;
           // gv.Enabled = false;
           // gv.Enter += new System.EventHandler(this.gv_Enter);
            this.gv.Size = new System.Drawing.Size(180, 140);
            gv.MouseClick += new System.Windows.Forms.MouseEventHandler(this.gv_MouseClick);
            gv.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.gv_MouseDoubleClick);
            gv.KeyDown += new System.Windows.Forms.KeyEventHandler(this.gv_KeyDown);
            gv.Leave += new System.EventHandler(this.gv_Leave);
            gv.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.gv_KeyPress);
            gv.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
        //unrelated code removed
Posted 19-May-10 21:14pm
Updated 20-May-10 19:59pm
William Winner 21-May-10 11:53am
I don't understand the comment you left on my answer. this.Controls.SetChildIndex works perfectly for me. Maybe I didn't understand your question. I don't know why you would have controls on your form that would be in the same location as a DataGridView anyway. Is your problem that you have multiple controls on a form that are over that form? Or is it that you have two forms and you want to bring one of the forms to the front of your screen? If my answer didn't help, then I must not be understanding your question.
AnupOmi 24-May-10 5:42am
Actually i want to made a UserControl or Component library dll with TextBox and DataGridView.On user Click on textBox i want to show datagridview ,So that i can use it as a suggestion or help to client user to see more details related to fill some name or codeID.Bcz ComboBox shows only one field at a time so instead of ComboBox i decided to use DataGridView.May be this.Controls.SetChildIndex is working perfectly for You But i am not getting the required result to Display the datagridView over other parent form control.Any help greatly accepted..
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Next time, only provide the code that is directly related to your question. The majority of that code has nothing to do with your question.

Now, to answer your question, use

after you add it to the controls collection. This will tell the UserControl to draw it first before anything else.

Also, as an FYI, it is not good practice to call
DataGridView gv = new DataGridView();

outside of a method. That should really be placed inside the contstructor.

You can declare gv globally still, but you shouldn't call new outside of a method.
AnupOmi 21-May-10 1:14am
After adding your suggestion I get the same situation
So where to add? If i manually set the parent form control property to send Back than GridView is displaying properly for the first time. I have to use this user control for many times in a page. So plz give me some suggestion. Thanks for your FYI suggestion
Rate this: bad
Please Sign up or sign in to vote.

Solution 3


to clarify:

To get control to front just add the line


after making new instance;

DataGridView gv = new DataGridView();

This should look like this:

DataGridView gv = new DataGridView();

this should help.

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Try setting the zindex of the dynamic control.
Search on msdn to read / learn more about this property.
William Winner 20-May-10 12:30pm
Reason for my vote of 1
what kind of control directly exposes the ZIndex?

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170728.2 | Last Updated 3 Apr 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100