1. DataGridView和ContextMenuStrip的綁定是發生在DataGridView的CellMouseClick事件,在事件中指定右鍵菜單彈出;
2. DataGridView的列名稱(columnName)的指定是(name)屬性,如果沒有手工指定,那么將會默認為DataPRopertyName + 列類型(比如idTextBoxColumn),所以需要手工指定一下:
或者是在load方法中同步一下:
private void frmTaskList_Load(object sender, EventArgs e){ … foreach (DataGridViewColumn item in this.dgTaskList.Columns) { item.Name = item.DataPropertyName; } …}
3. 父類事件是不能直接讓子類調用(但是可以通過實例化之后的子類來為父類的事件賦值),于是需要讓父類通過方法封裝一下于是有了:
public class BaseEditForm : Form{ …
public event RefreshList RefreshParent; public void OnRefreshParent() { if (this.RefreshParent != null) { this.RefreshParent(); }
}
…}public partial class frmEditProject : BaseEditForm{ …private void btnOK_Click(object sender, EventArgs e) { …base.OnRefreshParent(); this.Close(); } …}
4. Combox的綁定,如果綁定的是List對象,需要先制定displayMember和ValueMember,然后再指定DataSource,否則將會報錯:Cannot bind to the new display member.
this.cbProj.DisplayMember = "ProjectName";this.cbProj.ValueMember = "Id";this.cbProj.DataSource = ProjectService.GetProjects();this.cbProj.SelectedValue = task.ProjId;另外,如果是要根據“value”選中下拉框某項,只需要設置selectedValue即可。
5. 循環刪除
方式一:將刪除內容放置到一個新的刪除列表中,然后遍歷刪除列表中的項,從真正的列表中刪除該項(避免直接遍歷真正list刪除導致缺位情況);
方式二:倒敘刪除
6. LinqToSql的Join
切記,linqtosql的的Join,左側一定是關聯表,右側才是本次Join的表,如下:
var query = from ws in context.workflowsequeuece join w in context.workflow on ws.WFID equals w.Id join t in context.task on ws.TaskId equals t.Id join e in context.employee on ws.EmpID equals e.Id where ws.TaskId == m.Id select …
6.Linq返回實體類
public static List<BizEmployee> GetMemebersByProjId(int pProjId){ Model1Container container = DbUtil.GetDbContainer(); var query = from t in container.project_employee where t.ProjId == pProjId select new BizEmployee(t.EmpId.Value, t.EmpName); return query.ToList<BizEmployee>();}
返回異常:Only parameterless constructors and initializers are supported in LINQ to Entities.
修改為:
public static List<BizEmployee> GetMemebersByProjId(int pProjId){ Model1Container container = DbUtil.GetDbContainer(); var query = from t in container.project_employee where t.ProjId == pProjId select new BizEmployee { Id = t.EmpId.Value, EmpName = t.EmpName }; return query.ToList<BizEmployee>();}問題解決。
7. DataGridView性能情況
后來發現是因為DataSouce(List<T>)中綁定了一個Workflow對象,去掉這個對象(不賦值)即可輕松綁定。
8. Settings
考慮如何實現Winform的“記住用戶名密碼”,后來發現使用Settings可以實現:
直接在Settings.setting中添加內容:
添加了Settings內容后,直接就會在Settings.Default中體現出來;
賦值方式(注意最后要Save):
Settings.Default.Mail = mail;Settings.Default.PassWord = pwd;Settings.Default.Save();
取值方式:
this.txtPwd.Text = Settings.Default.Password;this.txtUserName.Text = Settings.Default.Mail;
評價:簡單易行,比之向xml以及ini文件寫還要去打開文件、關閉文件,要方便得多。
9. MDI父窗體的關閉
如果直接關閉MDI父窗體,將會導致一個問題,主線程沒有關閉,這個時候需要:
private void frmParent_FormClosing(object sender, FormClosingEventArgs e){ application.Exit();}
10.Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.
碰到了兩次這種情況:
1.調用共通方法,傳入的entity所定義的類型錯了SetEntityType()發放有誤;
2.是因為表結構主鍵沒有設置自增長,Id也沒有賦值導致。
1. 如果為DataGridview指定了ContextMenuStrip屬性,那么右鍵的話,DataGridView將不會再走自定義的CellMouseClick事件方法體,而是走內置的展示ContextStript事件;所以如果需要在右鍵事件中做額外處理(比如獲取當前指定的行),那么就不要為DataGridView指定ContextMenuStrip屬性了。
2. DataGridView的PropertyName和(Design)Name默認是不一致的,而且通過列名索引使用的是(Design)Name,所以需要對其進行手工設置,或者存取的時候按照(Design)Name來進行獲取。
1. Requested value 'Geometry' was not found
幾乎一個晚上都在調整這個問題,后來發現是因為Updater工程,被我重定向到了UI/bin/Debug之后,產生的副產物,后來從定向回到了自己的bin目錄下,問題完美解決。
Unable to convert MySQL date/time value to System.DateTime
后來發現是因為Mysql的datetime字段值為0-00000-0,對于空值的日期處理有問題,后來填充上了真實日期后完美解決此問題;
新聞熱點
疑難解答