C#實例應用總結(一)html
本人沒有系統學習過,C#,只是在工做中積累了一些,爲了方便之後開發中使用,在此作一下總結,若有不對的地方,歡迎各位觀衆多多批評與指正!java
您的意見,才能使你們更好的進步!望不吝指教!sql
概念就不說了,有興趣的話,能夠留言,之後再加上!數據庫
工具用的是VS2013,後臺用的java數組
1、gridView的增刪改查,ide
一、查詢工具
根據sql查詢,post
ServiceFactory.CreateService<IDeptParameterService>().queryWaitParameter(param).Then(list =>
{
this.gridControl1.DataSource = list;
});學習
ServiceResult<List<WaitParVo>> queryWaitParameter(string pkDept);字體
IDeptParameterService是個人service類,裏面只寫方法,頁面的上方法體裏,用「.」的方式,能夠調用出本身寫的方法,
list 用list變量來接收
this.gridControl1.DataSource = list;賦值給你要顯示的gridControl的列表上的數據源,列表上寫好對應的名稱便可!
ServiceResult<List<WaitParVo>> queryWaitParameter(string pkDept);是service層的方法,
WaitParVo爲該列表對應的實體類,
string pkDept爲參數,
queryWaitParameter方法名。
二、新增
gridView常常會用到新增的時候,在這裏,提供兩種方法,(固然也但願各位觀衆有更好的方法,多多指教。)
這兩種也是視條件來決定,有一種,只插入數據庫的方式,而後,刷新頁面,即新增一行。下面會細講!
主要講第二種,
gridView1.AddEmptyRow<WaitParVo>(row => string.IsNullOrEmpty(row.Code), new WaitParVo
{
Code = ar.CodeArgu,
Name = ar.NameArgu,
PkOrg = AppContext.Session.OrganizationId,
});
AddEmptyRow爲新增一行的方法,
WaitParVo,該gridView對應的實體類
row 變量名,
string.IsNullOrEmpty(row.Code)是否爲空判斷
{}裏面爲須要給列表賦的值。
可根據實際狀況改變。
其中ar是什麼?
這段代碼是我從本身的項目上粘出來的,講一下,爲何這麼寫
我當時的需求是這樣的

點擊右箭頭,變成這樣

點擊》按鈕,左側列表所有移動到右側,《和<同理
左側列表爲gridControl1,右側爲GridControl2
var ar = this.gridView2.GetFocusedRow() as ArguVo;
if(ar == null){
MessageBoxUtils.Hint("沒有已選參數了");
return;
}
gridView1.AddEmptyRow<WaitParVo>(row => string.IsNullOrEmpty(row.Code), new WaitParVo
{
Code = ar.CodeArgu,
Name = ar.NameArgu,
PkOrg = AppContext.Session.OrganizationId,
});
int rowindex = gridView2.FocusedRowHandle;
gridView2.DeleteRow(rowindex);
左右同理,先獲取到列表的光標選中行this.gridView2.GetFocusedRow() as ArguVo;
用變量來接收,增長以後,把獲取到的值,賦值給另外一個列表的對應字段。
int rowindex = gridView2.FocusedRowHandle;獲取選中行的下標,根據選中行的下標刪除。完成移動的操做!
在寫》的方法
private List<WaitParVo> WList;
private List<ArguVo> AList;
List<ArguVo> s = this.AList;
List<WaitParVo> d = this.WList;
if (d == null || d.Count < 1)
{
MessageBoxUtils.Hint("待選參數已全選");
return;
}
foreach (var g in d)
{
gridView2.AddEmptyRow<ArguVo>(row => string.IsNullOrEmpty(row.CodeArgu), new ArguVo
{
CodeArgu = g.Code,
NameArgu = g.Name,
PkOrg = AppContext.Session.OrganizationId,
PkArgu = g.PkParamtemp,
Arguval = g.ValDef,
NoteArgu = g.DescParam
});
}
this.gridControl1.DataSource = null;
this.WList = null;
return;
先根據列表對應的實體類,建立list集合,
用foreach遍歷新增,將集合裏全部的值,所有新增,根據集合裏的值,對應賦值給,兩一個列表的相應名稱。
this.gridControl1.DataSource = null;將以前的列表爲空。
完成移動,右側同理
三、刪除、
①將DataSource置爲null,再保存,
②刪除表中的信息,在次查詢,後面會講
③單行刪除,(刪除所選中行)
int rowindex = gridView2.FocusedRowHandle;
gridView2.DeleteRow(rowindex);
歡迎補充
四、修改
List<ArguVo> arguList = gridView1.DataSource as List<ArguVo>;
foreach (var a in arguList)
{
pkPcArgu = a.PkPcargu;
}
if (ServiceFactory.CreateService<IDeptParameterService>().UpdateList(arguList, pkPcArgu).Success)
{
MessageBoxUtils.Hint("保存成功!");
}
首先獲取到當前列表,遍歷當前列表,這裏是等到了列表的主鍵,根據主鍵修改,java代碼,執行修改的sql語句,便可實現。
在這裏要講一下,個人需求,但願會對你們有所幫助,

