Question

I have a datatable like this

    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);
     }

It updates nicely when I click on button12, but I wanted to make it better, and tried to make it auto-update, so I moved her into

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

That way the table would be acessible form1-wide, and maybe, just maybe, it would update on every action.. it didnt turn out ok, because when I change the variables readG.Text, the table, since it took the value on the early program, didnt update the values.

I tought about creating a button that would update the table, and would repeat the Rows and Columns "Add", but is there a simpler and time effective way to do it? like, "on every action, reload values"?

One added question, if I create other form, where it shows the current datatable, how can I make the table I just created acessible there, keeping the table in the form 1?

(I understand nothing about get, set, so if anyone can explain those "commands" in a simple way I'll be very thankfull)

Was it helpful?

Solution

If I understand your question correctly this could be one way to achieve what you need:

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);
        }
     }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top