Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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;
        #endregion

        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.BringToFront();
            gv.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            Controls.Add(gv);
       
            InitializeComponent();
        }

        //unrelated code removed
}
Posted
Updated 20-May-10 19:59pm
v4
Comments
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.
OmiAshish 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..

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
C#
this.Controls.SetChildIndex(gv,0);

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
C#
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.
 
Share this answer
 
Comments
OmiAshish 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
Hi

to clarify:

To get control to front just add the line

gv.BringToFront();

after making new instance;

DataGridView gv = new DataGridView();

This should look like this:

C#
DataGridView gv = new DataGridView();
gv.BringToFront();


this should help.

regards
 
Share this answer
 
v2
Try setting the zindex of the dynamic control.
Search on msdn to read / learn more about this property.
 
Share this answer
 
v2
Comments
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)



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