自己开发小软件对工作,加了个搜索医嘱名称,再显示颜色添加,回车索引跳行搜索,自用代码
程序员文章站
2024-01-06 20:28:58
private void txtYZName_TextChanged(object sender, EventArgs e) { if (tabControl1.SelectedTab == tabControl1.TabPages["YZPage"]) { if (SearchYZ == false) { #region 恢复原先......
private void txtYZName_TextChanged(object sender, EventArgs e)
{
if (tabControl1.SelectedTab == tabControl1.TabPages["YZPage"])
{
if (SearchYZ == false)
{
#region 恢复原先没颜色的状态
if (txtYZName.Text.Equals(""))//&& (DataGridView2.Rows.Count == DGV_YZ_Count)
{
// 恢复之前行的背景颜色为默认的白色背景
for (int i = 0; i < beforeMatchedRowIndex.Count; i++)
{
DataGridView2.Rows[beforeMatchedRowIndex[i]].DefaultCellStyle.BackColor = System.Drawing.Color.White;
}
return;
}
// 恢复之前行的背景颜色为默认的白色背景
for (int i = 0; i < beforeMatchedRowIndex.Count; i++)
{
DataGridView2.Rows[beforeMatchedRowIndex[i]].DefaultCellStyle.BackColor = System.Drawing.Color.White;
}
}
#endregion
#region 添加状态
//如果是刷新了搜索,如果是空字符,就退出
if (txtYZName.Text.Equals(""))
{
return;
}
// Linq模糊查询
IEnumerable<DataGridViewRow> enumerableList = DataGridView2.Rows.Cast<DataGridViewRow>();
Matchedlist = (from item in enumerableList
where item.Cells["YZMC"].Value.ToString().IndexOf(txtYZName.Text) >= 0
select item).ToList();
if (Matchedlist.Count > 0)
{
beforeMatchedRowIndex.Clear();
for (int i = 0; i < Matchedlist.Count; i++)
{
// 查找匹配行高亮显示
int matchedRowIndex = Matchedlist[i].Index;
DataGridView2.Rows[matchedRowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.Yellow;
this.beforeMatchedRowIndex.Add(matchedRowIndex);
}
}
k = 1;
SearchYZ = false;
#endregion
}
else
{
MessageBox.Show("请先定位到医嘱表,再输入医嘱名称");
return;
}
}
private void txtYZName_KeyDown(object sender, KeyEventArgs e)
{
//if (e.KeyCode == Keys.Enter)
//{
// // 恢复之前行的背景颜色为默认的白色背景
// if (k <= Matchedlist.Count)
// {
// if((k-2) >=0)//判断第一次不消颜色
// {
// DataGridView2.Rows[Matchedlist[k-2]].Selected = false;
// }
// DataGridView2.Rows[Matchedlist[k-1]].Selected = true; //加颜色
// DataGridView2.CurrentCell = DataGridView2.Rows[Matchedlist[k - 1]].Cells["YZMC"];//设置成当前选框
// if(k== Matchedlist.Count)//到了最大值
// {
// k++;
// return;
// }
// k++;
// }
// if (k > beforeMatchedRowIndex.Count)
// {
// DataGridView2.Rows[beforeMatchedRowIndex[k - 2]].Selected = false;
// k = 1;
// }
//}
if (e.KeyCode == Keys.Enter)
{
if (SearchYZ == false)
{
if (txtYZName.Text != "")
{
// 恢复之前行的背景颜色为默认的白色背景
if (k <= Matchedlist.Count && Matchedlist.Count > 0)
{
//if ((k - 2) >= 0)//判断第一次不消颜色
//{
// DataGridView2.Rows[Matchedlist[k - 2]].Selected = false;
//}
//DataGridView2.Rows[Matchedlist[k - 1]].Selected = true; //加颜色
DataGridView2.CurrentCell = Matchedlist[k - 1].Cells["YZMC"];//设置成当前选框
if (k == Matchedlist.Count)//到了最大值
{
k++;
return;
}
k++;
}
if (k > Matchedlist.Count)
{
//DataGridView2.Rows[beforeMatchedRowIndex[k - 2]].Selected = false;
k = 1;
}
}
}
}
}
本文地址:https://blog.csdn.net/babyruirui4/article/details/110630925
上一篇: 表查询操作