获取datagridview使用哪个绑定源 [英] get which bindingsource is used by datagridview

查看:83
本文介绍了获取datagridview使用哪个绑定源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!

我正在尝试建立一个我可以在所有表​​单和datagridviews上重用100%的类.

此类的功能是生成许多文本框,将它们放在datagridview的顶部,并使它们充当针对bindingssource的过滤器.

到目前为止,这是我的代码:


这一点工作正常!

 公共 静态  void  SettOppDGW(DataGridView dgw,布尔 kunles =  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.AutoSizeRowsMo​​de = DataGridViewAutoSizeRowsMo​​de.DisplayedCells;

                如果(kunles)
                {
                    dgw.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                    dgw.CellBorderStyle = DataGridViewCellBorderStyle.SingleHorizo​​ntal;
                }


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

                如果(dgw.Name == " 跨度>)
                {
                    dgw.CellFormatting + =  DataGridViewCellFormattingEventHandler(dgwMinSideForbedringCellFormatting);
                    dgw.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
                }


                如果(FilterFelt)
                {

                     int  TextBoxHeight =  25 ;
                     int  VenstreAkk =  0 ;

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

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


                }
            } 




这一点我有麻烦.我已经制作了一个过滤字符串以放入bindingssource中,但是我需要以编程方式进行此操作,因为我不想在此代码中包含任何常量

 私有 静态  void  Filtrer(对象发​​送者,EventArgs e)
            {
                TextBox tb =(TextBox)发送器;
                形式f = tb.Parent.FindForm();
                字符串 FilterString = " ;
                字符串 verdi = " ;

                 foreach (控件c  in  f.Controls)
                {
                     foreach (控件d  in  c.Controls)
                    {
                        字符串 a = d.名称;
                    }
                    如果(c.Name.Length >   17 )
                    {
                        如果(c.Name.Substring( 0  17 )== " )
                        {
                            如果(c.Text!= " )
                            {
                                verdi = c.Text;
                                如果(!Snacks.IsInteger(verdi))
                                {
                                    verdi = "  + c.Text + " '";
                                }

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

                } 

解决方案

您可能想看一下^ ]它不使用文本框,但可以让您大致了解所需要做的事情.

请注意,此技术只能将数据表用作数据源.列表<>不起作用


我为 DataGridView过滤器弹出窗口 [ ^ ]


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!

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

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 + ";";
                            }
                        }
                    }

                }

解决方案

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


I vent for DataGridView Filter Popup[^]


这篇关于获取datagridview使用哪个绑定源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