右側列表不可編輯,點擊修改,參數值和停用可編輯,

選中參數值,查看屬性,optionsColumn裏面,allowedit設置爲true,readonly爲false,這是可編輯狀態
參數說明,參數名稱的屬性,由於這裏是不可編輯的,均設置爲allowedit爲false,readonly爲true,不可編輯
在修改按鈕的點擊事件下,
setBtnStatus(true);//設置按鈕狀態
並寫setBtnStatus方法,方法以下將boolean值以參數的形式傳遞進去,
/// <summary>
/// 設置按鈕狀態
/// </summary>
/// <param name="editable"></param>
public void setBtnStatus(bool editable)
{
gridView1.OptionsBehavior.Editable = editable;
}
便可,不要忘了將其餘按鈕下的狀態,置位false;可根據實際狀況改變。
至此,增刪改查已完成。往後或有補充。
在講一下樹狀圖,treeList
在vs中的工具欄裏,有treelist工具,經過查詢的方法,將列表的數據,以樹的形式展示出來。也頁面設計器中,右鍵選中

run Designer

fieldName爲你想展現出來的實體類中的哪個字段,這裏是由於要展現兩個字段,在實體類中加
public string TreeText
{
get
{
return "[" + CodeDept + "]" + NameDept;
}
}
CodeDept 和 NameDept;爲你實體類中自己的值。
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------2017年12月28日
按回車頁面上輸入框跳轉問題

若是這些輸入框,在一個panel下,是能夠自動正常跳轉的,會按照順序跳轉,可是若是想讓他按回車以後跳轉到某一個輸入框的話,
在設計器頁面下,點擊視圖,裏面有一個tab建順序按鈕

點擊以後,

上圖藍色的爲編號,x.y.z。
xy同樣,主要看z
鼠標點擊一次藍色的小方塊,藍色的小方塊爲初始時,在此點擊+1,
比方說A輸入框的藍色的小方塊爲7.1.15,想讓他跳轉到右側的B輸入框,則把右側的輸入框設置爲7.1.16便可,其餘的同理,系統會自動按照順序一步一步跳轉過去。
記得修改完以後,要將tab鍵順序關掉,能夠按esc,保存,生成便可。
並將enterMoveNextControl屬性設置爲true(通常默認爲true)

順帶說一下,keyPress事件點擊

