Query_SOP.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.Net;
  6. using System.Collections.Generic;
  7. using System.Threading;
  8. using System.Data;
  9. using System.Drawing.Imaging;
  10. using System.Drawing;
  11. using UAS_MES_NEW.Entity;
  12. using UAS_MES_NEW.PublicMethod;
  13. using Aspose.Cells;
  14. using UAS_MES_NEW.DataOperate;
  15. using UAS_MES_NEW.PublicForm;
  16. using Aspose.Cells.Drawing;
  17. using Aspose.Cells.Rendering;
  18. using System.Web.Script.Serialization;
  19. using Seagull.BarTender.Print;
  20. namespace UAS_MES_NEW.Query
  21. {
  22. public partial class Query_SOP : Form
  23. {
  24. DataHelper dh;
  25. string usercode;
  26. string sourcecode;
  27. DataTable Dbfind;
  28. public Query_SOP()
  29. {
  30. usercode = "";
  31. sourcecode = "";
  32. InitializeComponent();
  33. }
  34. private void Form3_Load(object sender, EventArgs e)
  35. {
  36. CheckForIllegalCrossThreadCalls = false;
  37. ps_prodcode.TableName = "productsop";
  38. ps_prodcode.SelectField = "ps_code # 流水号,ps_prodcode # SOP编号";
  39. ps_prodcode.FormName = Name;
  40. ps_prodcode.DBTitle = "SOP查询";
  41. ps_prodcode.SetValueField = new string[] { "ps_code", "ps_prodcode" };
  42. ps_prodcode.Condition = "";
  43. ps_prodcode.DbChange += nr_rule_DBChange;
  44. pr_code.TableName = "product";
  45. pr_code.SelectField = "pr_code # 产品编号,pr_detail # 产品名称,pr_spec # 产品规格";
  46. pr_code.FormName = Name;
  47. pr_code.DBTitle = "物料查询";
  48. pr_code.Condition = "pr_code not like '%LX%'";
  49. pr_code.SetValueField = new string[] { "pr_code" };
  50. dh = SystemInf.dh;
  51. }
  52. private void nr_rule_DBChange(object sender, EventArgs e)
  53. {
  54. Dbfind = ps_prodcode.ReturnData;
  55. BaseUtil.SetFormValue(this.Controls, Dbfind);
  56. }
  57. void uploadfile()
  58. {
  59. if (FilePath.Text == "")
  60. {
  61. MessageBox.Show("文件不能为空!");
  62. return;
  63. }
  64. //if (pr_code.Text == "")
  65. //{
  66. // MessageBox.Show("产品编号不能为空!");
  67. // return;
  68. //}
  69. if (JPG.Checked)
  70. {
  71. ps_prodcode.Text = FilePath.Text.Substring(FilePath.Text.LastIndexOf(@"\") + 1).Replace(".xls", "").Replace(".xlsx", "");
  72. pr_code_Leave(new object(), new EventArgs());
  73. DataTable dt = (DataTable)dh.ExecuteSql("select * from productsop where ps_prodcode='" + ps_prodcode.Text + "' ", "select");
  74. if (dt.Rows.Count == 0)
  75. {
  76. string code = LogicHandler.GetPiInoutCode("ProductSOP", "1");
  77. ps_code.Text = code;
  78. if (VaildDate.Checked)
  79. {
  80. dh.ExecuteSql("insert into productsop(ps_id,ps_code,ps_prodcode,ps_indate,ps_inman,ps_status,ps_statuscode,ps_vailddate,ps_prcode)values(productsop_seq.nextval,'" + code + "','" + ps_prodcode.Text + "',sysdate,'" + User.UserName + "','在录入','ENTERING',to_date('" + DateCode.Value.ToString("yyyy-MM-dd") + "','yyyy-mm-dd'),'" + pr_code.Text + "')", "insert");
  81. }
  82. else
  83. {
  84. dh.ExecuteSql("insert into productsop(ps_id,ps_code,ps_prodcode,ps_indate,ps_inman,ps_status,ps_statuscode,ps_prcode)values(productsop_seq.nextval,'" + code + "','" + ps_prodcode.Text + "',sysdate,'" + User.UserName + "','在录入','ENTERING','" + pr_code.Text + "')", "insert");
  85. }
  86. }
  87. else
  88. {
  89. string ps_id = dt.Rows[0]["ps_id"].ToString();
  90. dh.ExecuteSql("delete from ProductSOPdetail where psd_psid='" + ps_id + "'", "delete");
  91. ps_prodcode.Text = dt.Rows[0]["ps_prodcode"].ToString();
  92. }
  93. Workbook workbook = new Workbook(FilePath.Text);
  94. List<Worksheet> list = new List<Worksheet>();
  95. List<string> filename = new List<string>();
  96. if (VaildDate.Checked)
  97. {
  98. dh.ExecuteSql("update ProductSOP set ps_attachsop='',ps_inman='" + User.UserName + "',ps_vailddate=to_date('" + DateCode.Value.ToString("yyyy-MM-dd") + "','yyyy-mm-dd') where ps_prodcode='" + ps_prodcode.Text + "'", "update");
  99. }
  100. else
  101. {
  102. dh.ExecuteSql("update ProductSOP set ps_attachsop='',ps_inman='" + User.UserName + "' where ps_prodcode='" + ps_prodcode.Text + "'", "update");
  103. }
  104. OperatResult.AppendText("一共Sheet: " + workbook.Worksheets.Count + "\n");
  105. for (int i = 0; i < workbook.Worksheets.Count; i++)
  106. {
  107. OperatResult.AppendText("正在解析Sheet" + workbook.Worksheets[i].Name + "\n");
  108. if (JPG.Checked)
  109. {
  110. try
  111. {
  112. if (!Directory.Exists(Application.StartupPath + @"\" + ps_prodcode.Text))
  113. {
  114. Directory.CreateDirectory(Application.StartupPath + @"\" + ps_prodcode.Text);
  115. }
  116. string path = Application.StartupPath + @"\" + ps_prodcode.Text + @"\";
  117. Worksheet she = workbook.Worksheets[i];
  118. // 获取原表的实际数据范围和最大列
  119. int totalRows = she.Cells.MaxDataRow+1;
  120. int lastColIndex = she.Cells.MaxDataColumn + 5;
  121. string lastColLetter = CellsHelper.ColumnIndexToName(lastColIndex);
  122. int rowSliceSize = int.Parse(RowCount.Value.ToString());
  123. int imageIndex = 1;
  124. // ========== 核心修改:手动计算总页数 ==========
  125. // 总页数 = 总行数 除以 每页行数,向上取整
  126. int totalPages = (int)Math.Ceiling((double)totalRows / rowSliceSize);
  127. // ============================================
  128. // 记录原表设置,循环结束后恢复,避免污染原文件
  129. string originalPrintArea = she.PageSetup.PrintArea;
  130. double originalLeftMargin = she.PageSetup.LeftMargin;
  131. double originalRightMargin = she.PageSetup.RightMargin;
  132. double originalTopMargin = she.PageSetup.TopMargin;
  133. double originalBottomMargin = she.PageSetup.BottomMargin;
  134. // 设置居中、无边距的打印模式,确保图片紧密贴合
  135. she.PageSetup.CenterHorizontally = true;
  136. she.PageSetup.CenterVertically = true;
  137. she.PageSetup.LeftMargin = 0;
  138. she.PageSetup.RightMargin = 0;
  139. she.PageSetup.TopMargin = 0;
  140. she.PageSetup.BottomMargin = 0;
  141. // 循环开始前先彻底清空原有页脚,防止残留格式干扰
  142. she.PageSetup.SetFooter(0, "");
  143. she.PageSetup.SetFooter(1, "");
  144. she.PageSetup.SetFooter(2, "");
  145. for (int startRow = 0; startRow < totalRows; startRow += rowSliceSize)
  146. {
  147. int endRow = Math.Min(startRow + rowSliceSize - 1, totalRows - 1);
  148. // 1. 动态设置原表的打印区域(模拟切片)
  149. if (endRow < rowSliceSize)
  150. {
  151. Console.WriteLine($"A{startRow + 1}:{lastColLetter}{rowSliceSize + 1}");
  152. she.PageSetup.PrintArea = $"A{startRow + 1}:{lastColLetter}{rowSliceSize + 1}";
  153. }
  154. else
  155. {
  156. Console.WriteLine($"A{startRow + 1}:{lastColLetter}{endRow + 1}");
  157. she.PageSetup.PrintArea = $"A{startRow + 1}:{lastColLetter}{endRow + 1}";
  158. }
  159. if (File.Exists(PrintPath.Text))
  160. {
  161. if (startRow == 0)
  162. {
  163. she.Pictures.Add(startRow + int.Parse(Y.Value.ToString()), int.Parse(X.Value.ToString()), PrintPath.Text);
  164. }
  165. else
  166. {
  167. she.Pictures.Add(startRow - 4 + int.Parse(Y.Value.ToString()), int.Parse(X.Value.ToString()), PrintPath.Text);
  168. }
  169. }
  170. // ========== 核心修改:手动写入硬编码页脚 ==========
  171. // 当前 page 是 imageIndex,总页数是我们刚才算的 totalPages
  172. string pageFooter = $"第 {imageIndex} 页 / 共 {totalPages} 页";
  173. // 直接写入页脚中间位置
  174. she.PageSetup.SetFooter(1, pageFooter);
  175. // ================================================
  176. // 2. 创建渲染选项
  177. ImageOrPrintOptions options = new ImageOrPrintOptions();
  178. options.ImageFormat = ImageFormat.Png;
  179. options.OnePagePerSheet = false;
  180. options.PrintingPage = PrintingPageType.Default;
  181. // 3. 渲染图片
  182. SheetRender sr = new SheetRender(she, options);
  183. Bitmap bitmap = sr.ToImage(0);
  184. // 4. 处理并保存图片
  185. string imagename = $"{she.Name}_{imageIndex}.png";
  186. string imagePath = Path.Combine(path, imagename);
  187. bitmap.Save(imagePath, ImageFormat.Png);
  188. bitmap.Dispose();
  189. //sr.Dispose(); // 如果旧版本这里报错,可以保留注释
  190. imageIndex++; // 这一行对应下一张图片的页码,放在这里即可
  191. OperatResult.AppendText("解析图片【" + imagePath + "】" + "\n");
  192. // 5. 上传与入库
  193. Dictionary<string, object> dic = new Dictionary<string, object>();
  194. dic.Add("em_name", "管理员");
  195. dic.Add("em_code", "ADMIN");
  196. dic.Add("caller", "ProductSOP");
  197. OperatResult.AppendText("上传文件【" + imagePath + "】\n");
  198. string fp_id = UploadFilesToRemoteUrl("https://mes.lihantech.cn/mes/MEScommon/uploadFiles.action?_noc=1", imagePath, dic);
  199. string ps_id = dh.getFieldDataByCondition("ProductSOP", "ps_id", "ps_prodcode='" + ps_prodcode.Text + "'").ToString();
  200. dh.ExecuteSql("insert into ProductSOPdetail(psd_id,psd_psid,psd_attach) select ProductSOPdetail_seq.nextval,ps_id,'" + imagename + ";" + fp_id + "' from ProductSOP where ps_prodcode='" + ps_prodcode.Text + "'", "insert");
  201. }
  202. // 恢复原表设置
  203. she.PageSetup.PrintArea = originalPrintArea;
  204. she.PageSetup.LeftMargin = originalLeftMargin;
  205. she.PageSetup.RightMargin = originalRightMargin;
  206. she.PageSetup.TopMargin = originalTopMargin;
  207. she.PageSetup.BottomMargin = originalBottomMargin;
  208. // 循环结束后彻底清空页脚,防止影响下次使用
  209. she.PageSetup.SetFooter(0, "");
  210. she.PageSetup.SetFooter(1, "");
  211. she.PageSetup.SetFooter(2, "");
  212. }
  213. catch (Exception ex)
  214. {
  215. OperatResult.AppendText(ex.Message + ex.StackTrace);
  216. }
  217. }
  218. }
  219. }
  220. else if (PDF.Checked)
  221. {
  222. ps_prodcode.Text = FilePath.Text.Substring(FilePath.Text.LastIndexOf(@"\") + 1).Replace(".pdf", "");
  223. pr_code_Leave(new object(), new EventArgs());
  224. DataTable dt = (DataTable)dh.ExecuteSql("select * from productsop where ps_prodcode='" + ps_prodcode.Text + "' ", "select");
  225. if (dt.Rows.Count == 0)
  226. {
  227. string code = LogicHandler.GetPiInoutCode("ProductSOP", "1");
  228. ps_code.Text = code;
  229. if (VaildDate.Checked)
  230. {
  231. dh.ExecuteSql("insert into productsop(ps_id,ps_code,ps_prodcode,ps_indate,ps_inman,ps_status,ps_statuscode,ps_vailddate)values(productsop_seq.nextval,'" + code + "','" + ps_prodcode.Text + "',sysdate,'" + User.UserName + "','在录入','ENTERING',to_date('" + DateCode.Value.ToString("yyyy-MM-dd") + "','yyyy-mm-dd'))", "insert");
  232. }
  233. else
  234. {
  235. dh.ExecuteSql("insert into productsop(ps_id,ps_code,ps_prodcode,ps_indate,ps_inman,ps_status,ps_statuscode)values(productsop_seq.nextval,'" + code + "','" + ps_prodcode.Text + "',sysdate,'" + User.UserName + "','在录入','ENTERING')", "insert");
  236. }
  237. }
  238. else
  239. {
  240. string psid = dt.Rows[0]["ps_id"].ToString();
  241. dh.ExecuteSql("delete from ProductSOPdetail where psd_psid='" + psid + "'", "delete");
  242. ps_prodcode.Text = dt.Rows[0]["ps_prodcode"].ToString();
  243. }
  244. if (VaildDate.Checked)
  245. {
  246. dh.ExecuteSql("update ProductSOP set ps_attachsop='',ps_inman='" + User.UserName + "',ps_vailddate=to_date('" + DateCode.Value.ToString("yyyy-MM-dd") + "','yyyy-mm-dd') where ps_prodcode='" + ps_prodcode.Text + "'", "update");
  247. }
  248. else
  249. {
  250. dh.ExecuteSql("update ProductSOP set ps_attachsop='',ps_inman='" + User.UserName + "' where ps_prodcode='" + ps_prodcode.Text + "'", "update");
  251. }
  252. Dictionary<string, object> dic = new Dictionary<string, object>();
  253. dic.Add("em_name", "管理员");
  254. dic.Add("em_code", "ADMIN");
  255. dic.Add("caller", "ProductSOP");
  256. OperatResult.AppendText("上传文件【" + ps_prodcode.Text + "" + "】\n");
  257. string fp_id = UploadFilesToRemoteUrl("https://mes.lihantech.cn/mes/MEScommon/uploadFiles.action?_noc=1", FilePath.Text, dic);
  258. string ps_id = dh.getFieldDataByCondition("ProductSOP", "ps_id", "ps_prodcode='" + ps_prodcode.Text + "'").ToString();
  259. dh.ExecuteSql("insert into ProductSOPdetail(psd_id,psd_psid,psd_attach) select ProductSOPdetail_seq.nextval,ps_id,'" + ps_prodcode.Text + ";" + fp_id + "' from ProductSOP where ps_prodcode='" + ps_prodcode.Text + "'", "insert");
  260. }
  261. }
  262. /// <summary>
  263. /// 请求上传图片到阿里云
  264. /// </summary>
  265. /// <param name="url">上传地址</param>
  266. /// <param name="filepath">本地文件路径</param>
  267. /// <param name="dic">上传的数据信息</param>
  268. /// <returns></returns>
  269. public string UploadFilesToRemoteUrl(string url1, string filepath, Dictionary<string, object> dic)
  270. {
  271. try
  272. {
  273. ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
  274. ServicePointManager.DefaultConnectionLimit = 50;
  275. string boundary = DateTime.Now.Ticks.ToString("x");
  276. byte[] boundarybytes = System.Text.Encoding.UTF8.GetBytes("--" + boundary + "\r\n");
  277. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url1);
  278. request.Method = "POST";
  279. request.Timeout = 10 * 10000;
  280. request.ContentType = "multipart/form-data; boundary=" + boundary;
  281. Stream rs = request.GetRequestStream();
  282. var endBoundaryBytes = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n");
  283. string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n" + "\r\n" + "{1}" + "\r\n";
  284. if (dic != null)
  285. {
  286. foreach (string key in dic.Keys)
  287. {
  288. rs.Write(boundarybytes, 0, boundarybytes.Length);
  289. string formitem = string.Format(formdataTemplate, key, dic[key]);
  290. byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
  291. rs.Write(formitembytes, 0, formitembytes.Length);
  292. }
  293. }
  294. string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n\r\n";
  295. {
  296. rs.Write(boundarybytes, 0, boundarybytes.Length);
  297. var header = string.Format(headerTemplate, "file", Path.GetFileName(filepath));
  298. var headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
  299. rs.Write(headerbytes, 0, headerbytes.Length);
  300. using (var fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read))
  301. {
  302. var buffer = new byte[1024];
  303. var bytesRead = 0;
  304. while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
  305. {
  306. rs.Write(buffer, 0, bytesRead);
  307. }
  308. }
  309. var cr = Encoding.UTF8.GetBytes("\r\n");
  310. rs.Write(cr, 0, cr.Length);
  311. }
  312. rs.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
  313. var response = request.GetResponse() as HttpWebResponse;
  314. StreamReader newReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
  315. string Content = newReader.ReadToEnd();
  316. Dictionary<string, object> dic1 = new Dictionary<string, object>();
  317. List<Dictionary<string, object>> dic2 = null;
  318. dic1 = BaseUtil.ToDictionary(Content);
  319. dic2 = dic1["data"] as List<Dictionary<string, object>>;
  320. string fp_id = "";
  321. if (dic2[0]["filepath"] != null)
  322. {
  323. fp_id = dic2[0]["filepath"].ToString();
  324. }
  325. if (response.StatusCode == HttpStatusCode.OK)
  326. {
  327. return fp_id;
  328. }
  329. }
  330. catch (Exception e)
  331. {
  332. LogicHandler.DoCommandLog("SOP", usercode, "", "", sourcecode, "上传SOP", "上传失败", ps_prodcode.Text, "");
  333. Console.WriteLine(e.Message + e.StackTrace);
  334. }
  335. return "";
  336. }
  337. private void ChooseFile_Click(object sender, EventArgs e)
  338. {
  339. ChoosePath.Filter = "(*.xlsx)|*.xlsx|(*.xls)|*.xls|(*.pdf)|*.pdf";
  340. DialogResult result = ChoosePath.ShowDialog();
  341. if (result == DialogResult.OK)
  342. {
  343. FilePath.Text = ChoosePath.FileName;
  344. }
  345. }
  346. private void UploadSOP_Click(object sender, EventArgs e)
  347. {
  348. Thread thread = new Thread(uploadfile);
  349. SetLoadingWindow stw = new SetLoadingWindow(thread, "上传文件");
  350. stw.StartPosition = FormStartPosition.CenterScreen;
  351. stw.ShowDialog();
  352. if (JPG.Checked)
  353. {
  354. LogicHandler.DoCommandLog("SOP", usercode, "", "", sourcecode, "上传SOP【图片格式】", "上传成功", ps_prodcode.Text, "");
  355. }
  356. else
  357. {
  358. LogicHandler.DoCommandLog("SOP", usercode, "", "", sourcecode, "上传SOP【PDF格式】", "上传成功", ps_prodcode.Text, "");
  359. }
  360. }
  361. private void OperatResult_TextChanged(object sender, EventArgs e)
  362. {
  363. //OperatResult.SelectionStart = Text.Length;
  364. OperatResult.ScrollToCaret();
  365. }
  366. private void SendSop_Click(object sender, EventArgs e)
  367. {
  368. try
  369. {
  370. if (ps_prodcode.Text == "")
  371. {
  372. MessageBox.Show("线别和SOP文档不能为空");
  373. return;
  374. }
  375. if (SendDGV.Rows.Count > 0)
  376. {
  377. List<Dictionary<string, string>> listr = new List<Dictionary<string, string>>();
  378. JavaScriptSerializer jss = new JavaScriptSerializer();
  379. Dictionary<string, object> map1 = new Dictionary<string, object>();
  380. Dictionary<string, object> map = new Dictionary<string, object>();
  381. string path;
  382. string pathroot = dh.GetConfig("filePathUrl", "sys").ToString();
  383. dh.ExecuteSql("insert into soprelease(SR_ID, sr_pscode, SR_STATUS, SR_MACHINETYPE,sr_releaseman,sr_readman)" +
  384. "select soprelease_seq.nextval,'" + ps_code.Text + "',0,'" + ps_prodcode.Text + "','" + User.UserName + "',em_code from employee ", "insert");
  385. LogicHandler.DoCommandLog("SOP", usercode, "", "", sourcecode, "广播SOP", "广播成功", ps_prodcode.Text, "");
  386. MessageBox.Show("文档[" + ps_prodcode.Text + "]下放成功");
  387. }
  388. else
  389. {
  390. MessageBox.Show("产品编号" + ps_prodcode.Text + "未维护SOP文档");
  391. LogicHandler.DoCommandLog("SOP", usercode, "", "", sourcecode, "广播SOP", "广播失败,未维护SOP文档", ps_prodcode.Text, "");
  392. }
  393. }
  394. catch (Exception ex)
  395. {
  396. LogicHandler.DoCommandLog("SOP", usercode, "", "", sourcecode, "广播SOP", "广播失败", ps_prodcode.Text, "");
  397. Console.WriteLine(ex.Message + ex.StackTrace);
  398. }
  399. }
  400. public string encryptBASE64(string key)
  401. {
  402. Console.WriteLine(key);
  403. byte[] bytes = Encoding.Default.GetBytes(key);
  404. return Convert.ToBase64String(bytes);
  405. }
  406. public static string EncodeBase64(string code_type, string code)
  407. {
  408. string encode = "";
  409. byte[] bytes = Encoding.GetEncoding(code_type).GetBytes(code);
  410. try
  411. {
  412. encode = Convert.ToBase64String(bytes);
  413. }
  414. catch
  415. {
  416. encode = code;
  417. }
  418. return encode;
  419. }
  420. private void pr_code_KeyDown(object sender, KeyEventArgs e)
  421. {
  422. if (e.KeyCode == Keys.Enter)
  423. {
  424. LoadPrCode();
  425. }
  426. }
  427. private BindingSource bs = new BindingSource(); // 类成员变量
  428. private void LoadPrCode()
  429. {
  430. DataTable dt = (DataTable)dh.ExecuteSql("select * from productsop where ps_prodcode='" + ps_prodcode.Text + "'", "select");
  431. if (dt.Rows.Count > 0)
  432. {
  433. string ps_id = dt.Rows[0]["ps_id"].ToString();
  434. dt = (DataTable)dh.ExecuteSql(@"select fp_name,fp_path,'' issend,psd_iskey
  435. from ProductSOPdetail
  436. left join filepath on FP_ID=substr(PSD_ATTACH,instr(PSD_ATTACH,';')+1)
  437. where psd_psid=" + ps_id, "select");
  438. if (dt.Rows.Count > 0)
  439. {
  440. // 在 UI 线程安全执行(如果已经在 UI 线程则直接调用)
  441. if (SendDGV.InvokeRequired)
  442. SendDGV.Invoke(new Action(() => BindData(dt)));
  443. else
  444. BindData(dt);
  445. }
  446. }
  447. }
  448. private void BindData(DataTable dt)
  449. {
  450. // 使用 BindingSource 避免直接置 null
  451. if (SendDGV.DataSource == null)
  452. {
  453. bs.DataSource = dt;
  454. SendDGV.DataSource = bs;
  455. }
  456. else
  457. {
  458. bs.DataSource = dt; // 直接替换内部数据,控件不会经历“无数据”状态
  459. }
  460. // 可选:刷新显示
  461. SendDGV.Refresh();
  462. }
  463. private void pr_code_Leave(object sender, EventArgs e)
  464. {
  465. //LoadPrCode();
  466. }
  467. bool AllChecked = false;
  468. private void ChooseALL_Click(object sender, EventArgs e)
  469. {
  470. if (AllChecked)
  471. {
  472. foreach (DataGridViewRow dr in SendDGV.Rows)
  473. dr.Cells[0].Value = false;
  474. AllChecked = false;
  475. }
  476. else
  477. {
  478. foreach (DataGridViewRow dr in SendDGV.Rows)
  479. dr.Cells[0].Value = true;
  480. AllChecked = true;
  481. }
  482. }
  483. private void ps_prodcode_UserControlTextChanged(object sender, EventArgs e)
  484. {
  485. LoadPrCode();
  486. }
  487. private void GetSOP_Click(object sender, EventArgs e)
  488. {
  489. LoadPrCode();
  490. }
  491. private void ChoosePrint_Click(object sender, EventArgs e)
  492. {
  493. ChoosePath.Filter = "(*.png)|*.png";
  494. DialogResult result = ChoosePath.ShowDialog();
  495. if (result == DialogResult.OK)
  496. {
  497. PrintPath.Text = ChoosePath.FileName;
  498. }
  499. }
  500. private void SendDGV_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
  501. {
  502. if (e.RowIndex < 0 || e.ColumnIndex < 0) return;
  503. }
  504. }
  505. }