Query_SOP.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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 lastColIndex = she.Cells.MaxDataColumn + 3;
  120. string lastColLetter = CellsHelper.ColumnIndexToName(lastColIndex);
  121. int headerRows = 3;
  122. int dataRowsPerPage = int.Parse(RowCount.Value.ToString());
  123. int totalDataRows = 0;
  124. for (int r = headerRows; r <= she.Cells.MaxDataRow; r++)
  125. {
  126. bool hasData = false;
  127. for (int c = 0; c <= she.Cells.MaxDataColumn; c++)
  128. {
  129. if (she.Cells[r, c].Value != null && !string.IsNullOrWhiteSpace(she.Cells[r, c].Value.ToString()))
  130. {
  131. hasData = true;
  132. break;
  133. }
  134. }
  135. if (hasData)
  136. {
  137. totalDataRows++;
  138. }
  139. else
  140. {
  141. totalDataRows++;
  142. //break;
  143. }
  144. }
  145. int totalRows = headerRows + totalDataRows;
  146. int totalPages = (int)Math.Ceiling((double)totalDataRows / dataRowsPerPage);
  147. int imageIndex = 1;
  148. for (int page = 0; page < (totalPages == 0 ? 1 : totalPages); page++)
  149. {
  150. int newSheetIndex = workbook.Worksheets.Add();
  151. Worksheet newshe = workbook.Worksheets[newSheetIndex];
  152. newshe.Copy(she);
  153. newshe.Name = $"Temp_{page}";
  154. // 计算当前页的数据范围
  155. int dataStartRow = headerRows + page * dataRowsPerPage;
  156. int dataEndRow = Math.Min(headerRows + (page + 1) * dataRowsPerPage - 1, totalRows - 1);
  157. for (int r = totalRows - 1; r >= 0; r--)
  158. {
  159. if (r < headerRows || (r >= dataStartRow && r <= dataEndRow))
  160. {
  161. // 保留标题行和当前页数据行
  162. continue;
  163. }
  164. else
  165. {
  166. // 删除不需要的行
  167. newshe.Cells.DeleteRow(r);
  168. }
  169. }
  170. int newTotalRows = newshe.Cells.MaxDataRow + 1;
  171. string newPrintArea = "";
  172. if (totalPages == 0)
  173. {
  174. newPrintArea = $"A1:{lastColLetter}{25}";
  175. }
  176. else
  177. {
  178. newPrintArea = $"A1:{lastColLetter}{newTotalRows}";
  179. //newPrintArea = $"A1:{lastColLetter}{dataRowsPerPage}";
  180. }
  181. //Console.WriteLine(totalPages + " " + dataRowsPerPage + " " + newTotalRows);
  182. //Console.WriteLine($"A1:{lastColLetter}{(totalPages == 0 ? dataRowsPerPage : newTotalRows)}");
  183. //Console.WriteLine($"A1:{lastColLetter}{dataRowsPerPage}");
  184. newshe.PageSetup.PrintArea = newPrintArea;
  185. newshe.PageSetup.CenterHorizontally = true;
  186. newshe.PageSetup.CenterVertically = true;
  187. newshe.PageSetup.LeftMargin = 2;
  188. newshe.PageSetup.RightMargin = 2;
  189. newshe.PageSetup.TopMargin = 2;
  190. newshe.PageSetup.BottomMargin = 2;
  191. newshe.PageSetup.SetFooter(0, "");
  192. newshe.PageSetup.SetFooter(1, "");
  193. newshe.PageSetup.SetFooter(2, "");
  194. // ========== 添加图片/水印 ==========
  195. if (File.Exists(PrintPath.Text))
  196. {
  197. int rowOffset = int.Parse(Y.Value.ToString());
  198. newshe.Pictures.Add(rowOffset, int.Parse(X.Value.ToString()), PrintPath.Text);
  199. }
  200. string pageFooter = $"第 {imageIndex} 页 / 共 {totalPages} 页";
  201. newshe.PageSetup.SetFooter(1, pageFooter);
  202. ImageOrPrintOptions options = new ImageOrPrintOptions();
  203. options.ImageFormat = ImageFormat.Png;
  204. options.OnePagePerSheet = true; // 改为 true,确保只渲染一页
  205. options.PrintingPage = PrintingPageType.Default;
  206. SheetRender sr = new SheetRender(newshe, options);
  207. if (sr.PageCount > 0)
  208. {
  209. Bitmap bitmap = sr.ToImage(0);
  210. string imagename = $"{she.Name}_{imageIndex}.png";
  211. string imagePath = Path.Combine(path, imagename);
  212. bitmap.Save(imagePath, ImageFormat.Png);
  213. bitmap.Dispose();
  214. OperatResult.AppendText("解析图片【" + imagePath + "】" + "\n");
  215. // ========== 上传与入库 ==========
  216. Dictionary<string, object> dic = new Dictionary<string, object>();
  217. dic.Add("em_name", "管理员");
  218. dic.Add("em_code", "ADMIN");
  219. dic.Add("caller", "ProductSOP");
  220. OperatResult.AppendText("上传文件【" + imagePath + "】\n");
  221. string fp_id = UploadFilesToRemoteUrl("https://mes.lihantech.cn/mes/MEScommon/uploadFiles.action?_noc=1", imagePath, dic);
  222. string ps_id = dh.getFieldDataByCondition("ProductSOP", "ps_id", "ps_prodcode='" + ps_prodcode.Text + "'").ToString();
  223. 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");
  224. }
  225. // ========== 删除临时工作表 ==========
  226. workbook.Worksheets.RemoveAt(newSheetIndex);
  227. imageIndex++;
  228. }
  229. }
  230. catch (Exception ex)
  231. {
  232. OperatResult.AppendText(ex.Message + ex.StackTrace);
  233. }
  234. }
  235. }
  236. }
  237. else if (PDF.Checked)
  238. {
  239. ps_prodcode.Text = FilePath.Text.Substring(FilePath.Text.LastIndexOf(@"\") + 1).Replace(".pdf", "");
  240. pr_code_Leave(new object(), new EventArgs());
  241. DataTable dt = (DataTable)dh.ExecuteSql("select * from productsop where ps_prodcode='" + ps_prodcode.Text + "' ", "select");
  242. if (dt.Rows.Count == 0)
  243. {
  244. string code = LogicHandler.GetPiInoutCode("ProductSOP", "1");
  245. ps_code.Text = code;
  246. if (VaildDate.Checked)
  247. {
  248. 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");
  249. }
  250. else
  251. {
  252. 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");
  253. }
  254. }
  255. else
  256. {
  257. string psid = dt.Rows[0]["ps_id"].ToString();
  258. dh.ExecuteSql("delete from ProductSOPdetail where psd_psid='" + psid + "'", "delete");
  259. ps_prodcode.Text = dt.Rows[0]["ps_prodcode"].ToString();
  260. }
  261. if (VaildDate.Checked)
  262. {
  263. 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");
  264. }
  265. else
  266. {
  267. dh.ExecuteSql("update ProductSOP set ps_attachsop='',ps_inman='" + User.UserName + "' where ps_prodcode='" + ps_prodcode.Text + "'", "update");
  268. }
  269. Dictionary<string, object> dic = new Dictionary<string, object>();
  270. dic.Add("em_name", "管理员");
  271. dic.Add("em_code", "ADMIN");
  272. dic.Add("caller", "ProductSOP");
  273. OperatResult.AppendText("上传文件【" + ps_prodcode.Text + "" + "】\n");
  274. string fp_id = UploadFilesToRemoteUrl("https://mes.lihantech.cn/mes/MEScommon/uploadFiles.action?_noc=1", FilePath.Text, dic);
  275. string ps_id = dh.getFieldDataByCondition("ProductSOP", "ps_id", "ps_prodcode='" + ps_prodcode.Text + "'").ToString();
  276. 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");
  277. }
  278. }
  279. /// <summary>
  280. /// 请求上传图片到阿里云
  281. /// </summary>
  282. /// <param name="url">上传地址</param>
  283. /// <param name="filepath">本地文件路径</param>
  284. /// <param name="dic">上传的数据信息</param>
  285. /// <returns></returns>
  286. public string UploadFilesToRemoteUrl(string url1, string filepath, Dictionary<string, object> dic)
  287. {
  288. try
  289. {
  290. ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
  291. ServicePointManager.DefaultConnectionLimit = 50;
  292. string boundary = DateTime.Now.Ticks.ToString("x");
  293. byte[] boundarybytes = System.Text.Encoding.UTF8.GetBytes("--" + boundary + "\r\n");
  294. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url1);
  295. request.Method = "POST";
  296. request.Timeout = 10 * 10000;
  297. request.ContentType = "multipart/form-data; boundary=" + boundary;
  298. Stream rs = request.GetRequestStream();
  299. var endBoundaryBytes = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n");
  300. string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n" + "\r\n" + "{1}" + "\r\n";
  301. if (dic != null)
  302. {
  303. foreach (string key in dic.Keys)
  304. {
  305. rs.Write(boundarybytes, 0, boundarybytes.Length);
  306. string formitem = string.Format(formdataTemplate, key, dic[key]);
  307. byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
  308. rs.Write(formitembytes, 0, formitembytes.Length);
  309. }
  310. }
  311. string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n\r\n";
  312. {
  313. rs.Write(boundarybytes, 0, boundarybytes.Length);
  314. var header = string.Format(headerTemplate, "file", Path.GetFileName(filepath));
  315. var headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
  316. rs.Write(headerbytes, 0, headerbytes.Length);
  317. using (var fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read))
  318. {
  319. var buffer = new byte[1024];
  320. var bytesRead = 0;
  321. while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
  322. {
  323. rs.Write(buffer, 0, bytesRead);
  324. }
  325. }
  326. var cr = Encoding.UTF8.GetBytes("\r\n");
  327. rs.Write(cr, 0, cr.Length);
  328. }
  329. rs.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
  330. var response = request.GetResponse() as HttpWebResponse;
  331. StreamReader newReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
  332. string Content = newReader.ReadToEnd();
  333. Dictionary<string, object> dic1 = new Dictionary<string, object>();
  334. List<Dictionary<string, object>> dic2 = null;
  335. dic1 = BaseUtil.ToDictionary(Content);
  336. dic2 = dic1["data"] as List<Dictionary<string, object>>;
  337. string fp_id = "";
  338. if (dic2[0]["filepath"] != null)
  339. {
  340. fp_id = dic2[0]["filepath"].ToString();
  341. }
  342. if (response.StatusCode == HttpStatusCode.OK)
  343. {
  344. return fp_id;
  345. }
  346. }
  347. catch (Exception e)
  348. {
  349. LogicHandler.DoCommandLog("SOP", usercode, "", "", sourcecode, "上传SOP", "上传失败", ps_prodcode.Text, "");
  350. Console.WriteLine(e.Message + e.StackTrace);
  351. }
  352. return "";
  353. }
  354. private void ChooseFile_Click(object sender, EventArgs e)
  355. {
  356. ChoosePath.Filter = "(*.xlsx)|*.xlsx|(*.xls)|*.xls|(*.pdf)|*.pdf";
  357. DialogResult result = ChoosePath.ShowDialog();
  358. if (result == DialogResult.OK)
  359. {
  360. FilePath.Text = ChoosePath.FileName;
  361. }
  362. }
  363. private void UploadSOP_Click(object sender, EventArgs e)
  364. {
  365. Thread thread = new Thread(uploadfile);
  366. SetLoadingWindow stw = new SetLoadingWindow(thread, "上传文件");
  367. stw.StartPosition = FormStartPosition.CenterScreen;
  368. stw.ShowDialog();
  369. if (JPG.Checked)
  370. {
  371. LogicHandler.DoCommandLog("SOP", usercode, "", "", sourcecode, "上传SOP【图片格式】", "上传成功", ps_prodcode.Text, "");
  372. }
  373. else
  374. {
  375. LogicHandler.DoCommandLog("SOP", usercode, "", "", sourcecode, "上传SOP【PDF格式】", "上传成功", ps_prodcode.Text, "");
  376. }
  377. }
  378. private void OperatResult_TextChanged(object sender, EventArgs e)
  379. {
  380. //OperatResult.SelectionStart = Text.Length;
  381. OperatResult.ScrollToCaret();
  382. }
  383. private void SendSop_Click(object sender, EventArgs e)
  384. {
  385. try
  386. {
  387. if (ps_prodcode.Text == "")
  388. {
  389. MessageBox.Show("线别和SOP文档不能为空");
  390. return;
  391. }
  392. if (SendDGV.Rows.Count > 0)
  393. {
  394. List<Dictionary<string, string>> listr = new List<Dictionary<string, string>>();
  395. JavaScriptSerializer jss = new JavaScriptSerializer();
  396. Dictionary<string, object> map1 = new Dictionary<string, object>();
  397. Dictionary<string, object> map = new Dictionary<string, object>();
  398. string path;
  399. string pathroot = dh.GetConfig("filePathUrl", "sys").ToString();
  400. dh.ExecuteSql("insert into soprelease(SR_ID, sr_pscode, SR_STATUS, SR_MACHINETYPE,sr_releaseman,sr_readman)" +
  401. "select soprelease_seq.nextval,'" + ps_code.Text + "',0,'" + ps_prodcode.Text + "','" + User.UserName + "',em_code from employee ", "insert");
  402. LogicHandler.DoCommandLog("SOP", usercode, "", "", sourcecode, "广播SOP", "广播成功", ps_prodcode.Text, "");
  403. MessageBox.Show("文档[" + ps_prodcode.Text + "]下放成功");
  404. }
  405. else
  406. {
  407. MessageBox.Show("产品编号" + ps_prodcode.Text + "未维护SOP文档");
  408. LogicHandler.DoCommandLog("SOP", usercode, "", "", sourcecode, "广播SOP", "广播失败,未维护SOP文档", ps_prodcode.Text, "");
  409. }
  410. }
  411. catch (Exception ex)
  412. {
  413. LogicHandler.DoCommandLog("SOP", usercode, "", "", sourcecode, "广播SOP", "广播失败", ps_prodcode.Text, "");
  414. Console.WriteLine(ex.Message + ex.StackTrace);
  415. }
  416. }
  417. public string encryptBASE64(string key)
  418. {
  419. Console.WriteLine(key);
  420. byte[] bytes = Encoding.Default.GetBytes(key);
  421. return Convert.ToBase64String(bytes);
  422. }
  423. public static string EncodeBase64(string code_type, string code)
  424. {
  425. string encode = "";
  426. byte[] bytes = Encoding.GetEncoding(code_type).GetBytes(code);
  427. try
  428. {
  429. encode = Convert.ToBase64String(bytes);
  430. }
  431. catch
  432. {
  433. encode = code;
  434. }
  435. return encode;
  436. }
  437. private void pr_code_KeyDown(object sender, KeyEventArgs e)
  438. {
  439. if (e.KeyCode == Keys.Enter)
  440. {
  441. LoadPrCode();
  442. }
  443. }
  444. private BindingSource bs = new BindingSource(); // 类成员变量
  445. private void LoadPrCode()
  446. {
  447. DataTable dt = (DataTable)dh.ExecuteSql("select * from productsop where ps_prodcode='" + ps_prodcode.Text + "'", "select");
  448. if (dt.Rows.Count > 0)
  449. {
  450. string ps_id = dt.Rows[0]["ps_id"].ToString();
  451. dt = (DataTable)dh.ExecuteSql(@"select fp_name,fp_path,'' issend,psd_iskey
  452. from ProductSOPdetail
  453. left join filepath on FP_ID=substr(PSD_ATTACH,instr(PSD_ATTACH,';')+1)
  454. where psd_psid=" + ps_id, "select");
  455. if (dt.Rows.Count > 0)
  456. {
  457. // 在 UI 线程安全执行(如果已经在 UI 线程则直接调用)
  458. if (SendDGV.InvokeRequired)
  459. SendDGV.Invoke(new Action(() => BindData(dt)));
  460. else
  461. BindData(dt);
  462. }
  463. }
  464. }
  465. private void BindData(DataTable dt)
  466. {
  467. // 使用 BindingSource 避免直接置 null
  468. if (SendDGV.DataSource == null)
  469. {
  470. bs.DataSource = dt;
  471. SendDGV.DataSource = bs;
  472. }
  473. else
  474. {
  475. bs.DataSource = dt; // 直接替换内部数据,控件不会经历“无数据”状态
  476. }
  477. // 可选:刷新显示
  478. SendDGV.Refresh();
  479. }
  480. private void pr_code_Leave(object sender, EventArgs e)
  481. {
  482. //LoadPrCode();
  483. }
  484. bool AllChecked = false;
  485. private void ChooseALL_Click(object sender, EventArgs e)
  486. {
  487. if (AllChecked)
  488. {
  489. foreach (DataGridViewRow dr in SendDGV.Rows)
  490. dr.Cells[0].Value = false;
  491. AllChecked = false;
  492. }
  493. else
  494. {
  495. foreach (DataGridViewRow dr in SendDGV.Rows)
  496. dr.Cells[0].Value = true;
  497. AllChecked = true;
  498. }
  499. }
  500. private void ps_prodcode_UserControlTextChanged(object sender, EventArgs e)
  501. {
  502. LoadPrCode();
  503. }
  504. private void GetSOP_Click(object sender, EventArgs e)
  505. {
  506. LoadPrCode();
  507. }
  508. private void ChoosePrint_Click(object sender, EventArgs e)
  509. {
  510. ChoosePath.Filter = "(*.png)|*.png";
  511. DialogResult result = ChoosePath.ShowDialog();
  512. if (result == DialogResult.OK)
  513. {
  514. PrintPath.Text = ChoosePath.FileName;
  515. }
  516. }
  517. private void SendDGV_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
  518. {
  519. if (e.RowIndex < 0 || e.ColumnIndex < 0) return;
  520. }
  521. }
  522. }