<p><a href="https://www.pianshen.com/" style="box-sizing: border-box; background: 0px 0px; color: rgb(255, 255, 255); text-decoration-line: none; line-height: 23px;"><span style="box-sizing: border-box; display: block;">程序员大本营</span><small>技术文章内容聚合第一站</small></a></p><p><br/></p><p><br/></p><p> </p><h2 style="box-sizing: border-box; font-family: inherit; font-weight: 500; line-height: 32px; color: inherit; margin-top: 20px; margin-bottom: 10px; font-size: 30px; word-break: break-all;"><span style="box-sizing: border-box;">在GridControl控件中使用PopupContainerEdit构建数据快速输入检索</span></h2><p> </p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify;">这几天一直被这个东西困扰,不过最终还是解决了,所以分享一下。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify;"><span style="box-sizing: border-box; font-weight: 700;">效果图如下:</span></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify;"><img alt="" src="/ueditor/net/upload/image/20210414/6375401206655210937440893.gif"/></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify;"> 这边我就说一个,就是药品名称的检索(看懂一个其它都大同小异)。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify;">第一步,你需要创建一个用户控件,这个控件就是你下拉时来显示数据的,如下图:</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify;">我这里用户控件名为YpxxList</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify;"><img alt="" src="/ueditor/net/upload/image/20210414/6375401207048960934642908.png"/></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify;">第二步,需要在GridView中你需要实现下拉的列,ColumnEdit类型选PopupContainerEdit<span style="box-sizing: border-box; font-weight: 700;">(这是它的类型RepositoryItemPopupContainerEdit)</span></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify;"><img alt="" src="/ueditor/net/upload/image/20210414/6375401207310484371344860.png"/></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify;">这里重点,改好类型后这里会显示出来,要记得改一下TextEditStyle的类型,它默认是不让你在下拉框中输入东西,这样你就不能检索数据,这个问题我搞了好久....</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify;"><img alt="" src="/ueditor/net/upload/image/20210414/6375401207672105466448230.png"/> </p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify;"> </p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify;">第三步,需要在主窗体里声明几个对象,如下:</p><pre class="has" style="box-sizing: border-box; overflow: auto; font-family: Consolas, Inconsolata, Courier, monospace; padding: 8px; margin-top: 0px; margin-bottom: 24px; line-height: 22px; color: rgb(0, 0, 0); word-break: break-all; overflow-wrap: break-word; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); border-radius: 4px; position: relative; white-space: pre-wrap;">private YpxxList ypxxList = null; //药品下拉列表(就是我刚才的用户控件对象)public delegate void UiHandleGrid(string text);//声明委托,需要跟用户控件进行传递数据,很重要public static event UiHandleGrid uihandleGrid;</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify;">然后在主窗体里开始编写代码,如下: </p><pre class="has" style="box-sizing: border-box; overflow: auto; font-family: Consolas, Inconsolata, Courier, monospace; padding: 8px; margin-top: 0px; margin-bottom: 24px; line-height: 22px; color: rgb(0, 0, 0); word-break: break-all; overflow-wrap: break-word; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); border-radius: 4px; position: relative; white-space: pre-wrap;">//这个方法放到主窗体的Load里private void InitYpxx(){//这个是RepositoryItemPopupContainerEdit的子,这个控件应该是类似于Winform中的PanelPopupContainerControl 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){gridView1.SetFocusedRowCellValue("ypmc", dr["itemname"]);gridView1.SetFocusedRowCellValue("dosageUnit", dr["doseunit"]);//popupContainerEdit.ClosePopup();}</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify;">第四步,就是给PopupContainerEdit添加两个事件,如下:</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify;"><img alt="" src="/ueditor/net/upload/image/20210414/6375401207994664061737035.png"/></p><pre class="has" style="box-sizing: border-box; overflow: auto; font-family: Consolas, Inconsolata, Courier, monospace; padding: 8px; margin-top: 0px; margin-bottom: 24px; line-height: 22px; color: rgb(0, 0, 0); word-break: break-all; overflow-wrap: break-word; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); border-radius: 4px; position: relative; white-space: pre-wrap;">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();}}</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify;"> 到这里主窗体的工作已完成,下面开始用户控件的代码编写,如下:‘</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify;">第一步,也是声明几个对象下面要用到,代码如下:</p><pre class="has" style="box-sizing: border-box; overflow: auto; font-family: Consolas, Inconsolata, Courier, monospace; padding: 8px; margin-top: 0px; margin-bottom: 24px; line-height: 22px; color: rgb(0, 0, 0); word-break: break-all; overflow-wrap: break-word; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); border-radius: 4px; position: relative; white-space: pre-wrap;">//放显示的数据private DataTable dt;//委托,把选中的行数据传递给主窗体public delegate void GetGridDialog(DataRow dr);public event GetGridDialog getGridDialog;</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify;">第二步,我就一次性把代码放出来了,偷个懒哈哈哈,如下:</p><pre class="has" style="box-sizing: border-box; overflow: auto; font-family: Consolas, Inconsolata, Courier, monospace; padding: 8px; margin-top: 0px; margin-bottom: 24px; line-height: 22px; color: rgb(0, 0, 0); word-break: break-all; overflow-wrap: break-word; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); border-radius: 4px; position: relative; white-space: pre-wrap;">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);}}</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify;">这样就大功告成了!!!首次用有些地方还不太懂,鄙人愚钝,有错的地方请明示。</p><p style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 16px; padding: 0px; color: rgb(196, 196, 200); overflow: auto hidden; overflow-wrap: break-word; font-family: -apple-system, "SF UI Text", Arial, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", sans-serif, SimHei, SimSun; white-space: normal; background-color: rgb(38, 39, 40); line-height: 26px !important;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; overflow-wrap: break-word; color: rgb(243, 59, 69);"><span style="box-sizing: border-box; outline: 0px; font-weight: 700; overflow-wrap: break-word;">补充:最近发现代码有点问题,下面进行补充;</span></span></p><p style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 16px; padding: 0px; color: rgb(196, 196, 200); overflow: auto hidden; overflow-wrap: break-word; font-family: -apple-system, "SF UI Text", Arial, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", sans-serif, SimHei, SimSun; white-space: normal; background-color: rgb(38, 39, 40); line-height: 26px !important;"><img alt="" class="has" height="1048" src="/ueditor/net/upload/image/20210414/6375401244450425785507930" width="1200"/></p><p style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 16px; padding: 0px; color: rgb(196, 196, 200); overflow: auto hidden; overflow-wrap: break-word; font-family: -apple-system, "SF UI Text", Arial, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", sans-serif, SimHei, SimSun; white-space: normal; background-color: rgb(38, 39, 40); line-height: 26px !important;">需要把三个属性值改为“False” ,我遇到的问题是在进行检索后选择某个值,会出现下面这种情况:</p><p style="box-sizing: border-box; outline: 0px; margin-top: 0px; margin-bottom: 16px; padding: 0px; color: rgb(196, 196, 200); overflow: auto hidden; overflow-wrap: break-word; font-family: -apple-system, "SF UI Text", Arial, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", sans-serif, SimHei, SimSun; white-space: normal; background-color: rgb(38, 39, 40); line-height: 26px !important;"><img alt="" class="has" height="212" src="/ueditor/net/upload/image/20210414/6375401244472691407578390" width="360"/></p><p><span class="creativecommons" style="box-sizing: border-box;"><a href="https://creativecommons.org/licenses/by-sa/4.0/" style="box-sizing: border-box; background-image: initial; background-position: 0px 0px; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; color: rgb(66, 139, 202); text-decoration-line: none;"></a><span style="box-sizing: border-box;">版权声明:本文为博主原创文章,遵循<a href="https://creativecommons.org/licenses/by-sa/4.0/" target="_blank" style="box-sizing: border-box; background-image: initial; background-position: 0px 0px; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; color: rgb(66, 139, 202); text-decoration-line: none;"> CC 4.0 BY-SA </a>版权协议,转载请附上原文出处链接和本声明。</span></span></p><p>本文链接:<a href="https://blog.csdn.net/MonkeyBananas/article/details/89713012" target="_blank" style="box-sizing: border-box; background-image: initial; background-position: 0px 0px; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; color: rgb(66, 139, 202); text-decoration-line: none;">https://blog.csdn.net/MonkeyBananas/article/details/89713012</a></p><p><br/></p><p><a href="https://www.pianshen.com/copyright.html#del" target="_blank" style="box-sizing: border-box; background-image: initial; background-position: 0px 0px; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; color: rgb(66, 139, 202); text-decoration-line: none;">原作者删帖</a> <a href="https://www.pianshen.com/copyright.html#others" target="_blank" style="box-sizing: border-box; background-image: initial; background-position: 0px 0px; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; color: rgb(66, 139, 202); text-decoration-line: none;">不实内容删帖</a> <a href="mailto:pianshen@gmx.com?subject=%E6%8A%95%E8%AF%89%E6%9C%AC%E6%96%87%E5%90%AB%E5%B9%BF%E5%91%8A%E6%88%96%E5%9E%83%E5%9C%BE%E4%BF%A1%E6%81%AF%EF%BC%88%E8%AF%B7%E9%99%84%E4%B8%8A%E8%BF%9D%E8%A7%84%E9%93%BE%E6%8E%A5%E5%9C%B0%E5%9D%80%EF%BC%89" title="投诉本文含广告或垃圾信息" style="box-sizing: border-box; background-image: initial; background-position: 0px 0px; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; color: rgb(66, 139, 202); text-decoration-line: none;">广告或垃圾文章投诉</a></p><hr/><h3 style="box-sizing: border-box; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; margin-top: 20px; margin-bottom: 10px; font-size: 24px;"><br/></h3><p><br/></p>