Load two fields from a DataGrid using a txt file

1

How can I do this, see what I have so far:

DataGrid, I need to load comboBox and valor unitário with values that I previously saved in a txt

RecordProductsbutton,savename-price

privatevoidbtnGravar_Click(objectsender,EventArgse){i++;doublevtotal=0;vtotal=Convert.ToDouble(txtVlrVenda.Text)*Convert.ToInt32(txtQntd.Text);dgvProdutos.Rows.Add(i,txtNome.Text,txtFornecedor.Text,maskCNPJ.Text,txtEnd.Text,txtCidade.Text,txtUF.Text,maskFone.Text,txtEmail.Text,txtNfe.Text,txtVlrVenda.Text,txtQntd.Text,vtotal);using(StreamWriterwriter=newStreamWriter(@"C:\Users\willian\Downloads\dbProdutos.txt", true))
            {
                writer.WriteLine(txtNome.Text + " - " + txtVlrVenda.Text);
            }


            txtNome.Text = "";
            txtFornecedor.Text = "";
            maskCNPJ.Text = "";
            txtEnd.Text = "";
            txtCidade.Text = "";
            txtUF.Text = "";
            maskFone.Text = "";
            txtEmail.Text = "";
            txtNfe.Text = "";
            txtVlrVenda.Text = "";
            txtQntd.Text = "";

            btnEditar.Enabled = true;
            btnExcluir.Enabled = true;
            btnGravar.Enabled = false;
        }

In this previous topic: Save in txt and recover in a comboBox taught me to load comboBox , however in this case I did not understand how I can access the column I created inside the DataGrid. The code so far looks like this:

private void frmOrdemServico_Load(object sender, EventArgs e)
        {


            string[] lineOfContents = File.ReadAllLines(@"C:\Users\willian\Downloads\dbClientes.txt");
            cbClientes.Items.Clear(); // limpar para não duplicar valores
            foreach (var line in lineOfContents)
            {
                string[] nomes = line.Split(',');
                cbClientes.Items.Add(nomes[0]);
            }


            string[] Produtos = File.ReadAllLines(@"C:\Users\willian\Downloads\dbProdutos.txt");
            foreach (var line in Produtos)
            {
                string[] produtos = line.Split('-');
                dgvProdutos. //comboBox que irá carregar os produtos
                dgvProdutos. //txtBox que irá mostrar o valor de cada produto seleciona, ja cadastrado no txt
            }
        }

Can anyone help me with something? I even found this site that teaches to retrieve values that are concatenated but for in the DataGrid does not say anything: Recording and Reading data in text file with Csharp

    
asked by anonymous 27.10.2017 / 12:20

1 answer

1

One solution is to use a DataTable for the service.

Add to your project a DataTable with two columns, 'Product' and 'Price'

public DataTable produtos = new DataTable() { Columns = { new DataColumn { ColumnName = "Produto" }, new DataColumn { ColumnName = "Preço" } } };

Fill in this DataTable with the data in the file as follows:

public static void lerProdutos()
{
    DataGridViewComboBoxColumn comboBox = (DataGridViewComboBoxColumn)dataGridView.Columns[0];
    string[] Produtos = File.ReadAllLines(@"C:\Users\willian\Downloads\dbProdutos.txt");
    foreach (var line in Produtos)
    {
        string[] Dados = line.Split('-');
        produtos.Rows.Add(Dados[0], Dados[1]);
        comboBox.Items.Add(Dados[0]);
    }
}

In your DataGridView, add the EditingControlShowing event that will check if the comboBox value has changed

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        if (dataGridView.CurrentCell.ColumnIndex == 1 && e.Control is ComboBox)
        {
            ComboBox comboBox = e.Control as ComboBox;
            comboBox.SelectedIndexChanged -= valorModificado;
            comboBox.SelectedIndexChanged += valorModificado;
        }
    }

and the event that will fetch the price of the product:

private void valorModificado(object sender, EventArgs e)
    {
        DataGridViewComboBoxColumn comboBox = (DataGridViewComboBoxColumn)dataGridView.Columns[0];

        DataRow[] rows = produtos.Select($"Produto = '{comboBox.ValueMember}'");
        string valor = rows[0][0].ToString();
        comboBox.ValueMember = rows[0][0].ToString();
    }
    
27.10.2017 / 14:44