DataGridView添加另外一个控件。

2020-12-13 04:38

阅读:473

标签:des   datagridview   style   blog   http   color   

soscw.com,搜素材soscw.com,搜素材
  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Drawing;
  5 using System.Data;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 using System.Xml;
 10 
 11 namespace XjxMobilenursing
 12 {
 13     public partial class frmNurseList : UserControl
 14     {
 15         #region 全局变量
 16         private frmNurse myparentform = null; //父窗体
 17         private string strtemplate = "";      //Web Service 返回的XML数据
 18         private bool bolLook = true;         //是否锁定
 19         private ComboBox mySingleVeto;
 20         private DataGridView mypopudatagridview = null;
 21         private int iRowIndex = 0;
 22         private string strrq1 = "";
 23         private int yzlx = 0;
 24         private Nurseinfo fNurseinfo = null; 
 25      //   private Dictionary conDataitems = new Dictionary();
 26         #endregion
 27         public frmNurseList()
 28         {
 29             InitializeComponent();
 30         }
 31         #region 类成员GET和SET方法
 32         public frmNurse OwnerParentForm
 33         {
 34             get
 35             { return myparentform; }
 36             set
 37             { this.myparentform = value; }
 38         }
 39         public bool bLook
 40         {
 41             get
 42             {
 43                 return this.bolLook;
 44             }
 45             set
 46             {
 47                 this.bolLook = value;
 48             }
 49         }
 50         public string RQ1
 51         {
 52             get
 53             {
 54                 if (!StringCommon.IsDate(this.strrq1))
 55                 {
 56                     this.strrq1 = DateTime.Now.ToString("yyyy-MM-dd");
 57 
 58                 }
 59                 return strrq1;
 60             }
 61             set
 62             {
 63                 this.strrq1 = value;
 64             }
 65         }
 66         //public Dictionary conData
 67         //{
 68         //    get
 69         //    {
 70         //        return conDataitems;
 71         //    }
 72         //    set { conDataitems = value; }
 73         //}
 74         #endregion
 75         #region 重置dataGridView数据
 76         public void ReflashTable()
 77         {
 78             try
 79             {
 80                 if (this.NurseGridList == null) return;
 81                 DataTable dt = (DataTable)NurseGridList.DataSource;
 82                 if (dt != null)
 83                 {
 84                     dt.Rows.Clear();
 85                     NurseGridList.DataSource = dt;
 86                 }
 87             }
 88             catch (Exception ex)
 89             {
 90                 MessageBox.Show(ex.Source + ":" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
 91             }
 92         }
 93         #endregion
 94         #region get护理单记录信息XML
 95         public bool getNurseListInfo(string rq1, string rq2)
 96         {
 97             try
 98             {
 99                 if (this.OwnerParentForm == null)
100                     return false;
101                 if (!StringCommon.IsDate(rq1))
102                 {
103                     rq1 = DateTime.Parse(this.OwnerParentForm.OwnerParentForm.CurrentPatientInfo.rysj).ToString("yyyy-MM-dd");
104                 }
105                 if (!StringCommon.IsDate(rq2))
106                 {
107                     rq2 = DateTime.Now.ToString("yyyy-MM-dd");
108                 }
109                 string strresult;
110                 this.strtemplate = "";
111                 strresult = this.OwnerParentForm.OwnerParentForm.Xjxinfoweb.GetNurseTableinfo(Properties.Settings.Default.strapplication_account,
112                     this.OwnerParentForm.OwnerParentForm.CurrentPatientInfo.lsh, rq1, rq2);
113 
114                 if (strresult.IndexOf("error") > -1)
115                 {
116                     return false;
117                 }
118                 else if (strresult == "")
119                 {
120                     return false;
121                 }
122                 this.strtemplate = strresult;
123                 return true;
124             }
125             catch (Exception ex)
126             {
127                 MessageBox.Show(ex.Source + ":" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
128                 return false;
129             }
130         }
131         #endregion
132         #region Set护理单信息XML到DataTable
133         private DataTable FromXML2DataTable(string XMLStr)
134         {
135             XmlDocument myDoc = new XmlDocument();
136             myDoc.LoadXml(XMLStr);
137             if (string.IsNullOrEmpty(XMLStr) || !myDoc.HasChildNodes) return null;
138             DataTable dt = new DataTable();
139             DataRow row = null;
140             DataColumn col = null;
141             //填充数据
142             foreach (XmlElement el in myDoc.SelectNodes("Nurseinfos/row"))
143             {
144                 row = dt.NewRow();
145                 foreach (XmlNode node in el.ChildNodes)
146                 {
147                     if (!dt.Columns.Contains(node.Name))
148                     {
149                         col = new DataColumn();
150                         col.ColumnName = node.Name;
151                         dt.Columns.Add(col);
152                     }
153                     row[node.Name] = node.InnerText;
154                 }
155                 dt.Rows.Add(row);
156             }
157             return dt;
158         }
159         #endregion
160         #region Set护理单信息DataTable到GridView
161         public void ShowNurseTable()
162         {
163             DataTable ds = FromXML2DataTable(this.strtemplate);
164             if (ds != null && ds.Rows.Count > 0)
165             {
166                 NurseGridList.AutoGenerateColumns = false;
167                 NurseGridList.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
168                 NurseGridList.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
169                 NurseGridList.DataSource = ds;
170                 NurseGridList.ClearSelection();
171             }
172         }
173         #endregion
174         #region Set护理单GridView可编辑
175         public void SetEditNurseTable()
176         {
177             try
178             {
179                 this.bLook = false;
180                 this.NurseGridList.ReadOnly = this.bolLook;
181                 this.NurseGridList.AllowUserToAddRows = !this.bolLook;
182                 if (this.NurseGridList.RowCount 0) return;
183                 this.NurseGridList.FirstDisplayedScrollingRowIndex = this.NurseGridList.Rows.Count - 1;
184                 this.NurseGridList.Rows[this.NurseGridList.Rows.Count - 1].Selected = true;
185                 this.NurseGridList.CurrentCell = this.NurseGridList.Rows[this.NurseGridList.Rows.Count - 1].Cells["rqsj"];
186                 this.NurseGridList.Focus();
187                 this.NurseGridList.BeginEdit(false);
188             }
189             catch (Exception ex)
190             {
191                 MessageBox.Show("对象:" + this.GetType().Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "异常信息:" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
192             }
193         }
194         #endregion
195         #region Set护理单GridView删除
196         public void SetdelNurseTable()
197         {
198             try
199             {
200                 fNurseinfo = FillNurseinfo();
201                 this.yzlx = 3;
202                 string requestParam = this.PkgCtrlParams(yzlx, fNurseinfo);
203                 string rtn = OwnerParentForm.OwnerParentForm.Xjxinfoweb.SetNurseRequest(requestParam);
204                 MessageBox.Show(rtn);
205             }
206             catch (Exception ex)
207             {
208                 MessageBox.Show("对象:" + this.GetType().Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "异常信息:" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
209             }
210         }
211         #endregion
212         #region 窗体初始化
213         private void frmNurseList_Load(object sender, EventArgs e)
214         {
215             try
216             {
217                 this.mySingleVeto = new ComboBox();
218                 this.mySingleVeto.FormattingEnabled = true;
219                 this.mySingleVeto.Items.AddRange(new object[] {
220             "",
221             "True",
222             "False"});
223                 this.mySingleVeto.BringToFront();
224                 this.mySingleVeto.SelectedIndexChanged += new System.EventHandler(this.mySingleVeto_SelectedIndexChanged);
225                 //this.mySingleVeto.Click += new System.EventHandler(this.mySingleVeto_Click);
226                 this.mySingleVeto.KeyUp += new System.Windows.Forms.KeyEventHandler(this.mySingleVeto_KeyUp);
227                 this.mySingleVeto.Visible = false;
228                 InitDataGridView(this.NurseGridList, "ZY_tHLDTJLD");
229                 this.NurseGridList.Controls.Add(this.mySingleVeto);
230 
231                 mypopudatagridview = new DataGridView();
232                 mypopudatagridview.Width = 200;
233                 mypopudatagridview.Height = 200;
234                 mypopudatagridview.AllowUserToAddRows = false;
235                 mypopudatagridview.AllowUserToDeleteRows = false;
236                 mypopudatagridview.AllowUserToResizeRows = false;
237                 mypopudatagridview.BackgroundColor = System.Drawing.Color.White;
238                 mypopudatagridview.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
239                 mypopudatagridview.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.mypopudatagridview_CellClick);
240                 mypopudatagridview.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
241                 mypopudatagridview.MultiSelect = false;
242                 mypopudatagridview.Name = "mypopudatagridview";
243                 mypopudatagridview.ReadOnly = true;
244                 mypopudatagridview.RowTemplate.Height = 23;
245                 mypopudatagridview.Cursor = Cursors.Hand;
246                 mypopudatagridview.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
247                 mypopudatagridview.AllowUserToOrderColumns = false;
248                 mypopudatagridview.RowHeadersVisible = false;
249                 mypopudatagridview.Visible = false;
250                 this.NurseGridList.Controls.Add(mypopudatagridview);
251                 InitPopDataGridView(mypopudatagridview, "XT_TCSSZ");
252             }
253             catch (Exception ex)
254             {
255                 MessageBox.Show("对象:" + this.GetType().Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "异常信息:" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
256             }
257         }
258         #endregion
259         #region 主GridView初始化
260         private void InitDataGridView(DataGridView sender, string strName)
261         {
262             try
263             {
264                 DataTable dt = new DataTable(strName);
265                 dt.Columns.Add("jlid");
266                 dt.Columns.Add("lsh");
267                 dt.Columns.Add("JLR");
268                 dt.Columns.Add("rqsj");
269                 dt.Columns.Add("sz");
270                 dt.Columns.Add("xybhd");
271                 dt.Columns.Add("tk_y");
272                 dt.Columns.Add("tk_z");
273                 dt.Columns.Add("dgfs_y");
274                 dt.Columns.Add("dgfs_z");
275                 dt.Columns.Add("rl_mc");
276                 dt.Columns.Add("rllx");
277                 dt.Columns.Add("rl_l");
278                 dt.Columns.Add("cl_mc");
279                 dt.Columns.Add("cllx");
280                 dt.Columns.Add("cl_l");
281                 dt.Columns.Add("bbreathingmachine");
282                 dt.Columns.Add("hx");
283                 dt.Columns.Add("s");
284                 dt.Columns.Add("f");
285                 dt.Columns.Add("qm");
286                 sender.DataSource = dt;
287                 sender.AllowUserToOrderColumns = false;
288                 sender.RowHeadersVisible = false;
289                 sender.ReadOnly = this.bolLook;
290                 sender.AllowUserToAddRows = !this.bolLook;
291                 sender.Columns["jlid"].HeaderText = "jlid";
292                 sender.Columns["jlid"].Visible = false;
293                 sender.Columns["lsh"].HeaderText = "lsh";
294                 sender.Columns["lsh"].Visible = false;
295                 sender.Columns["JLR"].HeaderText = "JLR";
296                 sender.Columns["JLR"].Visible = false;
297 
298                 sender.Columns["rqsj"].HeaderText = "日期";
299                 sender.Columns["rqsj"].Width = 100;
300                 //  sender.Columns["rqsj"].ReadOnly = this.bolLook;
301                 sender.Columns["rqsj"].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
302 
303                 sender.Columns["sz"].HeaderText = "神志";
304                 sender.Columns["sz"].Width = 60;
305               //  sender.Columns["sz"].ReadOnly = this.bolLook;
306                 sender.Columns["sz"].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
307 
308                 sender.Columns["xybhd"].HeaderText = "血氧饱和度%";
309                 sender.Columns["xybhd"].Width = 150;
310                 //  sender.Columns["xybhd"].ReadOnly = this.bolLook;
311                 sender.Columns["xybhd"].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
312 
313                 sender.Columns["tk_y"].HeaderText = "对光反应右";
314                 sender.Columns["tk_y"].Width = 150;
315                 // sender.Columns["tk_y"].ReadOnly = this.bolLook;
316                 sender.Columns["tk_y"].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
317 
318                 sender.Columns["tk_z"].HeaderText = "对光反应左";
319                 sender.Columns["tk_z"].Width = 150;
320                 // sender.Columns["tk_z"].ReadOnly = this.bolLook;
321                 sender.Columns["tk_z"].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
322 
323                 sender.Columns["dgfs_y"].HeaderText = "瞳孔(右)";
324                 sender.Columns["dgfs_y"].Width = 100;
325                 //  sender.Columns["dgfs_y"].ReadOnly = this.bolLook;
326                 sender.Columns["dgfs_y"].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
327 
328                 sender.Columns["dgfs_z"].HeaderText = "瞳孔(左)";
329                 sender.Columns["dgfs_z"].Width = 100;
330                 //  sender.Columns["dgfs_z"].ReadOnly = this.bolLook;
331                 sender.Columns["dgfs_z"].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
332 
333                 sender.Columns["rl_mc"].HeaderText = "入量名称";
334                 sender.Columns["rl_mc"].Width = 150;
335                 // sender.Columns["rl_mc"].ReadOnly = this.bolLook;
336                 sender.Columns["rl_mc"].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
337 
338 
339                 sender.Columns["rllx"].HeaderText = "入量(单位)";
340                 sender.Columns["rllx"].Width = 150;
341                 //sender.Columns["rllx"].ReadOnly = this.bolLook;
342                 sender.Columns["rllx"].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
343 
344                 sender.Columns["rl_l"].HeaderText = "入量(量)";
345                 sender.Columns["rl_l"].Width = 100;
346                 //  sender.Columns["rl_l"].ReadOnly = this.bolLook;
347                 sender.Columns["rl_l"].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
348 
349                 sender.Columns["cl_mc"].HeaderText = " 出量名称";
350                 sender.Columns["cl_mc"].Width = 150;
351                 // sender.Columns["cl_mc"].ReadOnly = this.bolLook;
352                 sender.Columns["cl_mc"].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
353 
354 
355                 sender.Columns["cllx"].HeaderText = "出量(单位)";
356                 sender.Columns["cllx"].Width = 150;
357                 // sender.Columns["cllx"].ReadOnly = this.bolLook;
358                 sender.Columns["cllx"].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
359 
360                 sender.Columns["cl_l"].HeaderText = "出量(量)";
361                 sender.Columns["cl_l"].Width = 100;
362                 // sender.Columns["cl_l"].ReadOnly = this.bolLook;
363                 sender.Columns["cl_l"].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
364 
365                 sender.Columns["bbreathingmachine"].HeaderText = "呼吸机";
366                 sender.Columns["bbreathingmachine"].Width = 100;
367                 //sender.Columns["bbreathingmachine"].ReadOnly = this.bolLook;
368                 sender.Columns["bbreathingmachine"].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
369 
370                 sender.Columns["hx"].HeaderText = "呼吸";
371                 sender.Columns["hx"].Width = 60;
372                 // sender.Columns["hx"].ReadOnly = this.bolLook;
373                 sender.Columns["hx"].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
374 
375                 sender.Columns["s"].HeaderText = "";
376                 sender.Columns["s"].Width = 30;
377                 // sender.Columns["s"].ReadOnly = this.bolLook;
378                 sender.Columns["s"].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
379 
380                 sender.Columns["f"].HeaderText = "";
381                 sender.Columns["f"].Width = 30;
382                 // sender.Columns["f"].ReadOnly = this.bolLook;
383                 sender.Columns["f"].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
384 
385                 sender.Columns["qm"].HeaderText = "记录人";
386                 sender.Columns["qm"].Width = 100;
387                 //sender.Columns["qm"].ReadOnly = this.bolLook;
388                 sender.Columns["qm"].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
389 
390                 sender.Tag = strName;
391             }
392             catch (Exception ex)
393             {
394                 MessageBox.Show("对象:" + this.GetType().Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "异常信息:" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
395             }
396 
397         }
398         #endregion
399         #region 辅GridView初始化
400         private void InitPopDataGridView(DataGridView sender, string strName)
401         {
402             try
403             {
404                 DataTable dt = new DataTable(strName);
405                 dt.Columns.Add("id");
406                 dt.Columns.Add("mc");
407                 sender.DataSource = dt;
408                 sender.Columns["id"].HeaderText = "序号";
409                 sender.Columns["id"].Width = 60;
410                 sender.Columns["id"].ReadOnly = false;
411                 sender.Columns["id"].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
412                 sender.Columns["mc"].HeaderText = "名称";
413                 sender.Columns["mc"].Width = 260;
414                 sender.Columns["mc"].ReadOnly = false;
415                 sender.Columns["mc"].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
416             }
417             catch (Exception ex)
418             {
419                 MessageBox.Show("对象:" + this.GetType().Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "异常信息:" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
420             }
421 
422 
423 
424         }
425         #endregion
426         #region 主GridView触发事件
427         private void NurseGridList_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
428         {
429             if (this.bLook) return;
430             if (e.RowIndex 0)
431             {
432                 return;
433             }
434             if (this.NurseGridList.Columns[e.ColumnIndex].Name.ToString().ToUpper() == "rl_l".ToUpper()
435               || this.NurseGridList.Columns[e.ColumnIndex].Name.ToString().ToUpper() == "cl_l".ToUpper())
436             {
437                 string str = e.FormattedValue.ToString();
438                 this.NurseGridList.Rows[e.RowIndex].ErrorText = "";
439 
440                 if (XjxWordE.xjxformat.StringCommon.IsNumeric(str))
441                 {
442                     if (double.Parse(str) 0)
443                     {
444                         if (this.NurseGridList.Columns[e.ColumnIndex].Name.ToString().ToUpper() == "cl_l".ToUpper())
445                         {
446                             this.NurseGridList.Rows[e.RowIndex].ErrorText = "分值输入的必须是大于或等于0的数字,请重输";
447                         }
448                         else
449                         {
450                             this.NurseGridList.Rows[e.RowIndex].ErrorText = "数量输入的必须是大于或等于0的数字,请重输";
451                         }
45


评论


亲,登录后才可以留言!