Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7738441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:15:57+00:00 2026-06-01T08:15:57+00:00

im having a code a problem with the code.i have a checkedlistbox with 12

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:15:58+00:00Added an answer on June 1, 2026 at 8:15 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having a problem with the dataGridView control being populated with Display members: Code: private
Im having a small problem with my code. I have been trying to read
I'm having a problem with mixing managed and unmanaged code. I have created two
Having a problem getting a TreeView control to display node images. The code below
What is the problem here? (Besides having redundant code). $.getJSON works as expected. However
I am having problem getting this piece of code to run. The class is
I'm having a problem with my Seam code and I can't seem to figure
I'm having a problem with some code I've written. I've had to anonymize it,
I'm having a problem with some code that used to work in PHP 4.X
I did not see anything that address my particular code problem. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.