private void cboPosision_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
meComment.Focus();
}
}
雙擊keypress事件,cboPosision_KeyPress獲取該事件的輸入框,
meComment.Focus();獲取焦點的輸入框,
meComment爲輸入框的名字。
----------------------------------------------------------------------------------------------------------------------------------------------------------------------2017年12月28日
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------2018年01月06日
C#裏按鈕的快捷鍵
先說一下思路吧,有兩種思路,一種是註冊事件,另外一種是在屬性裏添加,
此次先講一下在屬性裏添加的方式,後續會整理註冊事件的方式
首先,先寫一個公共類,
類名HotKeysManager(可隨我的喜愛)
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Windows.Forms;
6
7 using Zebone.His.Controls;
8
9 namespace Zebone.His.Utils
10 {
11 /// <summary>
12 /// 快捷鍵管理
13 /// </summary>
14 public class HotkeysManager
15 {
16 private List<HotkeysInfo> hotkeysItems = new List<HotkeysInfo>();
17
18 /// <summary>
19 /// 清空全部的快捷鍵
20 /// </summary>
21 public void Clear()
22 {
23 hotkeysItems.Clear();
24 }
25
26 /// <summary>
27 /// 觸發指定快捷鍵對應的操做,指定的快捷鍵有對應操做時,返回true,不然返回false
28 /// </summary>
29 /// <param name="keys">快捷鍵鍵位</param>
30 /// <returns></returns>
31 public bool Handle(Keys keys, ref bool suppressKeyPress)
32 {
33 foreach (var item in hotkeysItems)
34 {
35 if (item.Keys == keys)
36 {
37 item.Action();
38 suppressKeyPress = item.SuppressKeyPress;
39
40 return true;
41 }
42 }
43
44 return false;
45 }
46
47 /// <summary>
48 /// 註冊快捷鍵
49 /// </summary>
50 /// <param name="hotkeysRegistration">註冊快捷鍵接口</param>
51 /// <returns></returns>
52 public HotkeysManager Register(IHotkeysRegistration hotkeysRegistration)
53 {
54 if (hotkeysRegistration != null) hotkeysRegistration.RegisterHotkeys(this);
55
56 return this;
57 }
58
59 /// <summary>
60 /// 註冊快捷鍵
61 /// </summary>
62 /// <param name="keys">快捷鍵鍵位</param>
63 /// <param name="description">快捷鍵說明</param>
64 /// <param name="action">快捷鍵對應操做</param>
65 public HotkeysManager Register(Keys keys, string description, Action action)
66 {
67 return Register(keys, description, false, action);
68 }
69
70 /// <summary>
71 /// 註冊快捷鍵
72 /// </summary>
73 /// <param name="keys">快捷鍵鍵位</param>
74 /// <param name="description">快捷鍵說明</param>
75 /// <param name="suppressKeyPress">執行快捷鍵後,是否取消按鍵輸入</param>
76 /// <param name="action">快捷鍵對應操做</param>
77 public HotkeysManager Register(Keys keys, string description, bool suppressKeyPress, Action action)
78 {
79 hotkeysItems.Insert(0, new HotkeysInfo()
80 {
81 Keys = keys,
82 Action = action,
83 SuppressKeyPress = suppressKeyPress,
84 Description = description
85 });
86
87 return this;
88 }
89
90 /// <summary>
91 /// 獲取當前已經註冊的快捷鍵數量
92 /// </summary>
93 internal int Count
94 {
95 get { return hotkeysItems.Count; }
96 }
97
98 /// <summary>
99 /// 從控件中發現並註冊快捷鍵
100 /// </summary>
101 /// <param name="control"></param>
102 /// <returns></returns>
103 internal HotkeysManager Register(Control control)
104 {
105 //查找並註冊按鈕快捷鍵
106 var button = control as IButtonControl;
107 var supportHotkeys = control as ISupportHotkeys;
108 if (button != null && supportHotkeys != null && supportHotkeys.Hotkeys != Keys.None)
109 {
110 Register(supportHotkeys.Hotkeys, control.Text, button.PerformClick);
111 }
112
113 if (control is IHotkeysRegistration)
114 {
115 Register(control as IHotkeysRegistration);
116 }
117
118 //從子控件中查找
119 foreach (var c in control.Controls)
120 {
121 if (c is Control) Register(c as Control);
122 }
123
124 return this;
125 }
126
127 private class HotkeysInfo
128 {
129 public Keys Keys { get; set; }
130
131 public Action Action { get; set; }
132
133 public bool SuppressKeyPress { get; set; }
134
135 public string Description { get; set; }
136 }
137 }
138 }

代碼裏有相應註釋,後期會在次詳細處理
在寫一個公共的類,引用這個類的方法,我這裏寫的是BaseForm(名稱可隨我的喜愛)
using Zebone.His.Utils;
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (!this.DesignMode)
{
var suppressKeyPress = false;
if (hotkeysManager.Handle(e.KeyData, ref suppressKeyPress))
{
e.SuppressKeyPress = suppressKeyPress;
}
}
}
上面要用using的方式引用以前的utils
而後在你要添加的頁面引用baseForm
public partial class InternEmpEdit : BaseForm
而後點擊toolbar按鈕----屬性 (這裏以F2爲例)

以下圖


保存生成,便可。
本期先整理到這,若有疑問,盡情留言!
您的質疑,使咱們共同進步。
---------------------------------------------------------------------------------------------------------------------------------------------------------------------2018年01月06日
---------------------------------------------------------------------------------------------------------------------------------------------------------------------2018年01月08日
C#上傳圖片

圖片上傳,首先在頁面上定義一個PictureEdit的控件,

再加一個buttencontrol

再添加一個工具控件,OpenFileDialog(在工具箱中能夠找到,名字自擬,這裏氣的名字爲ofdPhoto)
接下來開始寫代碼,
①首先要建立對應的實體類,數據庫裏存儲的類型爲image
對應的實體類的類型爲
/// <summary>
/// 獲取或設置照片
/// </summary>
public byte[] Photo { get; set; }
②要適應格式,
private const string FILEFILTER = "圖片|*.jpg;*.png;*.gif;*.jpeg;*.bmp|全部文件|*.*"; 如圖

