Click here to Skip to main content
15,887,376 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!

I am trying to make a class wich i can reuse 100% on all my forms and datagridviews.

The function of this class is to generate a number of textboxes, put them on top of datagridview and make them work as filters against bindingssource.

This i the code so far:


This bit works fine!
C#
public static void SettOppDGW(DataGridView dgw, bool kunles=true, bool ErClickAble = true,bool FilterFelt = false)
            {
                dgw.ReadOnly = kunles;
                dgw.AllowUserToAddRows = !kunles; 
                dgw.AllowUserToDeleteRows=!kunles;
                dgw.ClearSelection();
                dgw.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dgw.MultiSelect = false;


                dgw.RowHeadersVisible = !kunles;
                dgw.AlternatingRowsDefaultCellStyle.BackColor = Color.LightCyan;

                dgw.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
                dgw.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;

                if (kunles)
                {
                    dgw.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                    dgw.CellBorderStyle = DataGridViewCellBorderStyle.SingleHorizontal;
                }


                if (ErClickAble)
                {
                    dgw.MouseMove += new MouseEventHandler(dgwMouseOver);
                    dgw.MouseLeave +=new EventHandler(dgwMouseExit);
                }

                if (dgw.Name == "dgwForbedringstabell") 
                {
                    dgw.CellFormatting += new DataGridViewCellFormattingEventHandler(dgwMinSideForbedringCellFormatting);
                    dgw.DefaultCellStyle.WrapMode = DataGridViewTriState.True; 
                }


                if (FilterFelt)
                {

                    int TextBoxHeight = 25;
                    int VenstreAkk = 0;

                    dgw.Height = dgw.Height - TextBoxHeight;
                    dgw.Location = new Point(dgw.Location.X, dgw.Location.Y + TextBoxHeight);
                    Form f = dgw.Parent.FindForm();

              
                     foreach (DataGridViewColumn c in dgw.Columns)
                    {
                        if (c.Visible)
                        {
                            TextBox a = new TextBox();
                            a.Name = "AutoFilterTextBox " + c.DataPropertyName.ToString();
                            a.Height = TextBoxHeight-1;
                            a.Location = new Point(dgw.Location.X + VenstreAkk, dgw.Location.Y - TextBoxHeight+2);
                            a.Width=c.Width;
                            a.BackColor = Color.Yellow;
                            a.Validated+=new EventHandler(Filtrer);
                            VenstreAkk = a.Location.X + a.Width;
                            f.Controls.Add(a);
                        }
                    }


                }
            }



This bit i have trouble with. I have made an filterstring to put in the bindingssource, but i need to make this go programtically as i dont want to have any constants in this code

C#
private static void Filtrer(object sender, EventArgs e)
            {
                TextBox tb = (TextBox)sender;
                Form f = tb.Parent.FindForm();
                string FilterString = "";
                string verdi = "";

                foreach (Control c in f.Controls)
                {
                    foreach (Control d in c.Controls)
                    {
                        string a = d.Name;
                    }
                    if (c.Name.Length > 17)
                    {
                        if (c.Name.Substring(0, 17) == "AutoFilterTextBox")
                        {
                            if (c.Text != "")
                            {
                                verdi = c.Text;
                                if (!Snacks.IsInteger(verdi))
                                {
                                    verdi = "'" + c.Text + "'";
                                }

                                FilterString = FilterString + c.Name.Substring(17) + "=" + verdi + ";";
                            }
                        }
                    }

                }
Posted
Comments
luisnike19 8-Aug-11 0:22am    
and the problem is?

You might want to look at this article [^]it does not use text boxes but it will give you an idea what you will need to do.

Note that this technique ony works with datatables as a datasource. List<> does not work
 
Share this answer
 
 
Share this answer
 

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