Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.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
C# 我想将复选框值添加到DataGridView中,而不使用C中的数据库#_C#_.net_Visual Studio - Fatal编程技术网

C# 我想将复选框值添加到DataGridView中,而不使用C中的数据库#

C# 我想将复选框值添加到DataGridView中,而不使用C中的数据库#,c#,.net,visual-studio,C#,.net,Visual Studio,我想使用列表将复选框值添加到数据库中 private List<Months> ShopingCart = new List<Months>(); 在月份列表中添加项目,如 Months items = new Months() { ItemId =GetObjc(), }; GetObjc()是一个类似 private string GetObjc() { string Mo

我想使用列表将复选框值添加到数据库中

  private List<Months> ShopingCart = new List<Months>();
在月份列表中添加项目,如

  Months items = new Months()
        {

            ItemId =GetObjc(),
        };
GetObjc()是一个类似

 private string GetObjc()
    {
        string Month = String.Empty;
        if (checkBox1.Checked)
        {
            Month = "Apr";
        }

        if (checkBox2.Checked)
        {
            Month = "March";
        }

        if (checkBox3.Checked)
        {
            Month = "May";
        }
        if(checkBox4.Checked)
        {
            Month = "June";
        }
        if (checkBox5.Checked)
        {
            Month = "July";
        }


        return Month;
    }
列表添加到GridView

 //List

        if (checkBox1.Checked || checkBox2.Checked)
        {
            ShopingCart.Add(items);
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = ShopingCart;
        }
        else
        {
            ShopingCart.Remove(items);
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = ShopingCart;
        }
**现在的问题是,当我选中该复选框时,月份的名称将被添加,而当我未选中时,则不会从datagridview中删除**


当取消选中复选框时,我想删除从Datagridview中取消选中的复选框数据。我希望它能起作用

if (checkBox1.Checked || checkBox2.Checked)
{
        ShopingCart.Add(items);
        dataGridView1.DataSource = null;
        dataGridView1.DataSource = ShopingCart;
}
else if(checkBox1.Unchecked || checkBox2.Unchecked)
{
    ShopingCart.Remove(items);
    dataGridView1.DataSource = null;
    dataGridView1.DataSource = ShopingCart;
}

您可以尝试以下步骤来解决此问题。
1.表单设计:在表单上放置一个
groupBox
,并将所有复选框放入其中

2.代码逻辑:
A.您可以为所有复选框注册
CheckedChanged
事件。
B如果选中复选框,
ShopingCart
添加当前的
项目
。 如果未选中该复选框,您可以记录
复选框。Name
并根据
Name
找到相应的
月份
,然后您将知道
项目ID
。最后
ShopingCart
删除与
项目ID
匹配的
项目

代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

namespace TestDemo
{     
public partial class Form1 : Form
{
    private List<Months> ShopingCart = new List<Months>();     
    public Form1()
    {
        InitializeComponent();
        foreach(CheckBox checkbox in groupBox1.Controls) 
        {             
                checkbox.CheckedChanged += Checkbox_CheckedChanged;
        }           
    }
    private string GetObjc(string name)
    {
        string Month = String.Empty;
        switch (name) 
        {
            case "checkBox1": Month = "Apr"; break;
            case "checkBox2": Month = "March"; break;
            case "checkBox3": Month = "May"; break;
            case "checkBox4": Month = "June"; break;
            case "checkBox5": Month = "July"; break;
        }       
        return Month;
    }
    private void Checkbox_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox send = (CheckBox)sender;
        string checkboxName = send.Name;
        string Month= GetObjc(checkboxName);
        Months items = new Months()
        {
            ItemId = Month,            
        };       
        if (send.Checked) 
        {                            
            ShopingCart.Add(items);
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = ShopingCart;
        }
        else 
        {
            foreach (var item in ShopingCart) 
            {
                if (item.ItemId == Month)
                { 
                    ShopingCart.Remove(item);
                    break;
                } 
            }             
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = ShopingCart;
        }     
    }
}
public class Months
{
    [DisplayName("ID")]
    public string ItemId { get; set; }
}   
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用System.Windows.Forms;
名称空间测试演示
{     
公共部分类Form1:Form
{
私有列表购物车=新列表();
公共表格1()
{
初始化组件();
foreach(groupBox1.Controls中的复选框)
{             
checkbox.CheckedChanged+=checkbox\u CheckedChanged;
}           
}
私有字符串GetObjc(字符串名称)
{
string Month=string.Empty;
交换机(名称)
{
案例“checkBox1”:月=4月;中断;
案例“checkBox2”:月=“三月”;中断;
案例“checkBox3”:月=“五月”;中断;
案例“checkBox4”:月=“六月”;中断;
案例“checkBox5”:月=“七月”;中断;
}       
返回月份;
}
私有无效复选框\u CheckedChanged(对象发送方,事件参数e)
{
复选框send=(复选框)sender;
字符串checkboxName=send.Name;
字符串Month=GetObjc(checkboxName);
月份项目=新月份()
{
ItemId=月份,
};       
如果(发送。选中)
{                            
ShopingCart.Add(项目);
dataGridView1.DataSource=null;
dataGridView1.DataSource=ShopingCart;
}
其他的
{
foreach(ShopingCart中的var项目)
{
如果(item.ItemId==月)
{ 
ShopingCart。删除(项目);
打破
} 
}             
dataGridView1.DataSource=null;
dataGridView1.DataSource=ShopingCart;
}     
}
}
公课月份
{
[显示名称(“ID”)]
公共字符串ItemId{get;set;}
}   
}
结果:


此外,您可以在此Microsoft Docs链接中选中复选框行为:不按我所说的那样工作,在选中时添加值,但在未选中时不删除值如果遇到其他问题,请随时与我联系。如果没有,你可以标出答案。这将帮助其他面临同样问题的人。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

namespace TestDemo
{     
public partial class Form1 : Form
{
    private List<Months> ShopingCart = new List<Months>();     
    public Form1()
    {
        InitializeComponent();
        foreach(CheckBox checkbox in groupBox1.Controls) 
        {             
                checkbox.CheckedChanged += Checkbox_CheckedChanged;
        }           
    }
    private string GetObjc(string name)
    {
        string Month = String.Empty;
        switch (name) 
        {
            case "checkBox1": Month = "Apr"; break;
            case "checkBox2": Month = "March"; break;
            case "checkBox3": Month = "May"; break;
            case "checkBox4": Month = "June"; break;
            case "checkBox5": Month = "July"; break;
        }       
        return Month;
    }
    private void Checkbox_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox send = (CheckBox)sender;
        string checkboxName = send.Name;
        string Month= GetObjc(checkboxName);
        Months items = new Months()
        {
            ItemId = Month,            
        };       
        if (send.Checked) 
        {                            
            ShopingCart.Add(items);
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = ShopingCart;
        }
        else 
        {
            foreach (var item in ShopingCart) 
            {
                if (item.ItemId == Month)
                { 
                    ShopingCart.Remove(item);
                    break;
                } 
            }             
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = ShopingCart;
        }     
    }
}
public class Months
{
    [DisplayName("ID")]
    public string ItemId { get; set; }
}   
}