im having a code a problem with the code.i have a checkedlistbox with 12 months in it..i have a datagridview where i have to display the fees for each month already described in the database.when i select 'june' from checkedlistbox it should retrieve all columns of table where month is june, and when i uncheck the same it should rollback(remove dat particular row) I have a code,
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)
{
}
}
}
when i run this it works fine but the problem is when i select 'june' it shows june and when i select 'july' it overwrites june n shows july,it should add row july,and when i uncheck 'july' it should remove row 'july' n same for 'june'.. plss help srry for anything wrongly explained.