简体   繁体   中英

I want to add CheckBox Value into DataGridView without Database in C#

I want to add CheckBox Value into database using list

  private List<Months> ShopingCart = new List<Months>();

Note : Months is a Class define here

  public class Months
{
    [DisplayName("ID")]
    public string ItemId { get; set; }
}

Add items in Months List Like

  Months items = new Months()
        {

            ItemId =GetObjc(),
        };

GetObjc() is a Function like

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

List Add To 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;
        }

**Now Problem is that when I check the checkbox the month's name add and when I unchecked then not remove from datagridview **

I want when Uncheck CheckBox remove that Checkbox data which is ubchecked from Datagridview

Here is my solution. I hope it would work.

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

You can try the following steps to solve it.
1.Form design:Place a groupBox on the form, and put all checkBoxes in it.

2.Code logic:
a. You could register CheckedChanged event for all checkBoxes.
b.If CheckBox is selected, ShopingCart adds the current items . If the CheckBox is unchecked, you could record the CheckBox.Name and find the corresponding Month according to the Name , then you will know the ItemId .Finally ShopingCart removes the item matching the ItemId .

The code is as follows:

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

Result:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM