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 codeprivate 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 + ";"; } } } } 解决方案 这篇关于获取datagridview使用哪个绑定源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-16 04:49