Browse Source

DBFind双击单元格赋值功能修改

Hcsy 8 years ago
parent
commit
3904878fb3

+ 28 - 10
UAS-MES/CustomControl/TextBoxWithIcon/MaCodeSearchTextBox.cs

@@ -63,6 +63,9 @@ namespace UAS_MES.CustomControl.TextBoxWithIcon
         /// </summary>
         /// </summary>
         LockCheckBox LockCheckBox;
         LockCheckBox LockCheckBox;
 
 
+
+        DataTable dt = new DataTable();
+
         DbFind db;
         DbFind db;
 
 
         public override string Text
         public override string Text
@@ -259,21 +262,14 @@ namespace UAS_MES.CustomControl.TextBoxWithIcon
                 {
                 {
                     sql += " and " + condition;
                     sql += " and " + condition;
                 }
                 }
-                DataTable dt = (DataTable)dh.ExecuteSql(sql, "select");
+                 dt = (DataTable)dh.ExecuteSql(sql, "select");
                 FormCollection fmCollection = Application.OpenForms;
                 FormCollection fmCollection = Application.OpenForms;
+                ControlCollection controls = (ControlCollection)fmCollection[FormName].Controls;
                 if (dt.Rows.Count > 0)
                 if (dt.Rows.Count > 0)
                 {
                 {
                     for (int i = 0; i < dt.Columns.Count; i++)
                     for (int i = 0; i < dt.Columns.Count; i++)
                     {
                     {
-                        for (int j = 0; j < SetValueField.Length; j++)
-                        {
-                            //利用字段的Tag属性来和列名进行比对
-                            object tag = fmCollection[FormName].Controls[SetValueField[j]].Tag;
-                            if (SetValueField[j] == dt.Columns[i].ColumnName.ToLower() || (tag != null && tag == dt.Columns[i].ColumnName.ToLower()))
-                            {
-                                fmCollection[FormName].Controls[SetValueField[j]].Text = dt.Rows[0][dt.Columns[i].ColumnName].ToString();
-                            }
-                        }
+                        fillControl( i, fmCollection[FormName]);
                     }
                     }
                     LeaveFindData = true;
                     LeaveFindData = true;
                 }
                 }
@@ -284,6 +280,26 @@ namespace UAS_MES.CustomControl.TextBoxWithIcon
             }
             }
         }
         }
 
 
+        private void fillControl( int i, Control ct)
+        {
+            for (int j = 0; j < setValueField.Length; j++)
+            {
+                if (ct.Controls.Count > 0 && ct.Name.ToString() != setValueField[j])
+                {
+                    Control.ControlCollection controls = ct.Controls;
+                    for (int k = 0; k < ct.Controls.Count; k++)
+                    {
+                        fillControl( i, controls[k]);
+                    }
+                }
+                else
+                {
+                    if ((setValueField[j] == dt.Columns[i].Caption.ToLower() || setValueField[j] == dt.Columns[i].ColumnName.ToLower() || setValueField[j].Contains(dt.Columns[i].Caption.ToLower()) || (ct != null && ct.Tag != null && ct.Tag.ToString() == dt.Columns[i].Caption.ToLower())) && ct.Name.ToString() == setValueField[j])
+                        ct.Text = dt.Rows[0][dt.Columns[i].ColumnName].ToString();
+                }
+            }
+        }
+
         private void Search_Icon_Click(object sender, EventArgs e)
         private void Search_Icon_Click(object sender, EventArgs e)
         {
         {
             SearchIconClick?.Invoke(sender, new EventArgs());
             SearchIconClick?.Invoke(sender, new EventArgs());
@@ -316,5 +332,7 @@ namespace UAS_MES.CustomControl.TextBoxWithIcon
         {
         {
             TextBox.Width = this.Width - Search_Icon.Width - 3;
             TextBox.Width = this.Width - Search_Icon.Width - 3;
         }
         }
+
+    
     }
     }
 }
 }

+ 26 - 32
UAS-MES/CustomControl/TextBoxWithIcon/SearchTextBox.cs

@@ -57,6 +57,7 @@ namespace UAS_MES.CustomControl.TextBoxWithIcon
         /// 查询的字段
         /// 查询的字段
         /// </summary>
         /// </summary>
         private string selectField;
         private string selectField;
+        DataTable dt = new DataTable();
 
 
         public override string Text
         public override string Text
         {
         {
@@ -286,6 +287,28 @@ namespace UAS_MES.CustomControl.TextBoxWithIcon
             TextBox.Width = this.Width - Search_Icon.Width - 3;
             TextBox.Width = this.Width - Search_Icon.Width - 3;
         }
         }
 
 
