Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net c#datagridview填充非数据库数据/设置_C#_Visual Studio 2008 - Fatal编程技术网

.net c#datagridview填充非数据库数据/设置

.net c#datagridview填充非数据库数据/设置,c#,visual-studio-2008,C#,Visual Studio 2008,我从来没有在任何其他场景中使用过datagridview,除了一个由数据库填充的场景,所以我的大脑突然变得一片空白 我有10根管子,每个管子内有8个垂直位置,所以我基本上有一个10×8的网格。每个插槽在文件夹中都有(或没有)一个图像。如何获取datagridview以反映此信息、绘制网格、检查文件夹,如果图像存在,则将其绘制为白色,如果不存在,则将其绘制为红色 抱歉,如果听起来有点奇怪,谢谢,R.假设它名为DataGridView1,包含10列,并且您有一个名为ImageExists的方法,该方

我从来没有在任何其他场景中使用过datagridview,除了一个由数据库填充的场景,所以我的大脑突然变得一片空白

我有10根管子,每个管子内有8个垂直位置,所以我基本上有一个10×8的网格。每个插槽在文件夹中都有(或没有)一个图像。如何获取datagridview以反映此信息、绘制网格、检查文件夹,如果图像存在,则将其绘制为白色,如果不存在,则将其绘制为红色


抱歉,如果听起来有点奇怪,谢谢,R.

假设它名为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;                        
     }
}
dataGridView1.AllowUserToAddress=false;
dataGridView1.ReadOnly=true;
对于(int-rowIndex=0;rowIndex<8;rowIndex++)
{
DataGridViewRow行=新建DataGridViewRow();
CreateCells(dataGridView1);
dataGridView1.Rows.Add(row);
对于(int-cellIndex=0;cellIndex
这可以很好地与虚拟模式下的网格集配合使用:

  • 将网格的
    VirtualMode
    属性设置为True
  • 向CellValueRequired事件添加一个处理程序,如下所示:

当然,%2内容将替换为实际的图像存在性检查

有关虚拟模式下DataGridView的更多信息

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