datagridview 中的自动匹配
先看效果:
楼盘那是个txtbox,单击时出现txtbox覆盖原来的名称,然后出现自动匹配的DataGridView;
代码: dataProject 知道匹配的DataGridView ,datacase显示数据的DataGridView
#region 楼盘自动匹配
/// <summary>
/// 自动匹配楼盘
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGrid_Project_Click(object sender, EventArgs e)
{
try
{
if (dataGrid_Project.CurrentRow.Index < 0) return;
if (dataGrid_Project.CurrentRow.Cells["ProjectId"].Value.ToString() == "") return;
int selProjectid = Convert.ToInt32(dataGrid_Project.CurrentRow.Cells["ProjectId"].Value.ToString());
string DGVProjectname = dataGrid_Case.CurrentRow.Cells["DGVProjectName"].Value.ToString();
//LoadProjectData(selProjectid);
dataGrid_Case.CurrentRow.Cells["DGVProjectId"].Value = selProjectid.ToString();
dataGrid_Case.CurrentRow.Cells["DGVProjectName"].Value = dataGrid_Project.CurrentRow.Cells["Col_ProjectName"].Value.ToString();
dataGrid_Case.CurrentRow.Cells["DGVProjectName"].Style.BackColor = Color.White;
txtProjectName.Text = dataGrid_Project.CurrentRow.Cells["Col_ProjectName"].Value.ToString();
panel3.Visible = false;
txtProjectName.Visible = false;
dataGrid_Case.CurrentRow.Cells["DGVProjectId"].ToolTipText = "modify";
for (int j = 0; j < dataGrid_Case.Rows.Count; j++)
{
if (DGVProjectname == dataGrid_Case.Rows[j].Cells["DGVProjectName"].Value.ToString())
{
dataGrid_Case.Rows[j].Cells["DGVProjectId"].Value = selProjectid.ToString();
dataGrid_Case.Rows[j].Cells["DGVProjectName"].Value = txtProjectName.Text;
dataGrid_Case.Rows[j].Cells["DGVProjectName"].Style.BackColor = Color.White;
dataGrid_Case.Rows[j].Cells["DGVProjectId"].ToolTipText = "modify";
}
}
}
catch (Exception ex)
{
return;
}
}
/// <summary>
/// 首次按下
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGrid_Project_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == System.Windows.Forms.Keys.Down || e.KeyCode == System.Windows.Forms.Keys.Up)
return;
dataGrid_Project_Click(null, null);
}
/// <summary>
/// 鼠标指针离开单元格时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGrid_Case_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
{
//if (dataGrid_Project.Focused) return;
if (txtProjectName.Focused)
return;
else
panel3.Visible = false;
}
private void dataGrid_Case_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
lblmessage.Visible = false;
//右键菜单
if (e.Button == MouseButtons.Right)
{
if (e.RowIndex >= 0)
{
//若行已是选中状态就不再进行设置
if (dataGrid_Case.Rows[e.RowIndex].Selected == false)
{
dataGrid_Case.ClearSelection();
dataGrid_Case.Rows[e.RowIndex].Selected = true;
}
//只选中一行时设置活动单元格
if (dataGrid_Case.SelectedRows.Count == 1)
{
dataGrid_Case.CurrentCell = dataGrid_Case.Rows[e.RowIndex].Cells[e.ColumnIndex];
}
//弹出操作菜单
contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
}
}
try
{
if (((DataGridView)sender).CurrentCell.OwningColumn.Name == "DGVProjectName")
{
System.Drawing.Point pt = ((DataGridView)sender).CurrentCellAddress;
FormMain fm = new FormMain();
int a = ((FormMain)this.Parent.Parent).fmTop;
if (a.ToString() == "-4")
{
pt.Y = ((DataGridView)sender).CurrentCell.AccessibilityObject.Bounds.Location.Y - 20;
}
else
{
pt.Y = ((DataGridView)sender).CurrentCell.AccessibilityObject.Bounds.Location.Y - a - 20;
}
txtProjectName.Location = pt;
txtProjectName.Size = ((DataGridView)sender).CurrentCell.Size;
if (((DataGridView)sender).CurrentCell.Value != null)
txtProjectName.Text = ((DataGridView)sender).CurrentCell.Value.ToString();
txtProjectName.Visible = true;
txtProjectName.Focus();
txtProjectName.SelectionStart = txtProjectName.TextLength;
//单击的时候显示匹配项
panel3.Visible = true;
System.Windows.Forms.TextBox edit = txtProjectName;
if (edit.Text == "")
{
panel3.Visible = false;
return;
}
try
{
string strname = edit.Text;
string filterString = "ProjectName LIKE '%" + strname + "%' or OtherName LIKE '%" + strname + "%' or PinYin LIKE '%" + strname + "%'";
System.Drawing.Point p = edit.Location;
System.Drawing.Point location = p;
location.Y += 21;
if (location.Y > 290) location.Y -= 281;
panel3.Visible = true;
panel3.Location = location;
DataView dv = new DataView(dsProjectList.Tables[0]);
dv.RowFilter = filterString;
dv.RowStateFilter = DataViewRowState.CurrentRows;
dataGrid_Project.DataSource = dv;
}
catch (Exception ex)
{
return;
}
if (dataGrid_Project.Rows.Count > 0)
{
for (int i = 0; i < dataGrid_Project.Rows.Count; i++)
{
if (dataGrid_Project.Rows[i].Cells["Col_ProjectName"].Value.ToString() == dataGrid_Case.CurrentRow.Cells["DGVProjectName"].Value.ToString())
{
panel3.Visible = true;
label9.Visible = false;
dataGrid_Case.Rows[i].DefaultCellStyle.ForeColor = Color.Black;
dataGrid_Case.Rows[i].DefaultCellStyle.BackColor = Color.White;
dataGrid_Case.Rows[i].Cells["DGVProjectName"].Style.BackColor = Color.White;
dataGrid_Case.Rows[i].Cells["DGVCaseId"].Value = "";
}
}
}
if ((dataGrid_Project.Rows.Count == 0 && dataGrid_Case.CurrentRow.Cells["DGVCaseId"].Value.ToString() != "-2") || txtProjectName.Text.Trim() == "" || dataGrid_Case.CurrentRow.Cells["DGVProjectName"].Style.BackColor == Color.Yellow)
{
panel3.Visible = true;
label9.Visible = true;
label9.Text = "添加" + '“' + dataGrid_Case.CurrentRow.Cells["DGVProjectName"].Value.ToString() + '”' + "到待建楼盘列表!";
}
else if (dataGrid_Case.CurrentRow.Cells["DGVCaseId"].Value.ToString() == "-2")
{
panel3.Visible = true;
label9.Visible = false;
lblmessage.Visible = true;
lblmessage.Text = "此楼盘已是待建楼盘!";
}
else
{
panel3.Visible = true;
label9.Visible = false;
}
}
}
catch (Exception ex)
{
MessageBox.Show("暂无数据可供排序!");
return;
}
}
//双击
private void dataGrid_Case_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
////右键菜单
//if (e.Button == MouseButtons.Right)
//{
// if (e.RowIndex >= 0)
// {
// //若行已是选中状态就不再进行设置
// if (dataGrid_Case.Rows[e.RowIndex].Selected == false)
// {
// dataGrid_Case.ClearSelection();
// dataGrid_Case.Rows[e.RowIndex].Selected = true;
// }
// //只选中一行时设置活动单元格
// if (dataGrid_Case.SelectedRows.Count == 1)
// {
// dataGrid_Case.CurrentCell = dataGrid_Case.Rows[e.RowIndex].Cells[e.ColumnIndex];
// }
// //弹出操作菜单
// contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
// }
//}
//if (((DataGridView)sender).CurrentCell.OwningColumn.Name == "DGVProjectName")
//{
// System.Drawing.Point pt = ((DataGridView)sender).CurrentCellAddress;
// FormMain fm = new FormMain();
// int a = ((FormMain)this.Parent.Parent).fmTop;
// if (a.ToString() == "-4")
// {
// pt.Y = ((DataGridView)sender).CurrentCell.AccessibilityObject.Bounds.Location.Y - 20;
// }
// else
// {
// pt.Y = ((DataGridView)sender).CurrentCell.AccessibilityObject.Bounds.Location.Y - a - 20;
// }
// txtProjectName.Location = pt;
// txtProjectName.Size = ((DataGridView)sender).CurrentCell.Size;
// if (((DataGridView)sender).CurrentCell.Value != null)
// txtProjectName.Text = ((DataGridView)sender).CurrentCell.Value.ToString();
// txtProjectName.Visible = true;
// txtProjectName.Focus();
// txtProjectName.SelectionStart = txtProjectName.TextLength;
// //单击的时候显示匹配项
// panel3.Visible = true;
// System.Windows.Forms.TextBox edit = txtProjectName;
// if (edit.Text == "")
// {
// panel3.Visible = false;
// return;
// }
// string strname = edit.Text;
// string filterString = "ProjectName LIKE '%" + strname + "%' or OtherName LIKE '%" + strname + "%' or PinYin LIKE '%" + strname + "%'";
// System.Drawing.Point p = edit.Location;
// System.Drawing.Point location = p;
// location.Y += 21;
// if (location.Y > 290) location.Y -= 281;
// panel3.Visible = true;
// panel3.Location = location;
// DataView dv = new DataView(dsProjectList.Tables[0]);
// dv.RowFilter = filterString;
// dv.RowStateFilter = DataViewRowState.CurrentRows;
// dataGrid_Project.DataSource = dv;
//}
}
//释放键的时候发生
private void txtProjectName_KeyUp(object sender, KeyEventArgs e)
{
System.Windows.Forms.TextBox edit = (System.Windows.Forms.TextBox)sender;
try
{
lblmessage.Visible = false;
if (edit.Text == "" && dataGrid_Case.CurrentRow.Cells["DGVProjectName"].Style.BackColor == Color.Yellow)
{
panel3.Visible = true;
label9.Visible = true;
label9.Text = "添加" + '“' + dataGrid_Case.CurrentRow.Cells["DGVProjectName"].Value.ToString() + '”' + "到待建楼盘列表!";
return;
}
}
catch (Exception ex)
{
return;
}
try
{
string strname = edit.Text;
string filterString = "ProjectName LIKE '%" + strname + "%' or OtherName LIKE '%" + strname + "%' or PinYin LIKE '%" + strname + "%'";
System.Drawing.Point p = edit.Location;
System.Drawing.Point location = p;
location.Y += 21;
if (location.Y > 290) location.Y -= 281;
panel3.Visible = true;
panel3.Location = location;
DataView dv = new DataView(dsProjectList.Tables[0]);
dv.RowFilter = filterString;
dv.RowStateFilter = DataViewRowState.CurrentRows;
dataGrid_Project.DataSource = dv;
}
catch (Exception ex)
{
return;
}
//默认回车选择第一行
if (dataGrid_Project.Rows.Count > 0)
{
panel3.Visible = true;
label9.Visible = false;
//dataGrid_Project.Rows[0].Selected = true;
if (e.KeyValue == 13)
{
for (int i = 0; i < dataGrid_Project.Rows.Count; i++)
{
if (dataGrid_Project.Rows[i].Selected == true)
{
string DGVProjectname = dataGrid_Case.CurrentRow.Cells["DGVProjectName"].Value.ToString();
if (e.KeyCode == System.Windows.Forms.Keys.Down || e.KeyCode == System.Windows.Forms.Keys.Up)
return;
int selProjectid = Convert.ToInt32(dataGrid_Project.CurrentRow.Cells["ProjectId"].Value.ToString());
dataGrid_Case.CurrentRow.Cells["DGVProjectId"].Value = selProjectid.ToString();
dataGrid_Case.CurrentRow.Cells["DGVProjectName"].Value = dataGrid_Project.CurrentRow.Cells["Col_ProjectName"].Value.ToString();
dataGrid_Case.CurrentRow.Cells["DGVProjectName"].Style.BackColor = Color.White;
txtProjectName.Text = dataGrid_Project.CurrentRow.Cells["Col_ProjectName"].Value.ToString();
panel3.Visible = false;
txtProjectName.Visible = false;
dataGrid_Case.CurrentRow.Cells["DGVProjectId"].ToolTipText = "modify";
for (int j = 0; j < dataGrid_Case.Rows.Count; j++)
{
if (DGVProjectname == dataGrid_Case.Rows[j].Cells["DGVProjectName"].Value.ToString())
{
dataGrid_Case.Rows[j].Cells["DGVProjectId"].Value = selProjectid.ToString();
dataGrid_Case.Rows[j].Cells["DGVProjectName"].Value = txtProjectName.Text;
dataGrid_Case.Rows[j].Cells["DGVProjectName"].Style.BackColor = Color.White;
dataGrid_Case.Rows[j].Cells["DGVProjectId"].ToolTipText = "modify";
}
}
}
}
}
}
else if ((dataGrid_Project.Rows.Count == 0 || txtProjectName.Text.Trim() == "" || dataGrid_Case.CurrentRow.Cells["DGVProjectName"].Style.BackColor == Color.Yellow) && dataGrid_Case.CurrentRow.Cells["DGVCaseId"].Value.ToString() != "-2")
{
panel3.Visible = true;
label9.Visible = true;
label9.Text = "添加" + '“' + dataGrid_Case.CurrentRow.Cells["DGVProjectName"].Value.ToString() + '”' + "到待建楼盘列表!";
}
}
//当控件不是窗体上的活动控件时发生
private void txtProjectName_Leave(object sender, EventArgs e)
{
if (dataGrid_Project.Focused) return;
//if (txtProjectName.Focused) return;
//
else
panel3.Visible = false;
txtProjectName.Visible = false;
}
//首次按下某个键
private void txtProjectName_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == System.Windows.Forms.Keys.Down)
{
if (panel3.Visible == true)
{
if (dataGrid_Project.RowCount > 0)
{
dataGrid_Project.Focus();
}
}
}
else if (dataGrid_Project.RowCount > 0) panel3.Visible = true;
}
#endregion

浙公网安备 33010602011771号