有没有办法以编程方式调用DataGridView中的复选框和列?

时间:2018-02-14 01:21:52

标签: c# excel checkbox datagridview

我正在制作Excel验证计划。导入后,如果它们是小数,则每个列都将被验证。

enter image description here

复选框是动态制作的,并使用以下代码表示每个标签的数据表中的列(标签是Excel文件中的表格):

int dynamicHeight = 0;
int padding = 10;

CheckBox[] chk = new CheckBox[dt.Columns.Count];

for (int i = 0; i <= dt.Columns.Count - 1; i ++)
{


    chk[i] = new CheckBox();
    chk[i].Name = dt.Columns[i].ColumnName;
    chk[i].Text = dt.Columns[i].ColumnName;
    chk[i].AutoCheck = true;
    chk[i].Bounds = new Rectangle(10, 20 + padding + dynamicHeight, 40, 22);
    panelCol.Controls.Add(chk[i]);
    dynamicHeight += 20;
    panelCol.Size = new Size(120, dynamicHeight);
    panelCol.Controls.Add(chk[i]);
    chk[i].Location = new Point(0, dynamicHeight);
    chk[i].Size = new Size(120, 21);
    panelCol.BackColor = Color.White;
    panelCol.AutoScroll = true;
    //panelCol.AutoScrollMinSize = new Size (0, 1200);
}

我想打电话给他们,但 动态地 。我正在做的是 手动 预设每个复选框,如下所示:

chk[0].CheckedChanged += (s, e) =>
{
    decimal num;
    if (chk[0].Checked == true && chk[0].Name.Contains(dt.Columns[0].ColumnName))
    {
        //MessageBox.Show("HELLOW WORLD " + 0);

        for (int i = 0; i < dataGridView1.RowCount; i++)
        {
            if (!Decimal.TryParse(dataGridView1.Rows[i].Cells["January"].Value.ToString(), out num))
            {
                if (dataGridView1.Rows[i].Cells[dt.Columns["January"].ColumnName].Value.ToString() == null || dataGridView1.Rows[i].Cells[dt.Columns["January"].ColumnName].Value.ToString() == "")
                {

                }
                else
                {
                    MessageBox.Show(dataGridView1.Rows[i].Cells["January"].Value.ToString() + "  NOT A DECIMAL!");
                }

            }
            else
            {

            }
        }
    }
};

是否有针对此的修复或解决方案,以便我不必手动预设它们?如果excel文件具有不同的列名称意味着再次预设新列,会导致什么。

1 个答案:

答案 0 :(得分:1)

CheckBox[] chk = new CheckBox[dt.Columns.Count];
for (int i = 0; i <= dt.Columns.Count - 1; i ++)
{
    chk[i] = new CheckBox();
    chk[i].Name = dt.Columns[i].ColumnName;
    chk[i].Text = dt.Columns[i].ColumnName;
    chk[i].AutoCheck = true;
    chk[i].Bounds = new Rectangle(10, 20 + padding + dynamicHeight, 40, 22);
    panelCol.Controls.Add(chk[i]);
    dynamicHeight += 20;
    panelCol.Size = new Size(120, dynamicHeight);
    panelCol.Controls.Add(chk[i]);
    chk[i].Location = new Point(0, dynamicHeight);
    chk[i].Size = new Size(120, 21);
    panelCol.BackColor = Color.White;
    panelCol.AutoScroll = true;
    //panelCol.AutoScrollMinSize = new Size (0, 1200);

    chk[i].CheckedChanged += (s, ev) =>
    {
        decimal num;
        if (chk[i].Checked == true && chk[i].Name.Contains(dt.Columns[0].ColumnName))
        {
        //MessageBox.Show("HELLOW WORLD " + 0);

          for (int i = 0; i < dataGridView1.RowCount; i++)
            {
              if (!Decimal.TryParse(dataGridView1.Rows[i].Cells[chk[i].Name].Value.ToString(), out num))
                {
                  if (dataGridView1.Rows[i].Cells[dt.Columns[chk[i].Name].ColumnName].Value.ToString() == null || dataGridView1.Rows[i].Cells[dt.Columns[chk[i].Name].ColumnName].Value.ToString() == "")
                    {

                    }
                    else
                    {
                    MessageBox.Show(dataGridView1.Rows[i].Cells[chk[i].Name].Value.ToString() + "  NOT A DECIMAL!");
                    }

                 }
                 else
                 {

                  }
             }
         }
    };

}

这未经过测试或其他任何内容,但它是您想要完成的基本想法。