【问题标题】:Keep datatable updated on every action "live-mode"/auto-update在每个动作“实时模式”/自动更新时保持数据表更新
【发布时间】:2014-04-20 16:44:58
【问题描述】:

我有一个这样的数据表

    private void button12_Click(object sender, EventArgs e)
    {   DataTable tableEC0 = new DataTable();
        tableEC0.Columns.Add("Nome", typeof(string));
        tableEC0.Columns.Add("valor", typeof(string));

        tableEC0.Rows.Add("G", readG.Text);
        tableEC0.Rows.Add("Q", readQ.Text);
        tableEC0.Rows.Add("W", readW.Text);
        tableEC0.Rows.Add("Tipodeverificacao", tipodeverificacao);
     }

当我点击 button12 时它会很好地更新,但我想让它变得更好,并尝试让它自动更新,所以我把她移到了

public partial class Form1 : Form
{ // same exact table
}

这样表格就可以在 form1 范围内访问,而且也许,只是也许,它会在每个动作上更新.. 结果不好,因为当我更改变量 readG.Text 时,表格,因为它花了早期程序的值,没有更新值。

我很想创建一个按钮来更新表格,并重复行和列“添加”,但是有没有更简单和时间有效的方法呢?比如,“在每一个动作上,重新加载值”?

还有一个问题,如果我创建其他表单,它会在其中显示当前数据表,我怎样才能使我刚刚创建的表在那里可访问,并将表保持在表单 1 中?

(我对get、set一无所知,所以如果有人能用简单的方式解释这些“命令”,我将非常感激)

【问题讨论】:

  • 我看过那些帖子,你对标签的看法是对的,我不知道,但关于礼仪,它所说的只是帖子本身,而不是 cmets.. 所以谢谢你的信息:)我会缓和我的行为:)

标签: c# datatable


【解决方案1】:

如果我正确理解您的问题,这可能是实现您需要的一种方法:

public partial class Form1 : Form
{ 
    DataTable tableEC0 = new DataTable(); // central table 

    public Form1()
    {
        InitializeComponent();

        // initialize your table with the columns needed
        tableEC0.Columns.Add("Nome", typeof(string));
        tableEC0.Columns.Add("valor", typeof(string));

        // hookup textbox
        readG.TextChanged += readG_TextChanged;
        readQ.TextChanged += readQ_TextChanged;
        readW.TextChanged += readW_TextChanged;
    } 

    // refactored to one call
    private void button12_Click(object sender, EventArgs e)
    {      
        UpdateAll();
    }

    // hookup this method to the TextBox_Changed event 
    private void readG_TextChanged(object sender, EventArgs e)
    {
        Update("G", (TextBox) sender);
    }

    private void readQ_TextChanged(object sender, EventArgs e)
    {
        Update("Q", (TextBox) sender);
    }

    private void readW_TextChanged(object sender, EventArgs e)
    {
        Update("W", (TextBox) sender);
    }

    // update all values
    private void UpdateAll()
    {
        Update("G", readG.Text);
        Update("Q", readQ.Text);
        Update("W", readW.Text);
        Update("Tipodeverificacao", tipodeverificacao);
    }

    // update from a textbox event
    private void Update(string key, TextBox txtBox)
    {
         Update(key, txtBox.Text);
    }

     // update (or insert in new) a row in your table
     private void Update(string key, string value)
     {
        // find a row
        var rows = tableEC0.Select(
                   String.Format("'Nome'='{0}'", key));
        if (rows.Length==1)  
        {
             // found, update
             rows[0]["valor"]= value;
        } 
        else if(rows.Length > 1)
        {
             throw new Exception("huh? too many rows found");
        }
        else
        {
             // not found, add
              tableEC0.Rows.Add(key, value);
        }
     }
}

【讨论】:

  • 你创建了一个方法,用于更新数据,而不是重复插入表格,对吧?
  • 这不是我的意思,我希望它在不使用按钮的情况下更新,例如,使用循环,每当 readG.Text 改变它的值时,表格就会更新它。但是它很远我现在做这样的事情太复杂了,我创建了一个按钮,它基本上重复了值的插入,我相信现在就可以了。还是谢谢你:)
  • 如果您更改任何文本框值(当控件失去焦点时),更改的事件将触发并更新表格。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-21
  • 2021-08-27
  • 2019-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-21
相关资源
最近更新 更多