【问题标题】:how can i assign 2-dimensional array to datagridview?如何将二维数组分配给 datagridview?
【发布时间】:2013-02-04 08:18:21
【问题描述】:

我有 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);

还有其他方法可以实现吗?

【问题讨论】:

标签: c# datagridview multidimensional-array


【解决方案1】:

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

这是一个非常基本的实现,但我相信你明白了

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-13
  • 2020-08-03
  • 1970-01-01
  • 1970-01-01
  • 2020-01-04
  • 1970-01-01
相关资源
最近更新 更多