在DataGridView(Winforms)上将数据源行显示为列 [英] Show datasource rows as columns on a DataGridView (Winforms)

查看:148
本文介绍了在DataGridView(Winforms)上将数据源行显示为列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在DataGridView上显示我所有的DataSource行,但不是以行的形式显示在行的列中。每个12项检索,我想在DataGridView上插入一个新行,并从DataSource(每行12个)中添加更多项目来填充此行。

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).

我的DataSource检索每个都只有一个项目,并且直接使用DataGridView是非常有效的,但每个项目都显示了不同的行。

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.

任何提示?

推荐答案

感谢@SriramSakthivel。

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

这篇关于在DataGridView(Winforms)上将数据源行显示为列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