在checkedlistbox.check上以编程方式在datagridview中添加行,并在取消选中时删除相同的行

时间:2012-04-03 08:31:05

标签: c# ado.net datagridview

我有一个代码有问题的代码。我有一个12个月的checkedlistbox ..我有一个datagridview我必须显示已经在数据库中描述的每个月的费用。当我选择&#39 ; 6月'从checkedlistbox它应该检索月份为6月的表的所有列,当我取消选中它时应该回滚(删除dat特定行) 我有一个代码,

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
    public Form1()
    {
        InitializeComponent();
        string strCon = "Data Source=.;Initial Catalog=SAHS;User Id=sa;Password=faculty";
        string strSQL = "select mnthname as 'Month',Description,Amount from monthfee";

        SqlDataAdapter dataAdapter = new SqlDataAdapter(strSQL, strCon);
        SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);

        // Populate a new data table and bind it to the BindingSource.
        DataTable table = new DataTable();
        table.Locale = System.Globalization.CultureInfo.InvariantCulture;
        dataAdapter.Fill(table);
        bindingSource1.DataSource = table;

        // Resize the DataGridView columns to fit the newly loaded content.

        dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
        // you can make it grid readonly.
        dataGridView1.ReadOnly = false;
        // finally bind the data to the grid
        dataGridView1.DataSource = bindingSource1;

    }

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
    }

    private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string strCon = "Data Source=.;Initial Catalog=SAHS;User Id=sa;Password=faculty";
        string strSQL = "select mnthname as 'Month',Description,Amount from monthfee where mnthname='" + checkedListBox1.Text + "'";

        SqlDataAdapter dataAdapter = new SqlDataAdapter(strSQL, strCon);
        SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);

        // Populate a new data table and bind it to the BindingSource.
        DataTable table = new DataTable();
        table.Locale = System.Globalization.CultureInfo.InvariantCulture;
        dataAdapter.Fill(table);
        bindingSource1.DataSource = table;

        // Resize the DataGridView columns to fit the newly loaded content.

        dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
        // you can make it grid readonly.
        dataGridView1.ReadOnly = false;
        // finally bind the data to the grid
        dataGridView1.DataSource = bindingSource1;

    }
    private void Form1_Load(object sender, EventArgs e)
    {

        }
    }
}

当我运行它时它工作正常,但问题是当我选择' june'它显示了六月,当我选择'七月'它覆盖了7月6月的7月,它应该添加7月的行,当我取消选中7月份时#7;它应该删除行' 7月'同样适用于' june' .. 如果错误地解释了任何问题,请帮忙。

1 个答案:

答案 0 :(得分:1)

我认为问题在于您没有使用selecteditems中的checkedListBox1。这意味着您要分配包含全部或仅一个月的新数据源。我无法理解为什么你首先在Form()的构造函数中复制代码,然后再在checkedListBox1_SelectedIndexChanged上复制代码。这是一个建议:

public Form1()
{
    InitializeComponent();
    BindGrid();
}
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    BindGrid();
}
private void BindGrid()
{
    CheckedListBox  checkedListBox1=new CheckedListBox();
    StringBuilder sb=new StringBuilder();
    foreach (ListBox item in checkedListBox1.SelectedItems)
    {
        sb.Append("'"+item.Text+"',");
    }
    if(sb.Length>0)
        sb.Length--;

    string strSQL;
    if(sb.Length>0)
    {
        strSQL = "select mnthname as 'Month',Description,Amount from monthfee where mnthname IN(" + sb.ToString()+ ")";
    }
    else
    {
        strSQL = "select mnthname as 'Month',Description,Amount from monthfee";
    }
    string strCon = "Data Source=.;Initial Catalog=SAHS;User Id=sa;Password=faculty";
    SqlDataAdapter dataAdapter = new SqlDataAdapter(strSQL, strCon);
    SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);

    // Populate a new data table and bind it to the BindingSource.
    DataTable table = new DataTable();
    table.Locale = System.Globalization.CultureInfo.InvariantCulture;
    dataAdapter.Fill(table);
    bindingSource1.DataSource = table;

    // Resize the DataGridView columns to fit the newly loaded content.

    dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
    // you can make it grid readonly.
    dataGridView1.ReadOnly = false;
    // finally bind the data to the grid
    dataGridView1.DataSource = bindingSource1;
}