如何显示行作为DataGridView列? [英] How to display rows as columns in DataGridView?

查看:156
本文介绍了如何显示行作为DataGridView列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图显示设置已使用在VS 2008中的DataGridView SQL数据库检索的数据,但我需要显示的数据垂直而不是水平。这是我在开始做了。



<预类=郎-CS prettyprint-覆盖> con.Open();
的SqlCommand CMD =新的SqlCommand(proc_SearchProfileCON);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(@ scute_id,SqlDbType.VarChar,(10))值= VAL。
SqlDataAdapter的适应=新SqlDataAdapter的(CMD);
的DataSet DSET =新的DataSet();

adapt.Fill(DSET,档案);
this.dataGridView1.DataSource = DSET;
this.dataGridView1.DataMember =档案;



我搜索和读取线程数,但这些都不工作。 ?谁能帮我在一个DataGridView垂直显示检索到的数据。


解决方案

试试这个:

  VAR TBL = dset.Tables [档案]:
变种swappedTable =新的DataTable();
的for(int i = 0; I< = tbl.Rows.Count;我++)
{
swappedTable.Columns.Add(Convert.ToString(I));
}
为(INT COL = 0;&山坳下,tbl.Columns.Count;西++)
{
变种R = swappedTable.NewRow(); $ B $; B R [0] = tbl.Columns [山口]的ToString();
为(INT J = 1; J< = tbl.Rows.Count; J ++)$ B $; B R [J] = tbl.Rows [J - 1] [COL];

swappedTable.Rows.Add(R);
}
dataGridView1.DataSource = swappedTable;


I'm trying to display set of data which have retrieved from the sql database using a datagridview in VS 2008. But I need to display data vertically rather than horizontally. This is what I have done at the beginning.

con.Open();
SqlCommand cmd = new SqlCommand("proc_SearchProfile", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@scute_id", SqlDbType.VarChar, (10)).Value = val;
SqlDataAdapter adapt = new SqlDataAdapter(cmd);
DataSet dset = new DataSet();

adapt.Fill(dset, "Profile");
this.dataGridView1.DataSource = dset;
this.dataGridView1.DataMember = "Profile";

I searched and read a few threads but none of those work. Can anyone help me on displaying retrieved data in a datagridview vertically?

解决方案

Try this:

var tbl = dset.Tables["Profile"]:
var swappedTable = new DataTable();
for (int i = 0; i <= tbl.Rows.Count; i++)
{
    swappedTable.Columns.Add(Convert.ToString(i));
}
for (int col = 0; col < tbl.Columns.Count; col++)
{
    var r = swappedTable.NewRow();
    r[0] = tbl.Columns[col].ToString();
    for (int j = 1; j <= tbl.Rows.Count; j++)
        r[j] = tbl.Rows[j - 1][col];

    swappedTable.Rows.Add(r);
}
dataGridView1.DataSource = swappedTable;

这篇关于如何显示行作为DataGridView列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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