我们当前使用FoxPro制作的软件获得了非常好的功能:当我们在网格上键入内容时,搜索功能会自动调用。当用户在datagridview上输入内容时开始搜索
例如:我有一个包含卡车列表的网格。当我点击电网并输入卡车号码时,电网直接进入电网的卡车记录。
我需要在C#中实现它,我怎么能做到这一点?
P.S.在某些情况下,网格可以编辑字段。但是如果它太复杂了,我不会在那些可编辑的网格上实现它。
我们当前使用FoxPro制作的软件获得了非常好的功能:当我们在网格上键入内容时,搜索功能会自动调用。当用户在datagridview上输入内容时开始搜索
例如:我有一个包含卡车列表的网格。当我点击电网并输入卡车号码时,电网直接进入电网的卡车记录。
我需要在C#中实现它,我怎么能做到这一点?
P.S.在某些情况下,网格可以编辑字段。但是如果它太复杂了,我不会在那些可编辑的网格上实现它。
你可以试试你的表单代码中是这样的:
TextBox searchBox = new TextBox();
Timer searchTimer = new Timer();
bool keyPressed = true;
public Form1()
{
InitializeComponent();
yourDataGridView.KeyUp += new KeyEventHandler(dgv_KeyUp);
searchTimer.Interval = 5000;
this.Controls.Add(searchBox);
searchBox.KeyUp += new KeyEventHandler(searchBox_KeyUp);
searchTimer.Tick += new EventHandler(timerTick);
searchTimer.Enabled = true;
}
void searchBox_KeyUp(object sender, KeyEventArgs e)
{
keyPressed = true;
}
void dgv_KeyUp(object sender, KeyEventArgs e)
{
searchBox.Show();
searchBox.Text += e.KeyCode.ToString().ToLowerInvariant();
searchBox.Location = Cursor.Position;
searchBox.Focus();
SendKeys.Send("{Right}");
searchBox.BringToFront();
// Do your sorting of your DataGridView here according to your search box
}
void timerTick(object sender, EventArgs e)
{
keyPressed = !keyPressed;
if (keyPressed)
{
searchBox.Text = "";
searchBox.Hide();
}
}
,将让你弹出“搜索“TextBox
,5秒后会消失。你可以使用框中的文本来执行搜索,因为你认为适合的数据在DataGridView
我所做的是使用我修改的dgv_KeyUp事件的基础将文本发送给我实际的搜索框(并关注它)。 谢谢你的帮助。 – Bestter
您可以与TextChanged事件网格上面的文本框
-------------------
| TextBox here | implement TextChanged event handler for the textbox
-------------------
-------------------
GridView here update this gridview when text changes
-------------------
protected void TextBox1_TextChanged(object sender, EventArgs) //this could be replaced with KeyUp event handlder
{
string truckNo = TextBox1.Text;
var newValues = findTrucksByNo(truckNo);
truckGridView.DataSource = newValues; //rebind items to refresh grid view
}
以上?这就是我得到的。当我进行第一次训练时,每个用户(5或6)都直接在网格中输入内容以搜索某些内容... – Bestter
Doh ...我意识到我已经理解你的问题完全错误...没关系我的回答 –