2010-06-16 31 views
0

我從來沒有使用DataGridView的比一個它是由一個數據庫,以便突然我的頭腦一片空白填充其他其他任何情況下....NET C#datagridview的非數據庫數據/設置填充

我有10個每個管子都有8個垂直位置,所以我基本上有一個10×8的網格。這些插槽中的每一個都有(或不)文件夾中的圖像。我如何得到一個datagridview來反映這些信息,繪製一個網格,檢查文件夾,如果圖像存在繪製它白色,如果不繪成紅色?

對不起,如果聽起來有點古怪,感謝,R.

回答

0

假設它被稱爲DataGridView1包含10列,並且你已經有了一個叫ImageExists方法接受2項INT指標下面應該工作:

dataGridView1.AllowUserToAddRows = false; 
dataGridView1.ReadOnly = true; 
for (int rowIndex = 0; rowIndex < 8; rowIndex++) 
{ 
    DataGridViewRow row = new DataGridViewRow(); 
    row.CreateCells(dataGridView1); 
    dataGridView1.Rows.Add(row); 
    for (int cellIndex = 0; cellIndex < row.Cells.Count; cellIndex++) 
    { 
      if (!ImageExists(rowIndex, cellIndex)) 
       row.Cells[cellIndex].Style.BackColor = Color.Red;       
    } 
} 
0

這可能與在虛擬模式下設置網格很好地工作:

  • 設置網格的VirtualMode屬性爲True。
  • 添加處理的CellValueNeeded事件,東西線沿線的:

private void Form1_Load(object sender, EventArgs e) 
{ 
    dataGridView1.RowCount = 8; 
    dataGridView1.ColumnCount = 10; 
} 

private void dataGridView1_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e) 
{ 
    var bgColor = ((0 == e.ColumnIndex % 2) && (0 == e.RowIndex % 2)) 
     ? Color.Red 
     : Color.White; 
    dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = bgColor; 
} 

當然,%2的東西會被替換爲實際的圖像存在檢查。

更多關於DataGridView的虛擬模式here