C#實例應用總結(一)
本人沒有系統學習過,C#,只是在工作中積累了一些,為了方便以后開發中使用,在此做一下總結,如有不對的地方,歡迎各位觀眾多多批評與指正!
您的意見,才能使大家更好的進步!望不吝指教!
概念就不說了,有興趣的話,可以留言,以后再加上!
工具用的是VS2013,后台用的java
一、gridView的增刪改查,
1、查詢
根據sql查詢,
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方法名。
2、新增
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;將之前的列表為空。
完成移動,右側同理
3、刪除、
①將DataSource置為null,再保存,
②刪除表中的信息,在次查詢,后面會講
③單行刪除,(刪除所選中行)
int rowindex = gridView2.FocusedRowHandle;
gridView2.DeleteRow(rowindex);
歡迎補充
4、修改
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日
C#保留小數點后幾位
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");//點后面幾個0就保留幾位
C#保留小數位N位四舍五入
- double dbdata = 0.55555;
- string str1 = dbdata.ToString("f2");//fN 保留N位,四舍五入
---------------------------------------------------------------------------------------------------------------------------------------------------------------------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 }
一,可以在保存時做判斷,
點擊按鈕,獲取需要的所有列表,循環遍歷調用方法,如果不符合條件,跳出循環,不細講了
二、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