这几天一直被这个东西困扰,不过最终还是解决了,所以分享一下。
效果图如下:
这边我就说一个,就是药品名称的检索(看懂一个其它都大同小异)。
第一步,你需要创建一个用户控件,这个控件就是你下拉时来显示数据的,如下图:
我这里用户控件名为YpxxList
第二步,需要在GridView中你需要实现下拉的列,ColumnEdit类型选PopupContainerEdit(这是它的类型RepositoryItemPopupContainerEdit)
这里重点,改好类型后这里会显示出来,要记得改一下TextEditStyle的类型,它默认是不让你在下拉框中输入东西,这样你就不能检索数据,这个问题我搞了好久....
第三步,需要在主窗体里声明几个对象,如下:
private YpxxList ypxxList = null; //药品下拉列表(就是我刚才的用户控件对象)
public delegate void UiHandleGrid(string text);//声明委托,需要跟用户控件进行传递数据,很重要
public static event UiHandleGrid uihandleGrid;
然后在主窗体里开始编写代码,如下:
//这个方法放到主窗体的Load里
private void InitYpxx()
{
//这个是RepositoryItemPopupContainerEdit的子,这个控件应该是类似于Winform中的Panel
PopupContainerControl pccYpxxList = new PopupContainerControl();
//把PopupContainerControl给RepositoryItemPopupContainerEdit的弹出控件(PopupControl)
PopupContainerEdit_Ypxx.PopupControl = pccYpxxList;
//然后设置一下显示的宽高
pccYpxxList.Width = 300;
pccYpxxList.Height = 200;
ypxxList = new YpxxList();
ypxxList.Dock = DockStyle.Fill;
//将用户控件添加到PopupContainerControl中
pccYpxxList.Controls.Add(ypxxList);
//这里是用户控件的一个委托,刚才说过了主窗体的委托,它们就是用来互相传递一些数据的,比如我在下拉框中选择一行数据,我需要把几列值赋给主窗体上对应的几列
ypxxList.getGridDialog += new YpxxList.GetGridDialog(YpxxList_getYpxxList);
}
//这个就是用户控件传递过来的一行选中的DataRow对象,我需要把一些值赋给主窗体的某些列
private void YpxxList_getYpxxList(DataRow dr)
{
if (gridView1.FocusedRowHandle < 0)
{
gridView1.AddNewRow();
}
gridView1.SetFocusedRowCellValue("ypmc", dr["itemname"]);
gridView1.SetFocusedRowCellValue("dosageUnit", dr["doseunit"]);
//popupContainerEdit.ClosePopup();
}
第四步,就是给PopupContainerEdit添加两个事件,如下:
private void PopupContainerEdit_Ypxx_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Down)
{
PopupContainerEdit_Ypxx.PopupControl.Controls[0].Focus();
}
}
//这个方法就是输入值时,把输入的值传给用户控件,用户控件进行检索。
private void PopupContainerEdit_Ypxx_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
{
popupContainerEdit = sender as PopupContainerEdit;
string srm = popupContainerEdit.Text;
if (srm.Trim() != string.Empty)
{
uihandleGrid(srm);
popupContainerEdit.ShowPopup();
popupContainerEdit.Focus();
}
else
{
popupContainerEdit.ClosePopup();
}
}
到这里主窗体的工作已完成,下面开始用户控件的代码编写,如下:‘
第一步,也是声明几个对象下面要用到,代码如下:
//放显示的数据
private DataTable dt;
//委托,把选中的行数据传递给主窗体
public delegate void GetGridDialog(DataRow dr);
public event GetGridDialog getGridDialog;
第二步,我就一次性把代码放出来了,偷个懒哈哈哈,如下:
public YpxxList()
{
InitializeComponent();
InitList();
//主窗体的委托
Qphzcyxxd.uihandleGrid += new Qphzcyxxd.UiHandleGrid(frmYpxxAdvMain_uihandleGrid);
}
/// <summary>
/// 根据输入的text来检索数据
/// </summary>
/// <param name="text"></param>
void frmYpxxAdvMain_uihandleGrid(string text)
{
//第一句根据你的需求替换即可,下面的应该是通用的
var drs = dt.Select(string.Format("itemname like '%{0}%' or inputcode like '%{0}%'", text));
var table = dt.Clone();
foreach (var dr in drs)
{
var temp = table.NewRow();
temp.ItemArray = dr.ItemArray;
table.Rows.Add(temp);
}
gcYpxx.DataSource = table;
gcYpxx.RefreshDataSource();
}
/// <summary>
/// 初始数据
/// </summary>
public void InitList()
{
//访问数据库得到你想要的数据源DataTalbe,然后给控件
gcYpxx.DataSource = dt;
}
//GridView的双击事件
private void gridView_DoubleClick(object sender, EventArgs e)
{
if (gv_Ypxx.FocusedRowHandle > -1)
{
//得到选中行的数据传递给主窗体
var dr = gv_Ypxx.GetFocusedDataRow();
getGridDialog(dr);
}
}
//鼠标按下的事件
private void gridView_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter && gv_Ypxx.FocusedRowHandle > -1)
{
//得到选中行的数据传递给主窗体
var dr = gv_Ypxx.GetFocusedDataRow();
getGridDialog(dr);
}
}
这样就大功告成了!!!首次用有些地方还不太懂,鄙人愚钝,有错的地方请明示。
补充:最近发现代码有点问题,下面进行补充;
需要把三个属性值改为“False” ,我遇到的问题是在进行检索后选择某个值,会出现下面这种情况:
会有空行,后来发现需要把这三个值改成“False”。