HMcompfreak
HMcompfreak

Reputation: 171

adding row in datagridview programmatically on checkedlistbox.check and delete same on uncheck

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.

Upvotes: 1

Views: 1235

Answers (1)

Arion
Arion

Reputation: 31239

I think the problem is that you are not using the selecteditems in the checkedListBox1. So that means that you are assigning the new datasource that contains all or just one Month. And I can not see why you have duplicate your code first in the constructor of the Form() and then again on the checkedListBox1_SelectedIndexChanged. Here is a suggestion:

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

Upvotes: 1

Related Questions