Files
lserp_cs_5.0/插件库/Lskj.MyControl/MySearchDgv.cs
T
2026-07-10 15:25:05 +08:00

271 lines
8.7 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.Windows;
namespace Lskj.MyControl
{
public partial class MySearchDgv : Myeditdgv
{
/// <summary>
/// 搜索框数据列表
/// </summary>
public DataGridView dgv;
/// <summary>
/// 数据面板
/// </summary>
private Panel dgvPanel;
/// <summary>
/// 组件父容器
/// </summary>
private IContainer myContainer;
public int selectRow;
public int isshowImage = 0;
public MySearchDgv()
{
InitializeComponent();
}
/// <summary>
/// 选择确认事件接口
/// </summary>
private KeyEventHandler cellkeydown;
public KeyEventHandler CellKeyDown
{
get { return cellkeydown; }
set { cellkeydown = value; }
}
/// <summary>
/// 选择确认事件接口
/// </summary>
private EventHandler accept;
public EventHandler Accept
{
get { return accept; }
set { accept = value; }
}
public MySearchDgv(IContainer container)
{
container.Add(this);
InitializeComponent();
myContainer = container;
container.Add(this);
this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
this.MultiSelect = false;
//数据列表初始化
this.dgv = new DataGridView();
this.dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
this.dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
this.dgv.EditMode = DataGridViewEditMode.EditProgrammatically;
this.dgv.Dock = DockStyle.Fill;
this.dgv.MultiSelect = false;
this.dgv.AllowUserToResizeColumns = true;
this.dgv.AllowUserToResizeRows = false;
this.dgv.RowHeadersVisible = false;
this.dgv.AllowUserToAddRows = false;
this.dgv.DataSource = null;
this.dgv.Leave += new EventHandler(VasunSearchDgv_Leave);
this.dgv.CellDoubleClick += new DataGridViewCellEventHandler(Dgv_RowDoubleClick);
this.dgv.BackgroundColor = Color.White;
//数据面板初始化
this.dgvPanel = new Panel();
this.dgvPanel.Visible = false;
this.dgvPanel.Size = new System.Drawing.Size(0, 0);
//添加数据列表
this.dgvPanel.Controls.Add(this.dgv);
}
private void VasunSearchDgv_Leave(object sender, EventArgs e)
{
if (!this.dgv.Focused && !this.dgvPanel.Focused)
{
this.closeImage();
}
}
private void Dgv_RowDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (this.Accept != null)
{
this.Accept.Invoke(sender, e);
}
}
//显示联想框
private void showImage()
{
selectRow = this.CurrentCell.RowIndex;
int x = 0, y = 0;
Rectangle rt = this.GetCellDisplayRectangle(this.CurrentCell.ColumnIndex, this.CurrentCell.RowIndex, false);//获取当前单元格的矩形框
Control Ctrol = this;
this.dgvPanel.Size = new System.Drawing.Size(this.Width * (this.ColumnCount - 1) / this.ColumnCount - 10, this.CurrentRow.Height * 6);
if (this.Height - rt.Y >= this.CurrentRow.Height * 6)
{
x += Ctrol.Location.X + this.Width / this.ColumnCount;
y += rt.Y + this.CurrentRow.Height;
}
else
{
x += Ctrol.Location.X + this.Width / this.ColumnCount;
y += rt.Y - 6 * this.CurrentRow.Height;
}
//显示
this.dgvPanel.Visible = true;
this.dgvPanel.Location = new System.Drawing.Point(x, y);
Ctrol.Controls.Add(this.dgvPanel);
this.dgvPanel.BringToFront();
isshowImage = 1;
}
/// <summary>
/// 关闭联想框
/// </summary>
private void closeImage()
{
if (dgvPanel != null)
{
this.dgvPanel.Visible = false;
}
isshowImage = 0;
}
//获取数据项
public Object getDgvSelectedData(int colum)
{
Object o = new Object();
if (this.dgv.SelectedRows.Count > 0)
{
o = this.dgv[colum, this.dgv.SelectedRows[0].Index].Value;
return o;
}
else return Text;
}
//重新绑定
public void reBing(DataTable dt)
{
this.dgv.DataSource = dt;
}
private void SearchBar_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
if (CurrentCell.ColumnIndex == 0 && CurrentCell.Value.ToString() == "")
showImage();
else closeImage();
}
private void VasunSearchDgv_CellBeginEdit(object sender, System.Windows.Forms.DataGridViewCellCancelEventArgs e)
{
if (CurrentCell.ColumnIndex == 0 && CurrentCell.Value.ToString() == "")
showImage();
else closeImage();
}
private void VasunSearchDgv_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (this.IsCurrentCellDirty)
{
this.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
private void VasunSearchDgv_CellValueChanged(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
{
if (this.CurrentCell.ColumnIndex == 0)
{
this.showImage();
}
}
protected override bool ProcessCmdKey(ref Message msg, Keys KeyData)
{
if (KeyData == Keys.Down)
{
int nowRowIndex;
if (this.dgv.Rows.Count > 0)
{
nowRowIndex = this.dgv.SelectedRows[0].Index;
if (this.dgv.Rows.Count - 1 > nowRowIndex)
{
this.dgv.Rows[nowRowIndex + 1].Selected = true;
this.dgv.CurrentCell = this.dgv.Rows[nowRowIndex + 1].Cells[0];
}
this.dgv.FirstDisplayedScrollingRowIndex = nowRowIndex;
}
return true;
}
else if (KeyData == Keys.Up)
{
int nowRowIndex;
if (this.dgv.Rows.Count > 0)
{
nowRowIndex = this.dgv.SelectedRows[0].Index;
if (nowRowIndex > 0)
{
this.dgv.Rows[nowRowIndex - 1].Selected = true;
this.dgv.CurrentCell = this.dgv.Rows[nowRowIndex - 1].Cells[0];
}
try
{
this.dgv.FirstDisplayedScrollingRowIndex = nowRowIndex - 1;
}
catch { }
}
return true;
}
else if (KeyData == Keys.Enter)
{
if (this.isshowImage == 1)
{
this.CellKeyDown(this, new KeyEventArgs(KeyData));
this.closeImage();
return true;
}
else if (this.CurrentCell.RowIndex == this.RowCount - 1 && this.CurrentCell.ColumnIndex == this.ColumnCount - 1)
{
DataTable table = (DataTable)this.DataSource;
DataRow dr = table.NewRow();
table.Rows.Add(dr);
this.CurrentCell = this.Rows[this.RowCount - 1].Cells[0];
this.BeginEdit(true);
return true;
}
}
else if (KeyData == Keys.Escape)
{
this.closeImage();
return true;
}
return base.ProcessCmdKey(ref msg, KeyData);
}
private void VasunSearchDgv_RowLeave(object sender, DataGridViewCellEventArgs e)
{
if (this.CurrentRow.Index == this.RowCount - 2)
{
DataTable table = (DataTable)this.DataSource;
DataRow dr = table.NewRow();
table.Rows.Add(dr);
}
}
}
}