+
+
+        private void fillControl(int i, Control ct)
+        {
+            for (int j = 0; j < setValueField.Length; j++)
+            {
+                if (ct.Controls.Count > 0 && ct.Name.ToString() != setValueField[j])
+                {
+                    Control.ControlCollection controls = ct.Controls;
+                    for (int k = 0; k < ct.Controls.Count; k++)
+                    {
+                        fillControl(i, controls[k]);
+                    }
+                }
+                else
+                {               
+                    if ((setValueField[j] == dt.Columns[i].Caption.ToLower() || setValueField[j] == dt.Columns[i].ColumnName.ToLower() || setValueField[j].Contains(dt.Columns[i].Caption.ToLower()) || (ct != null && ct.Tag != null && ct.Tag.ToString() == dt.Columns[i].Caption.ToLower())) && ct.Name.ToString() == setValueField[j])
+                        ct.Text = dt.Rows[0][dt.Columns[i].ColumnName].ToString();
+                }
+            }
+        }
+
         public void GetData()
         public void GetData()
         {
         {
             if (TextBox.Text != "")
             if (TextBox.Text != "")
@@ -305,43 +328,14 @@ namespace UAS_MES.CustomControl.TextBoxWithIcon
                 {
                 {
                     sql += " and " + condition;
                     sql += " and " + condition;
                 }
                 }
-                DataTable dt = (DataTable)dh.ExecuteSql(sql, "select");
-                Boolean groupcontains = false;
+                dt = (DataTable)dh.ExecuteSql(sql, "select");
                 //先判断DataTable里面是否有这个字段,然后从打开的窗口里面去获取到这个Form,从Form中的指定Panel获取到指定字段的控件
                 //先判断DataTable里面是否有这个字段,然后从打开的窗口里面去获取到这个Form,从Form中的指定Panel获取到指定字段的控件
                 FormCollection fmCollection = Application.OpenForms;
                 FormCollection fmCollection = Application.OpenForms;
                 ControlCollection controls = (ControlCollection)fmCollection[FormName].Controls;
                 ControlCollection controls = (ControlCollection)fmCollection[FormName].Controls;
                 for (int i = 0; i < dt.Columns.Count; i++)
                 for (int i = 0; i < dt.Columns.Count; i++)
                 {
                 {
-                    for (int k = 0; k < controls.Count; k++)
-                    {
-                        Control ctl = controls[k];
-                        if (ctl.Controls.Count > 0)
-                        {
-                            for (int j = 0; j < SetValueField.Length; j++)
-                            {
-                                Control ct2 = ctl.Controls[SetValueField[j]];
-                                if (ct2 != null)
-                                {
-                                    if (SetValueField[j] == dt.Columns[i].Caption.ToLower() || SetValueField[j] == dt.Columns[i].ColumnName.ToLower() || SetValueField[j].Contains(dt.Columns[i].Caption.ToLower()) || (ct2 != null && ct2.Tag != null && ct2.Tag.ToString() == dt.Columns[i].Caption.ToLower()))
-                                        ct2.Text = dt.Rows[0][dt.Columns[i].ColumnName].ToString();
-                                    groupcontains = true;
-                                }
-                            }
-                        }
-                    }
-                }
-                if (!groupcontains)
-                {
-                    for (int i = 0; i < dt.Columns.Count; i++)
-                    {
-                        for (int j = 0; j < SetValueField.Length; j++)
-                        {
-                            Control ctl = fmCollection[FormName].Controls[SetValueField[j]];
-                            if (SetValueField[j] == dt.Columns[i].Caption || SetValueField[j] == dt.Columns[i].ColumnName || SetValueField[j].Contains(dt.Columns[i].Caption) || (ctl != null && ctl.Tag != null && ctl.Tag.ToString() == dt.Columns[i].Caption))
-                                fmCollection[FormName].Controls[SetValueField[j]].Text = dt.Rows[0][dt.Columns[i].ColumnName].ToString();
-                        }
-                    }
-                }
+                    fillControl(i, fmCollection[FormName]);
+                }          
             }
             }
         }
         }
     }
     }

+ 26 - 35
UAS-MES/DbFind.cs

