2014-10-02 85 views
4

我有一个奇怪的问题。 基本上我有一个datagridview和一个按钮。当我点击这个按钮时,它会检查列1s的所有行值 - 复选框列。然后根据当前的内容将其设置为真/假。DatagridView复选框列始终为空

这一切都很好。

但是,然后,我有另一个按钮来做这些行被打勾的东西。我点击它,它只能识别第一行被勾选。其余的显然现在是空的..?

那么,我怎样才能编程设置一个复选框列的值在一个DataGrid视图,然后再次阅读,因为我显然是基于我的结果标记的方式。

这将蜱boxs,我可以看到他们,他们的勾去掉手动等

foreach (DataGridViewRow row in dgv.Rows) 
     { 
      var ch1 = new DataGridViewCheckBoxCell(); 
      ch1 = (DataGridViewCheckBoxCell)row.Cells[0]; 

      if (ch1.Value == null) 
       ch1.Value = false; 
      switch (ch1.Value.ToString()) 
      { 
       case "True": 
        ch1.Value = false; 
        break; 
       case "False": 
        ch1.Value = true; 
        break; 
      } 
     } 

那么接下来按钮,检查值只是找到空

foreach (DataGridViewRow row in rows) 
      { 
       var ch1 = new DataGridViewCheckBoxCell(); 
       ch1 = (DataGridViewCheckBoxCell)row.Cells[0]; 

       if (ch1.Value == null) 
        ch1.Value = false; 
       switch (ch1.Value.ToString()) 
       { 
        case "True": 
         ch1.Value = true; 
         break; 
        case "False": 
         ch1.Value = false; 
         break; 
       } 
       var val = row.Cells["EbayListingID"].Value.ToString(); 
       if (ch1.Value.ToString() == "true") continue; 
       var listing = dsEntities.EbayListings.First(x => x.EbayListingID.ToString() == val); 
       SubmitListingForReview(listing, false); 
      } 
+0

你可能做错了,但是,如果我们不知道自己在做什么,我们不知道这有什么错它。 – jmcilhinney 2014-10-02 00:44:18

+0

您设置并读取与普通组合框相同的数据。如果您希望我们告诉您您做错了什么,那么我们需要查看您的代码 – deathismyfriend 2014-10-02 00:50:39

+0

尝试发布您的代码。这将帮助我们知道你已经尝试了什么,没有。但是,我的假设是,您的模型没有正确开发来存储此复选框的结果。你是在数据绑定你的datagridview还是手动创建行? – 2014-10-02 01:10:29

回答

0

首先,

if (ch1.Value.ToString() == "true") continue; 

为什么字符串常量是“真”,但不是“真”?

二,在下一个按钮点击处理程序中,它是什么“行”?

foreach (DataGridViewRow row in rows) 

我试试这个代码,并能正常工作:

private void button1_Click(object sender, EventArgs e) 
     { 
      foreach (DataGridViewRow row in dataGridView1.Rows) 
      { 
       var ch1 = new DataGridViewCheckBoxCell(); 
       ch1 = (DataGridViewCheckBoxCell)row.Cells[0]; 

       if (ch1.Value == null) 
        ch1.Value = false; 
       switch (ch1.Value.ToString()) 
       { 
        case "True": 
         ch1.Value = false; 
         break; 
        case "False": 
         ch1.Value = true; 
         break; 
       } 
      } 
     } 

private void button2_Click(object sender, EventArgs e) 
     { 
      foreach (DataGridViewRow row in dataGridView1.Rows) 
      { 
       var ch1 = new DataGridViewCheckBoxCell(); 
       ch1 = (DataGridViewCheckBoxCell)row.Cells[0]; 

       if (ch1.Value == null) 
        ch1.Value = false; 
       switch (ch1.Value.ToString()) 
       { 
        case "True": 
         ch1.Value = true; 
         break; 
        case "False": 
         ch1.Value = false; 
         break; 
       } 
       var val = row.Cells[1].Value; 
       if (ch1.Value.ToString() == "True") continue; 
       MessageBox.Show("1"); 
      } 
     }