如何将二维数组分配给datagridview

本文关键字:datagridview 分配 二维数组 | 更新日期: 2023-09-27 17:57:33

我有6列,其中第一列是文本框单元格,其余为复选框。我想把二维数组的值放到数据网格中。

string[,] debugger={{"name","0","0","1","1","0"}};

0=错误1=true(我在datagridview属性窗口中指定了false值和true值。当我尝试这个时,它会给我一个格式异常?

grdFont.ColumnCount=6;
            var row = new DataGridViewRow();
            row.Cells.Add(new DataGridViewTextBoxCell()
            {
               Value = debugger[0] 
            });
            row.Cells.Add(new DataGridViewCheckBoxCell()
            {
                Value = debugger[1]
            });
            row.Cells.Add(new DataGridViewCheckBoxCell()
            {
                Value = debugger[2]
            });
            row.Cells.Add(new DataGridViewCheckBoxCell()
            {
                Value = debugger[3]
            });
            row.Cells.Add(new DataGridViewCheckBoxCell()
            {
                Value = debugger[4]
            });
            row.Cells.Add(new DataGridViewCheckBoxCell()
            {
                Value = debugger[5]
            });
            grdFont.Rows.Add(row);

有其他方法可以实现吗?

如何将二维数组分配给datagridview

0与false不等价,1与true不等价。编写一个函数,将这些数字转换为Boolean值:

public bool ConvertNumberToBoolean(int number)
{
    if (number == 0)
    {
        return false;
    }
    else
    {
        return true;
    }
}

将值设置为单元格时使用此功能:

row.Cells.Add(new DataGridViewCheckBoxCell()
{
    Value = ConvertNumberToBoolean(Convert.ToInt32(debugger[1]));
});

这是一个非常基本的实现,但我相信您已经了解了