获取WPF的DataGrid控件中,是否存在没有通过错误验证的Cell
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                获取WPF的DataGrid控件中,是否存在没有通过错误验证的Cell
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
                                
                            
                            
                            C# code  ? 
 
  
 
                        
                        
                        | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | ????????///?<summary> ????????///?獲取DataGrid的所有行是否存在驗(yàn)證錯(cuò)誤。 ????????///?</summary> ????????///?<param?name="dg">要檢查的DataGrid實(shí)例</param> ????????///?<returns>true?有錯(cuò),false?無(wú)錯(cuò)</returns> ????????public?static?bool?GetDataGridRowsHasError(DataGrid?dg) ????????{ ????????????bool?hasError?=?false?; ????????????for?(int?i?=?0;?i?<?dg.Items.Count;?i++) ????????????{ ????????????????DependencyObject?o?=?dg.ItemContainerGenerator.ContainerFromIndex(i); ????????????????hasError?=?Validation.GetHasError(o); ????????????????if?(hasError) ????????????????{ ????????????????????break; ????????????????} ????????????} ????????????return?hasError; ????????} | 
判斷有驗(yàn)證錯(cuò)誤?就不執(zhí)行提交。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | ///?<summary> ????????///?獲取DataGrid的第一個(gè)被發(fā)現(xiàn)的驗(yàn)證錯(cuò)誤結(jié)果。 ????????///?</summary> ????????///?<param?name="dg">被檢查的DataGrid實(shí)例。</param> ????????///?<returns>錯(cuò)誤結(jié)果。</returns> ????????public?static?ValidationError?GetDataGridRowsFirstError(DataGrid?dg) ????????{ ????????????ValidationError?err=null; ????????????for?(int?i?=?0;?i?<?dg.Items.Count;?i++) ????????????{ ????????????????DependencyObject?o?=?dg.ItemContainerGenerator.ContainerFromIndex(i); ????????????????bool?hasError?=?Validation.GetHasError(o); ????????????????if?(hasError) ????????????????{ ????????????????????err?=?Validation.GetErrors(o)[0]; ????????????????????break; ????????????????} ????????????} ????????????return?err; ????????} ????????///?<summary> ????????///?執(zhí)行檢查DataGrid,并提示第一個(gè)錯(cuò)誤。重新定位到錯(cuò)誤單元格。 ????????///?</summary> ????????///?<param?name="dg">被檢查的DataGrid實(shí)例。</param> ????????///?<returns>true?有錯(cuò)并定位,false?無(wú)錯(cuò)、返回</returns> ????????public?static?bool?ExcutedCheckedDataGridValidation(DataGrid?dg) ????????{ ????????????ValidationError?err?=?GetDataGridRowsFirstError(dg); ????????????if?(err?!=?null) ????????????{ ????????????????string?errColName?=?((System.Windows.Data.BindingExpression)err.BindingInError).ParentBinding.Path.Path; ????????????????DataGridColumn?errCol?=?dg.Columns.Single(p?=> ????????????????{ ????????????????????if?(((Binding)((DataGridTextColumn)p).Binding).Path.Path?==?errColName) ????????????????????????return?true; ????????????????????else?return?false; ????????????????}); ????????????????//string?errRow?=?((DataRowView)((System.Windows.Data.BindingExpression)err.BindingInError).DataItem)["SWH"].ToString(); ????????????????//dg.Items.IndexOf(((System.Windows.Data.BindingExpression)err.BindingInError).DataItem); ????????????????dg.SelectedItem?=?((System.Windows.Data.BindingExpression)err.BindingInError).DataItem; ????????????????int?errRowIndex?=?dg.SelectedIndex; ????????????????MessageBox.Show(string.Format("第\"{0}\"行?的\"{1}\"列的單元格數(shù)據(jù)不合法(以紅色標(biāo)出),請(qǐng)?zhí)顚?xiě)正確后再執(zhí)行其他操作。",?errRowIndex?+?1,?errCol.Header),?"系統(tǒng)消息",?MessageBoxButton.OK,?MessageBoxImage.Warning); ????????????????dg.CurrentCell?=?new?DataGridCellInfo(dg.SelectedItem,?errCol); ????????????????if?(!((DataRowView)dg.CurrentItem).IsEdit) ????????????????{ ????????????????????((DataRowView)dg.CurrentItem).BeginEdit(); ????????????????} ????????????????TextBox?txt?=?dg.CurrentColumn.GetCellContent(dg.CurrentItem)?as?TextBox; ????????????????txt.Focus(); ????????????????return?true; ????????????} ????????????else ????????????{ ????????????????return?false; ????????????} ????????} | 
轉(zhuǎn)載于:https://www.cnblogs.com/sjqq/p/8032201.html
總結(jié)
以上是生活随笔為你收集整理的获取WPF的DataGrid控件中,是否存在没有通过错误验证的Cell的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
                            
                        - 上一篇: js ajax数据的获取小示例 天气
 - 下一篇: SecureCRT同时向多个终端发送命令