1

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.

2
  • Since you have all the data in the datatable why not filter it there and then assign the source, instead of querying it again and again Commented Apr 3, 2012 at 8:37
  • can you please show how.i feel there is a need for addrow property n instead select query use insert . but i tried im getting errors.im a newbie so cant get through. Commented Apr 3, 2012 at 8:41

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

it is showing me an error .. The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?).where should i declare string var .
@HMcompfreak : Remember to upvote if you think the answer is good. It gives us all warm-fuzzy feelings :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.