Insert database record into columns of DataGridView in C #

3

I have a problem: I have DataGridView1 and when I read the database records each line should be added to a Column of my DataGridView1 . Example my DataGridView1 has the Column1, Column2, ... Column10 columns and when scanning the database I have the logs: 1,2,3 ... 20 where DataGridView1 should be fed as in the image below, anyone have any suggestions how can I do this?

AndthiswouldbemytableinthedatabasewhereIalreadymakeaqueryandgetthevalueofthecolumnFICCODImaketheconversiontointegerandthenaddittothegrid!

ThecodeIamdoingisbelowthisinsomesituationsitdoesnotfeedallrecordsofthebank,ifforexampleIhave15recordsitwillfeedonlythefirst10,itwillonlybeworkingifthebankisanamountofrecordsthatcanbedividedintointervalsof10"closed" records.

private void alimentaGrid()
{           
    DataGridViewListaMesa.Rows.Clear();

    DataSet ds = Engebuilder.Library.LDataAccess.GetDataSet("SELECT FICCOD FROM FICHA");
    int contador = 0;

    ArrayList ListaMesa = new ArrayList();

    foreach(DataRow row in ds.Tables[0].Rows)
    {               
        Int64 codigo = Int64.Parse(row["FICCOD"].ToString().Trim());

        ListaMesa.Add(codigo);  
        contador += 1;

        if(contador.Equals(10))
        {
            string Column1 = String.Empty;
            string Column2 = String.Empty;
            string Column3 = String.Empty;
            string Column4 = String.Empty;
            string Column5 = String.Empty;
            string Column6 = String.Empty;
            string Column7 = String.Empty;
            string Column8 = String.Empty;
            string Column9 = String.Empty;
            string Column10 = String.Empty;

            for(int i = 0; i < ListaMesa.Count; i++)
            {
                Column1 = ListaMesa[0].ToString().Trim();
                Column2 = ListaMesa[1].ToString().Trim();
                Column3 = ListaMesa[2].ToString().Trim();
                Column4 = ListaMesa[3].ToString().Trim();
                Column5 = ListaMesa[4].ToString().Trim();
                Column6 = ListaMesa[5].ToString().Trim();
                Column7 = ListaMesa[6].ToString().Trim();
                Column8 = ListaMesa[7].ToString().Trim();
                Column9 = ListaMesa[8].ToString().Trim();
                Column10 = ListaMesa[9].ToString().Trim();
            }

            DataGridViewListaMesa.Rows.Add(Column1, Column2, Column3, Column4, Column5, Column6, Column7, Column8, Column9, Column10);

            ListaMesa.Clear();
            contador = 0;
        }           
    }
}
    
asked by anonymous 28.07.2015 / 19:39

1 answer

1

