簡體   English   中英

我想在 C# 中將 CheckBox 值添加到沒有數據庫的 DataGridView 中

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM