2017-05-14 75 views
0

所以我試圖以datagridview的特定格式顯示我的數據。 所以我的格式是這樣的:DatagridView如何爲每列有不同的行數?

A B C 

1 1 1 

2 2 x 

3 x x 

x表示沒有單元格。

正如你可以看到每列有不同的行數。我想在DotNet框架中的DatagridView或任何其他控件中獲得相同的結果。

+0

這是不可能的,在所有作爲DGV總存放的2D陣列。當然,這取決於你填寫哪些單元格。 - 您可以在列表視圖中爲每個項目設置不同的列數,因爲它的項目是鋸齒狀的數組。因此,所顯示的效果是可能的,但只有在每行末尾的缺失單元中才有可能。 – TaW

回答

1

要擴大jdweng's answer,如果由於某種原因,你真正想要的:

[T]他X表示沒有細胞。

然後您可以處理DataGridView.CellPainting事件以有效隱藏空單元格。請注意,它將開始看起來奇數null單元格在價值單元格中混合 - 而不僅僅是在行結束。

// ... 
dt.Rows.Add(new object[] { 3, null, null }); 

this.dataGridView1.DataSource = dt; 
this.dataGridView1.CellPainting += DataGridView1_CellPainting; 

private void DataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) 
{ 
    if (e.RowIndex >= 0 && e.ColumnIndex >= 0) 
    { 
     DataGridViewCell cell = this.dataGridView1[e.ColumnIndex, e.RowIndex]; 

     if (cell.Value == null || cell.Value is DBNull) 
     { 
      using (SolidBrush brush = new SolidBrush(this.dataGridView1.BackgroundColor)) 
      { 
       e.Graphics.FillRectangle(brush, e.CellBounds); 
      } 

      e.Handled = true; 
     } 
    } 
} 

Empty cell's painted like DGV background

+0

哇這真的是答案 – Apple

+0

我會接受你的答案作爲真正的答案。 – Apple

2

嘗試以下

  DataTable dt = new DataTable("MyDataTable"); 

      dt.Columns.Add("A", typeof(int)); 
      dt.Columns.Add("B", typeof(int)); 
      dt.Columns.Add("C", typeof(int)); 

      dt.Columns["A"].AllowDBNull = true; 
      dt.Columns["B"].AllowDBNull = true; 
      dt.Columns["C"].AllowDBNull = true; 

      dt.Rows.Add(new object[] { 1,2,3}); 
      dt.Rows.Add(new object[] { 2, 2, }); 
      dt.Rows.Add(new object[] { 3 }); 

      datagridview1.DataSource = dt; 
+0

謝謝你,我正在尋找 – Apple

相關問題