DataTable with constructor methods of a class to make registrations

0

I'm trying to make a DataTable that saves data (I'm not using database) referring to the name and email of a client and displays in a DataGridView (display on the grid is not the problem, this I know do) the data that was typed in textbox . I started messing with DataTable yesterday and read that it is better for my application than a List<T> , which I have already used to do addition and deletion in a signup program.

Below I put the code of the Data class and its attributes and methods, but I would like to get help to understand how to get the method of my class (which in this case is the Name that is typed in the text box txt_nome Email that is received in txt_email ) and put it inside DataTable . Below I put the parts of the code that I already have and are relevant:

public partial class Form1 : Form
{

    DataSet ds = new DataSet();
    DataTable dt = new DataTable("Cadastro");

..........

private void bt_salvar_Click_1(object sender, EventArgs e)
{       
DataRow dr = dt.NewRow();
        dr["Nome"] = txt_nome.Text;

        dt.Rows.Add(dr);
        ds.Tables.Add(dt);

        dataGridView1.DataSource = dt;
}

...............

public class Dados
{

    private string _nome;
    private string _email;

    //construtor para iniciar os dados privados - recebe parametros
    public Dados(string nome, string email)
    {
        this._nome = nome;

        this._email = email;
    }


    public string Nome
    {
        get
        {
            return _nome;
        }
        set
        {
            _nome = value;
        }
    }

    public string Email
    {
        get
        {
            return _email;
        }
        set
        {
            _email = value;
        }
    }
}

If I'm talking about some nonsense I'm sorry, I'm a beginner and I'm trying to learn everything very well (I'm looking for several points of view on each subject of C #).

    
asked by anonymous 08.03.2017 / 17:20

1 answer

1

I do not know if it's exactly what you wanted, but I've put an example below using your class Dados

private void bt_salvar_Click_1(object sender, EventArgs e)
{       
        //No segundo parâmetro, que está vazio, você pode colocar o email
        //digitado em algum textbox, por exemplo.
        Dados dados = new Dados(txt_nome.Text, "");
        DataRow dr = dt.NewRow();

        //Aqui, em vez de pegarmos direto da textbox, pegamos do atributos
        //do objeto que declaramos logo acima
        dr["Nome"] = dados.Nome;

        //Você também poderia, por exemplo, colocar o email em alguma coluna        
        //dr["Email"] = dados.Email;

        dt.Rows.Add(dr);
        ds.Tables.Add(dt);

        dataGridView1.DataSource = dt;
}

If you want to read the data later, you can go through DataGrid by reading its lines and filling in the objects.

    
08.03.2017 / 17:35