③賦值
public void SetControlsValue(InternEmpVo setValue )
{
isBinding = true;
dcEmpIntern.SetValue(setValue);
this.pePhoto.EditValue = setValue.Photo;
isBinding = false;
}
④上傳按鈕的點擊事件
private void buttonControl1_Click(object sender, EventArgs e)
{
ofdPhoto.ShowDialog();
}
⑤第五步,最關鍵的一步
private void ofdPhoto_FileOk(object sender, CancelEventArgs e)
{
using (FileStream fs = new FileStream(ofdPhoto.FileName, FileMode.Open))
{
long size = fs.Length;
setInternEmp.Photo = new byte[size];
//將文件讀到byte數組中
fs.Read(setInternEmp.Photo, 0, setInternEmp.Photo.Length);
fs.Close();
this.pePhoto.EditValue = setInternEmp.Photo;
}
}
pePhoto爲pictureEdit的名稱
上傳成功,保存進數據庫便可。
---------------------------------------------------------------------------------------------------------------------------------------------------------------------2018年01月08日
---------------------------------------------------------------------------------------------------------------------------------------------------------------------2018年01月11日
String.Format("{0:N1}", a) 保留小數點後一位
String.Format("{0:N2}", a) 保留小數點後兩位
String.Format("{0:N3}", a) 保留小數點後三位
C#保留小數位N位四捨五入
- double s=0.55555;
- result=s.ToString("#0.00");
C#保留小數位N位四捨五入
- double dbdata = 0.55555;
- string str1 = dbdata.ToString("f2");
---------------------------------------------------------------------------------------------------------------------------------------------------------------------2018年01月11日
----------------------------------------------------------------------------------------------------------------------------------------------------------------------2018年04月20日
C# 怎麼判斷輸入的字符串是整數(輸入的不必定是字符串類型,能夠是object類型)
/// <summary>
/// 判斷是不是整數 是則返回true; 不是則返回false
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static bool isInIsInteger(string s)
{
long num;
bool falg = long.TryParse(s, out num); //此處爲核心代碼
if (falg == true)
{
return true;
}
else
{
return false;
}
}
我這裏傳的參數是string 類型,固然也能夠傳一個obj類型 不過在 long.TryParse(s, out num); 中的s能夠加上.toString();
在須要的方法中調用便可
(附贈按鈕的點擊方法)
private void btnSaveSupply_Click_1(object sender, EventArgs e)
{
if (!dcSupply.Validate()) return;
dcSupply.GetValue(editParam.supply);
editParam.itemList = gvItem.DataSource as List<SupplyItem>;
for (int i = 0; i < editParam.itemList.Count; i++)
{
if (editParam.itemList[i].Quan <= 0 || editParam.itemList[i].Quan == null )
{
MessageBoxUtils.Hint("數量爲必填項,且必須大於0的整數!",HintMessageBoxIcon.Error);
return;
}
string aa = editParam.itemList[i].Quan.ToString();
var bb = isInIsInteger(aa);
if (bb == false)
{
MessageBoxUtils.Hint("數量必須是整數!", HintMessageBoxIcon.Error);
return;
}
}
var result = ServiceFactory.CreateService<IOrderUsageService>().SaveSupply(editParam);
if (result.Success)
{
MessageBoxUtils.Hint("保存成功!");
LoadTree();
SetButtonStatus(false);
}
}
紅色字體爲調用該方法處
----------------------------------------------------------------------------------------------------------------------------------------------------------2018-06-05
C#列表中判斷輸入的值是否爲數字
兩種思路。
可是首先要寫一個方法,方法分爲兩種:
1
1 /// <summary>
2 /// 判斷輸入是否數字
3 /// </summary>
4 /// <param name="num">要判斷的字符串</param>
5 /// <returns></returns>
6 public static bool VldInt(string num)
7 {
8 #region
9 int ResultNum;
10 return int.TryParse(num, out ResultNum);
11 #endregion
}
2
1 /// <summary>
2 /// 判斷輸入的是否爲數字
3 /// </summary>
4 /// <param name="num"></param>
5 /// <returns></returns>
6 public static bool isNum(string num)
7 {
8 #region
9 try
10 {
11 Convert.ToInt32(num);
12 return true;
13 }
14 catch
15 {
16 return false;
17 }
18 #endregion
19 }
一,能夠在保存時作判斷,
點擊按鈕,獲取須要的全部列表,循環遍歷調用方法,若是不符合條件,跳出循環,不細講了
2、EditValueChanging事件,實時判斷,

1 private void riteQuan_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
2 {
3 var row = gvApply.GetFocusedRow() as BdOrdSetAndDtVO;
4 var aa = isNum(e.NewValue.ToString());
5 if (!aa) e.NewValue = "1";
6 if (e.NewValue.ToString() != "" || e.NewValue != null)
7 {
8 row.Quan = Convert.ToDecimal(e.NewValue);
9 }
10 if (row != null && row.Quan != 0)
11 {
12 var i = e.NewValue;
13 row.Amount = row.PriceCg * row.Quan;
14 }
15 else
16 gvApply.DeleteRow(gvApply.FocusedRowHandle);
17 gvApply.RefreshData();
18 }
19 #endregion
其中第四行調用該方法,第5行判斷,若是不知足條件,賦默認值爲1
-------------------------------------------------------------------------------------------------------------------------------------------------------2018-06-05