@@ -162,44 +162,12 @@ namespace UAS_MES
         {
         {
             try
             try
             {
             {
-                //赋值控件是否被GROUP包含
-                Boolean groupcontains = false;
                 //先判断DataTable里面是否有这个字段,然后从打开的窗口里面去获取到这个Form,从Form中的指定Panel获取到指定字段的控件
                 //先判断DataTable里面是否有这个字段,然后从打开的窗口里面去获取到这个Form,从Form中的指定Panel获取到指定字段的控件
                 FormCollection fmCollection = Application.OpenForms;
                 FormCollection fmCollection = Application.OpenForms;
                 ControlCollection controls = (ControlCollection)fmCollection[FormName].Controls;
                 ControlCollection controls = (ControlCollection)fmCollection[FormName].Controls;
                 for (int i = 0; i < dt.Columns.Count; i++)
                 for (int i = 0; i < dt.Columns.Count; i++)
                 {
                 {
-                    for (int k = 0; k < controls.Count; k++)
-                    {
-                        Control ctl = controls[k];
-                        if (ctl.Controls.Count > 0)
-                        {
-                            for (int j = 0; j < SetValueField.Length; j++)
-                            {
-                                Control ct2 = ctl.Controls[SetValueField[j]];
-                                MainControl = ctl.Controls[MainField];
-                                if (ct2 != null)
-                                {
-                                    if (SetValueField[j] == dt.Columns[i].Caption || SetValueField[j] == dt.Columns[i].ColumnName || SetValueField[j].Contains(dt.Columns[i].Caption) || (ct2 != null && ct2.Tag != null && ct2.Tag.ToString() == dt.Columns[i].Caption))
-                                        ct2.Text = DbFindGridView.Rows[e.RowIndex].Cells[dt.Columns[i].ColumnName].Value.ToString();
-                                    groupcontains = true;
-                                }
-                            }
-                        }
-                    }
-                }
-                if (!groupcontains)
-                {
-                    for (int i = 0; i < dt.Columns.Count; i++)
-                    {
-                        for (int j = 0; j < SetValueField.Length; j++)
-                        {
-                            Control ctl = fmCollection[FormName].Controls[SetValueField[j]];
-                            MainControl = fmCollection[FormName].Controls[MainField];
-                            if (SetValueField[j] == dt.Columns[i].Caption || SetValueField[j] == dt.Columns[i].ColumnName || SetValueField[j].Contains(dt.Columns[i].Caption) || (ctl != null && ctl.Tag != null && ctl.Tag.ToString() == dt.Columns[i].Caption))
-                                fmCollection[FormName].Controls[SetValueField[j]].Text = DbFindGridView.Rows[e.RowIndex].Cells[dt.Columns[i].ColumnName].Value.ToString();
-                        }
-                    }
+                    fillControl(e, i, fmCollection[FormName]);
                 }
                 }
                 //发起DBFind的控件
                 //发起DBFind的控件
                 SuccessReturnData = true;
                 SuccessReturnData = true;
@@ -215,15 +183,38 @@ namespace UAS_MES
                 }
                 }
                 MainControl.Focus();
                 MainControl.Focus();
             }
             }
-            catch (Exception ex)
+            catch (Exception ea)
             {
             {
-                LogManager.DoLog(ex.Message);
                 SuccessReturnData = false;
                 SuccessReturnData = false;
             }
             }
             Dispose();
             Dispose();
             Close();
             Close();
         }
         }
 
 
+        private void fillControl(DataGridViewCellEventArgs e, int i, Control ct)
+        {
+            for (int j = 0; j < SetValueField.Length; j++)
+            {
+                if (ct.Controls.Count > 0 && ct.Name.ToString() != SetValueField[j])
+                {
+                    Control.ControlCollection controls = ct.Controls;
+                    for (int k = 0; k < ct.Controls.Count; k++)
+                    {
+                        fillControl(e, i, controls[k]);
+                    }
+                }
+                else
+                {
+                    if ((SetValueField[j] == dt.Columns[i].Caption || SetValueField[j] == dt.Columns[i].ColumnName || SetValueField[j].Contains(dt.Columns[i].Caption) || (ct != null && ct.Tag != null && ct.Tag.ToString() == dt.Columns[i].Caption)) && ct.Name.ToString() == SetValueField[j])
+                        ct.Text = DbFindGridView.Rows[e.RowIndex].Cells[dt.Columns[i].ColumnName].Value.ToString();
+                    if (ct.Name == MainField)
+                    {
+                        MainControl = ct;
+                    }
+                }
+            }
+        }
+
         //按下Esc键的时候关闭当前的界面,用于DbFind
         //按下Esc键的时候关闭当前的界面,用于DbFind
         private void DbFindGridView_KeyPress(object sender, KeyPressEventArgs e)
         private void DbFindGridView_KeyPress(object sender, KeyPressEventArgs e)
         {
         {