goodmorningsky Posted April 27, 2004 Posted April 27, 2004 Hi, I created a DataGrid and textboxes and other controls. Those controls are bound to a DataTable like following private void frmDepartment_Load(object sender, System.EventArgs e) { tblDepartmentDB tbl = new tblDepartmentDB(); dtDept = tbl.GetVwDepartment(); dgDeptList.DataSource = dtDept; this.sCboComp.DataBindings.Add( new Binding("Text", dtDept, "CompanyCode")); this.txtCompName.DataBindings.Add(new Binding("Text", dtDept, "CompanyName")); this.txtDeptCode.DataBindings.Add(new Binding("Text", dtDept, "DepartmentCode")); .... and scroll through DataGrid and Edit value from textboxes and other bound controls, or Insert/delete a new row. So far is fine. But, when the form is closing, I want to get modified rows and inserted rows and deleted rows. So I did following way.. But, row.RowState returns true even though I didn't change any value but just scroll. If I select the row through the datagrid, the row's RowState is chagned to Modified. What's wrong with this? How can I get the only rows that are actually modified? private void frmDepartment_Closing(object sender, System.ComponentModel.CancelEventArgs e) { DataRow row; int mod=0; for(int i=0; i< dtDept.Rows.Count; i++) { row = dtDept.Rows[i]; if(row.RowState == DataRowState.Modified) { Debug.WriteLine(row["DepartmentName"].ToString()); mod++; } else if(row.RowState == DataRowState.Added) { Debug.WriteLine(row["DepartmentName"].ToString()); } else if(row.RowState == DataRowState.Deleted) { Debug.WriteLine(row["DepartmentName"].ToString()); } } Debug.WriteLine("moded: " + mod.ToString()); } Quote Sun Certified Web component Developer, Microsoft Certified Solution Developer .NET, Software Engineer
*Experts* Nerseus Posted April 27, 2004 *Experts* Posted April 27, 2004 You can use the GetChanges() method to return a new dataset of changed rows (inserted/updated/deleted). I didn't take a close look at your bindings, but the GetChanges should get you what you want. If you're finding too many rows being modified when they shouldn't maybe upload a small sample project. -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
goodmorningsky Posted April 27, 2004 Author Posted April 27, 2004 Thank you for your reply. it's right to use GetChange method. GetChange method return rows by checking RowState flag on each DataRow object. But, My problem is that the RowState of each row is set to Modified by just scrolling ( which means by just changing of this.BindingContext[this.dtDept].Position without modifing..) I think I miss something.. I think whenever I change Position it automatically update the row. I don't know why... Quote Sun Certified Web component Developer, Microsoft Certified Solution Developer .NET, Software Engineer
*Experts* Nerseus Posted April 27, 2004 *Experts* Posted April 27, 2004 The standard behavior when moving a position is NOT to change the status of the row. My guess is that you have some code that's updating the row when the position changes. It might be something as simple as format/parse event on a textbox (to format a value from 4.56 to $4.56 and back). You can add a ColumnChanged event (or RowChanged event) to a DataTable. In the event you can set a breakpoint and use the CallStack window to see what code, if any, is causing the update. -nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
goodmorningsky Posted April 27, 2004 Author Posted April 27, 2004 Thank you for reply. You are right. There was event fired as row changes. But, I don't know which code chagne it. I think there is problem with data binding. otherwise there is no place to cause... Some column value doesn't fire column change event while some bound values fires. My guess is there is something wrong on data types between table type and bound control's data type Please take a look on my code Thank you for considering.. From loading private void frmDepartment_Load(object sender, System.EventArgs e) { tblDepartmentDB tbl = new tblDepartmentDB(); dtDept = tbl.GetVwDepartment(); DataColumn col=new DataColumn(colRowState, typeof(int)); Debug.WriteLine(DataRowState.Modified.ToString()); Debug.WriteLine(Convert.ToInt32(DataRowState.Modified)); dtDept.Columns.Add(col); AddCustomDataTableStyle(); dgDeptList.DataSource = dtDept; //this.sprDeptList.DataSource =dtDept; this.lbTest.DataBindings.Add( new Binding("Text", dtDept, "RowState")); this.sCboComp.DataBindings.Add( new Binding("Text", dtDept, "CompanyCode")); this.txtCompName.DataBindings.Add(new Binding("Text", dtDept, "CompanyName")); this.txtDeptCode.DataBindings.Add(new Binding("Text", dtDept, "DepartmentCode")); this.txtDeptName.DataBindings.Add(new Binding("Text", dtDept, "DepartmentName")); this.nudPaySpan.DataBindings.Add(new Binding("Value", dtDept, "PayrollSpan")); this.nudFPrint.DataBindings.Add(new Binding("Value", dtDept, "FingerprintLevel")); this.sCboState.DataBindings.Add(new Binding("Text", dtDept, "StateName")); this.sCboState.DataBindings.Add(new Binding("Tag", dtDept, "StateCode")); this.sCboOverTime.DataBindings.Add(new Binding("Tag", dtDept, "OverTimeRule")); this.sCboOverTime.DataBindings.Add(new Binding("Text", dtDept, "OverTimeRuleName")); this.sCboDBIpAddr.DataBindings.Add(new Binding("Text", dtDept, "DBIpAddr")); this.sCboMgrLogin.DataBindings.Add(new Binding("Text", dtDept, "ManagerLogableLevelName")); this.sCboMgrLogin.DataBindings.Add(new Binding("Tag", dtDept, "MinLogableLevel")); this.sCboIncomEmp.DataBindings.Add(new Binding("Text", dtDept, "ShowCheckErrMinEmpLevelName")); this.sCboIncomEmp.DataBindings.Add(new Binding("Tag", dtDept, "ShowCheckErrMinEmpLevel")); this.txtInComWhen.DataBindings.Add(new Binding("Text", dtDept, "ShowCheckErrPunchType")); this.nudMinBreak.DataBindings.Add(new Binding("Value", dtDept, "MinBreakRestrict")); this.nudMaxBreak.DataBindings.Add(new Binding("Value", dtDept, "MaxBreakRestrict")); this.txtEmpID.DataBindings.Add(new Binding("Text", dtDept, "EmployeeIDCode")); this.txtLastTranTime.DataBindings.Add(new Binding("Text", dtDept, "LastTransferedTime")); this.txtUpTime.DataBindings.Add(new Binding("Text", dtDept, "UpdateTime")); this.txtLastClosedDate.DataBindings.Add(new Binding("Text", dtDept, "LastClosedDate")); this.hdContiPunch.DataBindings.Add(new Binding("Text", dtDept, "ContinuancePunch")); this.hdDMUserID.DataBindings.Add(new Binding("Text", dtDept, "DMUserID")); this.hdEditHour.DataBindings.Add(new Binding("Text", dtDept, "EditHour")); this.hdRegIn.DataBindings.Add(new Binding("Text", dtDept, "RegularInTime")); this.hdRegOut.DataBindings.Add(new Binding("Text", dtDept, "RegularOutTime")); this.sCboRemoteLogin.DataBindings.Add(new Binding("Text", dtDept, "MinRemoteLogableLevelName")); this.sCboRemoteLogin.DataBindings.Add(new Binding("Tag", dtDept, "MinRemoteLogableLevel")); this.nudClockInRound.DataBindings.Add(new Binding("Value", dtDept, "ClockInRounding")); this.hdOthers.DataBindings.Add(new Binding("Text", dtDept, "Others")); this.scboMinAddEmpLevel.DataBindings.Add(new Binding("Text", dtDept, "MinAddEmpLevelName")); this.scboMinAddEmpLevel.DataBindings.Add(new Binding("Tag", dtDept, "MinAddEmpLevel")); this.ChangeMode(FormMode.View); this.BindingContext[this.dtDept].Position=0; //-- populate option controls DataTable dtEmpList; DataTable dtComps; DataTable dtState; DataTable dtOvRule; tblEmployeeLevelDB tbl2 = new tblEmployeeLevelDB(); dtEmpList = tbl2.GetEmployeeLevelAsTable(); //employee level combo boxes foreach(DataRow aRow in dtEmpList.Rows) { this.sCboMgrLogin.Items.Add(new SkyComponents.ComboBoxItem((string)aRow["LevelName"], aRow["ID"].ToString())); this.sCboIncomEmp.Items.Add(new SkyComponents.ComboBoxItem((string)aRow["LevelName"], aRow["ID"].ToString())); this.sCboRemoteLogin.Items.Add(new SkyComponents.ComboBoxItem((string)aRow["LevelName"], aRow["ID"].ToString())); this.scboMinAddEmpLevel.Items.Add(new SkyComponents.ComboBoxItem((string)aRow["LevelName"], aRow["ID"].ToString())); } tblCompanyDB tbl3 = new tblCompanyDB(); dtComps = tbl3.GetCompaniesSummary(); foreach(DataRow aRow in dtComps.Rows) { this.sCboComp.Items.Add(new SkyComponents.ComboBoxItem((string)aRow["CompanyName"], (string)aRow["CompanyCode"])); } tblStateDB tbl4 = new tblStateDB(); dtState = tbl4.GetState(); foreach(DataRow aRow in dtState.Rows) { this.sCboState.Items.Add(new SkyComponents.ComboBoxItem((string)aRow["StateName"], aRow["StateCode"].ToString())); } tblOvertimeRuleDB tbl5 = new tblOvertimeRuleDB(); dtOvRule = tbl5.GetOvertimeRuleSummary(); foreach(DataRow aRow in dtOvRule.Rows) { this.sCboOverTime.Items.Add(new SkyComponents.ComboBoxItem((string)aRow["OvertimeRuleName"], aRow["ID"].ToString())); } DataTable tbl6 = new DBHandler().ExecuteDataTableOnHQ("SELECT Distinct DBIpAddr FROM tblDepartment"); foreach(DataRow aRow in tbl6.Rows) { this.sCboDBIpAddr.Items.Add(new SkyComponents.ComboBoxItem((string)aRow["DBIpAddr"], null)); } this.txtLastTranTime.ReadOnly = true; this.txtUpTime.ReadOnly=true; this.dtDept.RowChanged += new DataRowChangeEventHandler(this.dtDept_RowChanged); this.dtDept.ColumnChanging += new DataColumnChangeEventHandler(this.dtDept_ColChanging); } private void dtDept_ColChanging(object sender, DataColumnChangeEventArgs e ) { //Debug.WriteLine("positionsky: " + this.BindingContext[this.dtDept].Position.ToString()); Debug.WriteLine(e.Row.RowState.ToString()); Debug.WriteLine(e.Column.ColumnName); } private void dtDept_RowChanged(object sender, DataRowChangeEventArgs e) { Debug.WriteLine("positionsky: " + this.BindingContext[this.dtDept].Position.ToString()); } dtDept_ColChanging prints following message on Output window. Modified StateCode Modified OverTimeRule Modified MinLogableLevel Modified ShowCheckErrMinEmpLevel Modified MinRemoteLogableLevel Modified MinAddEmpLevel positionsky: 4 Modified StateCode Modified OverTimeRule Modified MinLogableLevel Modified ShowCheckErrMinEmpLevel Modified MinRemoteLogableLevel Modified MinAddEmpLevel positionsky: 4 Modified StateCode Modified OverTimeRule Modified MinLogableLevel Modified ShowCheckErrMinEmpLevel Modified MinRemoteLogableLevel Modified MinAddEmpLevel positionsky: 5 actual database table columns and data types of bound table. CompanyCode char DepartmentCode varchar DepartmentName varchar StateCode char EmployeeIDCode varchar DMUserID varchar ContinuancePunch tinyint RegularInTime char RegularOutTime char FingerprintLevel tinyint EditHour bit PayrollSpan smallint MinAddEmpLevel varchar MinLogableLevel char OverTimeRule int UpdateTime datetime DBIpAddr varchar LastClosedDate varchar MinBreakRestrict int MaxBreakRestrict int ShowCheckErrMinEmpLevel varchar ShowCheckErrPunchType varchar MinRemoteLogableLevel varchar ClockInRounding int Others varchar LastTransferedTime datetime CompanyName varchar OverTimeRuleName varchar StateName varchar ShowCheckErrMinEmpLevelName varchar ManagerLogableLevelName varchar MinRemoteLogableLevelName varchar MinAddEmpLevelName varchar Quote Sun Certified Web component Developer, Microsoft Certified Solution Developer .NET, Software Engineer
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.