I was able to solve my problem as follows: I know that a small gambiara has been left: (

If anyone has any better suggestions, I thank them!

private void alimentaGrid()
{
    DataGridViewListaMesa.Rows.Clear();

    DataSet ds = Engebuilder.Library.LDataAccess.GetDataSet("SELECT FICCOD FROM FICHA");
    int contador = 0;
    int aux = 0;
    int QtdTotal = ds.Tables[0].Rows.Count;
    int resto = QtdTotal;

    ArrayList ListaMesa = new ArrayList();

    foreach (DataRow row in ds.Tables[0].Rows)
    {
        Int64 codigo = Int64.Parse(row["FICCOD"].ToString().Trim());

        ListaMesa.Add(codigo);
        contador += 1;

        if (resto >= 10)
        {
            if (contador.Equals(10))
            {
                string Column1 = String.Empty;
                string Column2 = String.Empty;
                string Column3 = String.Empty;
                string Column4 = String.Empty;
                string Column5 = String.Empty;
                string Column6 = String.Empty;
                string Column7 = String.Empty;
                string Column8 = String.Empty;
                string Column9 = String.Empty;
                string Column10 = String.Empty;

                for (int i = 0; i < ListaMesa.Count; i++)
                {
                    Column1 = ListaMesa[0].ToString().Trim();
                    Column2 = ListaMesa[1].ToString().Trim();
                    Column3 = ListaMesa[2].ToString().Trim();
                    Column4 = ListaMesa[3].ToString().Trim();
                    Column5 = ListaMesa[4].ToString().Trim();
                    Column6 = ListaMesa[5].ToString().Trim();
                    Column7 = ListaMesa[6].ToString().Trim();
                    Column8 = ListaMesa[7].ToString().Trim();
                    Column9 = ListaMesa[8].ToString().Trim();
                    Column10 = ListaMesa[9].ToString().Trim();
                }

                DataGridViewListaMesa.Rows.Add(Column1, Column2, Column3, Column4, Column5, Column6, Column7, Column8, Column9, Column10);

                ListaMesa.Clear();
                contador = 0;
                aux += 1;

                resto = (QtdTotal - (10 * aux));
            }
        }
        else
        {
            switch (resto)
            {
                case 9:
                if (contador.Equals(9))
                {
                    string Column1 = String.Empty;
                    string Column2 = String.Empty;
                    string Column3 = String.Empty;
                    string Column4 = String.Empty;
                    string Column5 = String.Empty;
                    string Column6 = String.Empty;
                    string Column7 = String.Empty;
                    string Column8 = String.Empty;
                    string Column9 = String.Empty;

                    for (int i = 0; i < ListaMesa.Count; i++)
                    {
                        Column1 = ListaMesa[0].ToString().Trim();
                        Column2 = ListaMesa[1].ToString().Trim();
                        Column3 = ListaMesa[2].ToString().Trim();
                        Column4 = ListaMesa[3].ToString().Trim();
                        Column5 = ListaMesa[4].ToString().Trim();
                        Column6 = ListaMesa[5].ToString().Trim();
                        Column7 = ListaMesa[6].ToString().Trim();
                        Column8 = ListaMesa[7].ToString().Trim();
                        Column9 = ListaMesa[8].ToString().Trim();
                    }

                    DataGridViewListaMesa.Rows.Add(Column1, Column2, Column3, Column4, Column5, Column6, Column7, Column8, Column9);

                    ListaMesa.Clear();
                    contador = 0;

                    resto = (resto - 9);
                }
                break;

                case 8:
                if (contador.Equals(8))
                {
                    string Column1 = String.Empty;
                    string Column2 = String.Empty;
                    string Column3 = String.Empty;
                    string Column4 = String.Empty;
                    string Column5 = String.Empty;
                    string Column6 = String.Empty;
                    string Column7 = String.Empty;
                    string Column8 = String.Empty;

                    for (int i = 0; i < ListaMesa.Count; i++)
                    {
                        Column1 = ListaMesa[0].ToString().Trim();
                        Column2 = ListaMesa[1].ToString().Trim();
                        Column3 = ListaMesa[2].ToString().Trim();
                        Column4 = ListaMesa[3].ToString().Trim();
                        Column5 = ListaMesa[4].ToString().Trim();
                        Column6 = ListaMesa[5].ToString().Trim();
                        Column7 = ListaMesa[6].ToString().Trim();
                        Column8 = ListaMesa[7].ToString().Trim();
                    }

                    DataGridViewListaMesa.Rows.Add(Column1, Column2, Column3, Column4, Column5, Column6, Column7, Column8);

                    ListaMesa.Clear();
                    contador = 0;

                    resto = (resto - 8);
                }
                break;

                case 7:
                if (contador.Equals(7))
                {
                    string Column1 = String.Empty;
                    string Column2 = String.Empty;
                    string Column3 = String.Empty;
                    string Column4 = String.Empty;
                    string Column5 = String.Empty;
                    string Column6 = String.Empty;
                    string Column7 = String.Empty;

                    for (int i = 0; i < ListaMesa.Count; i++)
                    {
                        Column1 = ListaMesa[0].ToString().Trim();
                        Column2 = ListaMesa[1].ToString().Trim();
                        Column3 = ListaMesa[2].ToString().Trim();
                        Column4 = ListaMesa[3].ToString().Trim();
                        Column5 = ListaMesa[4].ToString().Trim();
                        Column6 = ListaMesa[5].ToString().Trim();
                        Column7 = ListaMesa[6].ToString().Trim();
                    }

                    DataGridViewListaMesa.Rows.Add(Column1, Column2, Column3, Column4, Column5, Column6, Column7);

                    ListaMesa.Clear();
                    contador = 0;

                    resto = (resto - 7);
                }
                break;

                case 6:
                if (contador.Equals(6))
                {
                    string Column1 = String.Empty;
                    string Column2 = String.Empty;
                    string Column3 = String.Empty;
                    string Column4 = String.Empty;
                    string Column5 = String.Empty;
                    string Column6 = String.Empty;

                    for (int i = 0; i < ListaMesa.Count; i++)
                    {
                        Column1 = ListaMesa[0].ToString().Trim();
                        Column2 = ListaMesa[1].ToString().Trim();
                        Column3 = ListaMesa[2].ToString().Trim();
                        Column4 = ListaMesa[3].ToString().Trim();
                        Column5 = ListaMesa[4].ToString().Trim();
                        Column6 = ListaMesa[5].ToString().Trim();
                    }

                    DataGridViewListaMesa.Rows.Add(Column1, Column2, Column3, Column4, Column5, Column6);

                    ListaMesa.Clear();
                    contador = 0;

                    resto = (resto - 6);
                }
                break;

                case 5:
                if (contador.Equals(5))
                {
                    string Column1 = String.Empty;
                    string Column2 = String.Empty;
                    string Column3 = String.Empty;
                    string Column4 = String.Empty;
                    string Column5 = String.Empty;

                    for (int i = 0; i < ListaMesa.Count; i++)
                    {
                        Column1 = ListaMesa[0].ToString().Trim();
                        Column2 = ListaMesa[1].ToString().Trim();
                        Column3 = ListaMesa[2].ToString().Trim();
                        Column4 = ListaMesa[3].ToString().Trim();
                        Column5 = ListaMesa[4].ToString().Trim();
                    }

                    DataGridViewListaMesa.Rows.Add(Column1, Column2, Column3, Column4, Column5);

                    ListaMesa.Clear();
                    contador = 0;

                    resto = (resto - 5);
                }
                break;

                case 4:
                if (contador.Equals(4))
                {
                    string Column1 = String.Empty;
                    string Column2 = String.Empty;
                    string Column3 = String.Empty;
                    string Column4 = String.Empty;

                    for (int i = 0; i < ListaMesa.Count; i++)
                    {
                        Column1 = ListaMesa[0].ToString().Trim();
                        Column2 = ListaMesa[1].ToString().Trim();
                        Column3 = ListaMesa[2].ToString().Trim();
                        Column4 = ListaMesa[3].ToString().Trim();
                    }

                    DataGridViewListaMesa.Rows.Add(Column1, Column2, Column3, Column4);

                    ListaMesa.Clear();
                    contador = 0;

                    resto = (resto - 4);
                }
                break;

                case 3:
                if (contador.Equals(3))
                {
                    string Column1 = String.Empty;
                    string Column2 = String.Empty;
                    string Column3 = String.Empty;

                    for (int i = 0; i < ListaMesa.Count; i++)
                    {
                        Column1 = ListaMesa[0].ToString().Trim();
                        Column2 = ListaMesa[1].ToString().Trim();
                        Column3 = ListaMesa[2].ToString().Trim();
                    }

                    DataGridViewListaMesa.Rows.Add(Column1, Column2, Column3);

                    ListaMesa.Clear();
                    contador = 0;

                    resto = (resto - 3);
                }
                break;

                case 2:
                if (contador.Equals(2))
                {
                    string Column1 = String.Empty;
                    string Column2 = String.Empty;

                    for (int i = 0; i < ListaMesa.Count; i++)
                    {
                        Column1 = ListaMesa[0].ToString().Trim();
                        Column2 = ListaMesa[1].ToString().Trim();
                    }

                    DataGridViewListaMesa.Rows.Add(Column1, Column2);

                    ListaMesa.Clear();
                    contador = 0;

                    resto = (resto - 2);
                }
                break;

                case 1:
                if (contador.Equals(1))
                {
                    string Column1 = String.Empty;

                    for (int i = 0; i < ListaMesa.Count; i++)
                    {
                        Column1 = ListaMesa[0].ToString().Trim();
                    }

                    DataGridViewListaMesa.Rows.Add(Column1);

                    ListaMesa.Clear();
                    contador = 0;

                    resto = (resto - 1);
                }
                break;

                default:
                break;
            }
        }
    }
}  
    
29.07.2015 / 22:56