开发者

C# need to add data to a field when a record is touched

开发者 https://www.devze.com 2023-02-14 02:53 出处:网络
I\'ve built a form that has a listbox on the left side, and a bunch of text fields on the right for each field in the data source.

I've built a form that has a listbox on the left side, and a bunch of text fields on the right for each field in the data source.

When the user clicks on an entry on the left it moves to that record so they can modify the fields on the right. All works fine up to this point.

The problem is that each user has a unique ID number and once they start typing in the record fields I want to populate a hidden field with their ID number so when they save, I know who touched that record.

I cannot find a suitable event to handle this method. I am not using a datagrid, I simply dragged members from the data sources window onto the form accordingly. CurrentC开发者_开发技巧hanged and CurrentItemChanged fire off when switching between items on the listbox so these don't reflect the behavior I need.

Any thoughts?


I feel this is a somewhat ugly answer, but it gets the job done without major any major effort. Once everything has been validated and the binding source EndEdit function has been called, you may check the DataSet for changed rows.

Before_Update handles this logic and looks at each row if changes have been made, making the necessary modifications to columns before calling UpdateAll.

Checking for Changed Rows - http://msdn.microsoft.com/en-US/library/czb9z269%28v=VS.80%29.aspx

    // Save event
    private void clientBindingNavigatorSaveItem_Click(object sender, EventArgs e)
    {
        this.Validate();
        this.clientBindingSource.EndEdit();

        Before_Update();

        this.tableAdapterManager.UpdateAll(this.dP_TestDataSet);
    }

    // Place logic in here to modify records if they are changed
    private void Before_Update()
    {
        if (dP_TestDataSet.HasChanges())
        {
            for (int tRow = 0; tRow < dP_TestDataSet.Tables["Client"].Rows.Count; tRow++)
            {
                // Modification Logic
                if (dP_TestDataSet.Tables["Client"].Rows[tRow].RowState == DataRowState.Modified)
                {
                    dP_TestDataSet.Tables["Client"].Rows[tRow]["userID"] = Program.SYSNG.UserID;
                }

                // Addition Logic
                if (dP_TestDataSet.Tables["Client"].Rows[tRow].RowState == DataRowState.Added)
                {
                    // Addition Logic
                    // ...
                }
                // Other RowStates such as Deleted, Detatched or Unchanged work here too
            }
        }
    }

This successfully solves my problem though I am hoping someone comes along to trump this answer. I feel that I am doing extra work that the framework has hidden somewhere under the sheets on me and hope someone can provide me insight to this functionality.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号