Insert database record into Datagridview columns in C#

Asked

Viewed 231 times

3

I got a problem, 'cause I got one DataGridView1 and when I read the database records each line should be added in a Column of my DataGridView1. My example DataGridView1 has the columns Column1, Column2, ... Column10 and when sweeping the bank I have the records: 1,2,3 ... 20 in which the DataGridView1 should be fed as in the image below, does anyone have any suggestions as to how I can do this?

inserir a descrição da imagem aqui

And this would be my table in the database where I already do a query and get the column value FICCOD I do the whole conversion and then add it to the grid!

inserir a descrição da imagem aqui

The code I am doing is this below more in some situations it does not feed all the bank records, if for example I have 15 records it will feed only the first 10, only be working if the database is a number of records that can be divided into 10-record intervals "closed".

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;
        }           
    }
}
  • You want to swap rows and columns this? I mean, turn rows (records) into columns and columns into rows?

  • It would be basically that, as being in the image the value of each column corresponds to a row in the database.

  • You would have an example of your ai database?

1 answer

1


Guys I managed to solve my problem the following way as being just below, I know it got a little gambiara :(

If anyone has any better suggestions I’d appreciate it!

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.