Scroll column in DataGridView and Add C # [closed]

0

How about, I hope and you can help me. I have a datagridview, the data I upload using a txt file, with two fields (Code and Stock).

Now, as I do so that when going through the Code column to find a repeated code I add the STOCK of the two or n codes found.

The result would only be a code and its due updated STOCK of the n codes Help!

    
asked by Perea.PS 27.05.2017 в 00:52
source

1 answer

1

what I would do, if that's just 2 fields, is to load the txt data into a generic KeyValuePair list.

example

        System.IO.StreamReader file = new System.IO.StreamReader(@"c:\stock.txt");
        string line = String.Empty;
        var contenido = file.ReadToEnd();
        file.Close();
        var list = new List<KeyValuePair<string, int>>();
        var dividir = contenido.Split(new string[] { Environment.NewLine}, StringSplitOptions.None);
        foreach (var d in dividir)
        {
            var s = d.Split('\t');
            list.Add(new KeyValuePair<string, int>(s[0], int.Parse(s[1])));
        }
        list.Add(new KeyValuePair<string, int>("SUMA TOTAL", (from l in list select l.Value).Sum()));

        dgstock.DataSource = list;

KeyValuePair

I hope it serves you

UPDATE 2/6/2017

public partial class Test2 : Form
{
    public List<stock> ListaStock { get; set; }
    public Test2()
    {
        InitializeComponent();
        Cargar();
    }
    private void Cargar()
    {
        System.IO.StreamReader file = new System.IO.StreamReader(@"c:\stock.txt");
        string line = String.Empty;
        var contenido = file.ReadToEnd();
        file.Close();
        if (ListaStock != null)
            ListaStock.Clear();
        else
            ListaStock = new List<stock>();
        var dividir = contenido.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
        foreach (var d in dividir)
        {
            var s = d.Split('\t');
            ListaStock.Add(new stock() { Codigo = s[0], Cantidad = int.Parse(s[1])});
        }
        dgvData.DataSource = ListaStock;
    }
    public class stock
    {
        public string Codigo { get; set; }
        public int Cantidad { get; set; }
    }

    private void btnReset_Click(object sender, EventArgs e)
    {
        Cargar();
    }

    private void btnBuscar_Click(object sender, EventArgs e)
    {
        if (txtBuscar.Text == String.Empty)
            return;
        dgvData.DataSource = null;
        var result = (from c in ListaStock where c.Codigo == (String)txtBuscar.Text select c).ToList<stock>();
        var suma = (from s in result select s.Cantidad).Sum();
        result.Add(new stock() { Codigo = "SUMA TOTAL", Cantidad = suma });
        dgvData.DataSource = result;
    }
}

    
answered by 27.05.2017 в 01:38