我正在使用vs2010的c#windows应用程序和一个名为“clients”的表的本地数据库。在我的一个表单中,我正在使用bindingNavigator来编辑和显示该表的条目。
我希望能够创建一个“查找”按钮(与文本框结合),以便能够通过插入id的数据库字段来访问任何记录(类似于报表查看器中的查找功能) )。这可能吗?怎么样?提前致谢
答案 0 :(得分:1)
通过在绑定导航器和点击事件中创建按钮和文本框来解决:我写道:
int pos = this.clientBindingSource.Find("id", toolStripTextBox1.Text);
this.clientBindingSource.Position = pos;
答案 1 :(得分:0)
对于以类似方式执行搜索的人来说,这可能是一个有用的示例:
private void searchButton_Click(object sender, EventArgs e)
{
//Objects to append to the Search form
Form searchForm = new Form();
Label searchLabel = new Label();
TextBox searchBox = new TextBox();
Button okSearchButton = new Button();
Button cancelSearchButton = new Button();
//Properties
searchForm.Text = "Search";
searchLabel.Text = "Search For:";
searchBox.Text = "";
okSearchButton.Text = "Search";
cancelSearchButton.Text = "Cancel";
//Button actions
cancelSearchButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
okSearchButton.DialogResult = System.Windows.Forms.DialogResult.OK;
searchForm.CancelButton = cancelSearchButton;
searchForm.AcceptButton = okSearchButton;
//Form control placement
searchLabel.SetBounds(9, 20, 372, 13);
searchBox.SetBounds(12, 36, 372, 20);
okSearchButton.SetBounds(228, 72, 75, 23);
cancelSearchButton.SetBounds(309, 72, 75, 23);
searchLabel.AutoSize = true;
searchBox.Anchor = searchBox.Anchor | AnchorStyles.Right;
okSearchButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
cancelSearchButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
//Set form properties
searchForm.ClientSize = new Size(400, 110);
searchForm.MinimizeBox = false;
searchForm.MaximizeBox = false;
//Add controls to the form
Control[] formControl = new Control[] { searchLabel, searchBox, okSearchButton, cancelSearchButton };
searchForm.Controls.AddRange(formControl);
//Show the form
DialogResult dialogResult = searchForm.ShowDialog();
if (searchForm.DialogResult == DialogResult.OK)
{
int pos = tableNameTestBindingSource.Find("LastName", searchBox.Text);
tableNameTestBindingSource.Position = pos;
}
}