如何访问数据源字段 [英] how to access datasource fields

查看:63
本文介绍了如何访问数据源字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

使用TableAdapter和类型化的数据集,如果您有带有user_ID字段的事务表,那么如果我想用输入的数据保存当前用户,则user_ID是MDIForm中的静态变量.确定user_ID 在数据源中进行访问以隐式更新用户字段

Using TableAdapter and typed dataset , if you have transaction table with field for user_ID , the user_ID is static variable in MDIForm if i want to save current user with entered data how to do that , i want to do that behindly so how to determine user_ID in Datasource to access it to update user field invisibly


谢谢

推荐答案

返回行然后更新字段值的逻辑是

The logic for returning to a row and then updating a field value would be to

  1. 在变量中记住当前行的主键,例如您的静态字段.
  2. 做一些移动到该行之外的工作.
  3. 在第一步中使用该字段返回到
  4. 设置该值(如果您要这样做的话).

在此代码示例中,我在DataSet/TableAdapter/BindingSource下有一个人员表.

In this code sample I have a person table under the DataSet/TableAdapter/BindingSource.

为项目(.xsd)文件生成

Generated for the project (.xsd) file

在下面的代码(它是SDI,但对于MDI或与其他类一起使用,其逻辑保持不变)中,Button1在项目符号列表中演示了以上内容.本地var currentId将是您的静态变量,我将它们放在一个地方以便清楚地看到事物.

In the code below (which is SDI but the logic stays the same for MDI or working with another class) Button1 demonstrates the above in the bullet list. The local var currentId would be your static variable, I put all in one place to see things clearly.

using System;
using System.Data;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void persons1BindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.persons1BindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.forumExamplesDataSet);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.persons1TableAdapter.Fill(this.forumExamplesDataSet.Persons1);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var currentId = ((DataRowView)persons1BindingSource.Current)
                .Row.Field<int>("id");

            // mimic moving off the record
            persons1BindingSource.MoveNext();
            
            // move back to the record
            persons1BindingSource.Position = persons1BindingSource.Find("id", currentId);

            // change first name field in the first record we were on
            ((DataRowView)persons1BindingSource.Current)
                .Row.SetField<string>("FirstName", "Karen");
        }
    }
}

 


这篇关于如何访问数据源字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