![](/img/trans.png)
[英]C# I want to add the Value data in datagridview with looping statement
[英]I want to add CheckBox Value into DataGridView without Database in C#
我想使用列表将复选框值添加到数据库中
private List<Months> ShopingCart = new List<Months>();
注意:月份是一个在这里定义的类
public class Months
{
[DisplayName("ID")]
public string ItemId { get; set; }
}
在月份列表中添加项目喜欢
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 中删除 **
我想当 Uncheck CheckBox 删除从 Datagridview 中 ubchecked 的 Checkbox 数据时
这是我的解决方案。 我希望它会起作用。
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
,将所有checkBox放入其中。
2.代码逻辑:
一种。 您可以为所有复选框注册CheckedChanged
事件。
湾如果选中 CheckBox, ShopingCart
将添加当前items
。 如果 CheckBox 没有勾选,可以记录CheckBox.Name
并根据Name
找到对应的Month
,然后就知道ItemId
。最后ShopingCart
删除与ItemId
匹配的item
。
代码如下:
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; }
}
}
结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.