2015-03-31 66 views
0

我想在DataGridView上显示所有DataSource行,但不是行,而是行中的列。每个检索到的12个项目,我想在DataGridView上插入一个新行,并使用DataSource中的更多项目(每行12个)填充此新行。将数据源行显示为DataGridView上的列(Winforms)

我的数据源每个检索一个项目,并直接使用它与DataGridView工作很好,但显示为每个项目不同的行。

任何提示?

+1

[这是你问的?](http://www.codeproject.com/Articles/19313/Displaying-Vertical-Rows-in-DataGrid-View)。没有内置的支持。 – 2015-03-31 12:24:21

+0

@SriramSakthivel几乎在那里,男人。我GOOGLE了很多,但没有发现。完成后我会在这里发布我的最终代码。非常感谢您的回答。 – 2015-03-31 12:36:24

+1

我很高兴它帮助你。我认为你没有使用正确的关键字。我搜索谷歌和第一击是我提供的链接。搜索词是“vertical datagridview c#”:) :) – 2015-03-31 12:50:22

回答

0

感谢@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; 
    }