0
votes

I wanna show all my DataSource rows on a DataGridView, but not as rows but as columns of a row. Each 12 items retrieved, I wanna insert a new row on the DataGridView and populate this new row with more items from the DataSource (12 at each row).

My DataSource retrieves just one item each, and using it directly with the DataGridView is working nicely, but shown a different row for each item.

Any hints?

1
Is this what you're asking about?. There is no built in support. - Sriram Sakthivel
@SriramSakthivel Almost there, man. I've googled a lot and didn't found that yet. I'll post my final code here when finished. Thanks a lot for your answer. - Nilo Paim
I'm glad that it helped you. I think you failed to use the right keywords. I searched google and first hit is the link which I provided. Search term is "vertical datagridview c#" :) - Sriram Sakthivel
@SriramSakthivel Sure I made a wrong search. The link you sent almost solve my problem, except for the fact that it puts ALL my datasource rows on the same final horizontal row, and I need to "create" a new row each 12 items of my datasource. But still working... Thanks again. - Nilo Paim

1 Answers

0
votes

Thanks to @SriramSakthivel.

private void AddToList(DataTable dt)
    {
        possibleWords = 0;

        // Cleans the data grid view
        WordList.DataSource = null;
        WordList.Refresh();

        // Let's transform the original data table onto another, changing rows by columns
        DataTable table = new DataTable();

        for (int i = 0; i < 10; i++)
        {
            table.Columns.Add(Convert.ToString(i));
        }

        DataRow r;

        int col = 0;
        //for (int k = 0; k < dt.Columns.Count; k++)
        {
            r = table.NewRow();

            for (int j = 0; j < dt.Rows.Count; j++)
            {
                if (col >= 10)
                {
                    table.Rows.Add(r);
                    col = 0;
                    r = table.NewRow();
                }

                r[col++] = (dt.Rows[j][0]).ToString().ToUpper();
                possibleWords++;
            }

            table.Rows.Add(r);
        }

        // Puts the new data table as datasource of the word list
        DataView dv = table.DefaultView;
        WordList.DataSource = dv;

        if (possibleWords == 0)
            return;

        WordList.Columns[0].DefaultCellStyle.BackColor = Color.WhiteSmoke;
        WordList.ColumnHeadersVisible = false;
        WordList.RowHeadersVisible = false;
    }