在checkedlistbox.check上以编程方式在datagridview中添加行,在取消选中时删除行
本文关键字:添加行 删除行 取消 datagridview check checkedlistbox 编程 方式 | 更新日期: 2023-09-27 18:29:45
我有一个代码问题。我有一一个checkedlistbox,里面有12个月。我有数据网格视图,我必须在其中显示数据库中已经描述的每个月的费用。当我从checkedlistbox'中选择'june'时,它应该检索表中月份为june的所有列,当我取消选中该列时,它会回滚(删除特定行)我有一个代码,
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"时,它显示june,当我选中"july"时,会覆盖june n显示july,它应该添加july行,当我取消选中"july'"时,应该删除Julyn行。。请帮助srry解决任何解释错误的问题。
我认为问题在于您没有在checkedListBox1
中使用selecteditems
。因此,这意味着您正在分配包含全部或仅包含一个月的新数据源。我不明白为什么要先在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;
}