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 8219037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:00:55+00:00 2026-06-07T13:00:55+00:00

I have a datagridview on my page, the datasource of which keeps changing based

  • 0

I have a datagridview on my page, the datasource of which keeps changing based on records retrieved with pair of values from 2 combo-boxes,
I need to add a checkbox column to my datagrid, which has no databinding with any column from my database table, i am using this code

public void RefreshDataGrid(string query)
        {
            Buisness_logic bl = new Buisness_logic();
            dataGridView1.DataSource = bl.GetDataTable(query);
            SetUpDataGridView();
            dataGridView1.ClearSelection();
        }

        public void SetUpDataGridView()
        {

            DataGridViewCellStyle style = dataGridView1.ColumnHeadersDefaultCellStyle;
            style.BackColor = Color.White;
            style.ForeColor = Color.Black;
            dataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;
            dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
            dataGridView1.RowHeadersVisible = false;
            dataGridView1.Columns[0].HeaderText = "Sr.No";
            dataGridView1.Columns[0].Width = 50;
            dataGridView1.Columns[1].HeaderText = "Rate";
            dataGridView1.Columns[1].Width = 70;
            dataGridView1.Columns[2].HeaderText = "Amount";
            dataGridView1.Columns[2].Width = 100;
            dataGridView1.Columns[3].HeaderText = "Mode";
            dataGridView1.Columns[3].Width = 60;
            dataGridView1.Columns[4].HeaderText = "Support";
            dataGridView1.Columns[4].Width = 80;
            dataGridView1.Columns[5].HeaderText = "Team1";
            dataGridView1.Columns[5].Width = 100;
            dataGridView1.Columns[6].HeaderText = "Team2";
            dataGridView1.Columns[6].Width = 100;
            dataGridView1.Columns[7].HeaderText = "Team3";
            dataGridView1.Columns[7].Width = 100;
                DataGridViewCheckBoxColumn column3 = new DataGridViewCheckBoxColumn();
                column3.Name = "Column3";
                column3.HeaderText = "IsCheck";
                column3.ReadOnly = false;
                dataGridView1.Columns.Add(column3);
        }

It datagridview is good when tge form loads the first time, but when i change the value of the combo-boxes and the datasource changes, the columns get messed up, and a no. of checkbox columns get added and that ruins my form,

here is the code i used to retrieve the records from table

Combo_pair pr1 = combo_match_code.SelectedItem as Combo_pair;
                int match_code_f1 = Convert.ToInt32(pr1.Text);
                Combo_pair pair = combo_name.SelectedItem as Combo_pair;
                int userid_f1 = Convert.ToInt32(pair.Value);
                string query = "Select int_sr_no,double_rate,double_amount,txt_mode,txt_support,double_team1,double_team2,double_team3 from match_transaction where int_match_code='" + match_code_f1 + "' AND int_user_id='" + userid_f1 + "' AND is_deleted=0";
                RefreshDataGrid(query);

this is the image when the form loads for the first time

enter image description here

and this is the image after i change the combo box values a few times

enter image description here

*(sorry, having trouble with images)
i really need some help with these, thanxx

  • 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-07T13:00:58+00:00Added an answer on June 7, 2026 at 1:00 pm

    Before the answer I’m just going to restate the problem in steps (so that my solution hopefully makes more sense):

    • You have a databound DataGridView where you want to change details of columns
    • When you want refresh the data in the grid (your search criteria change) your changes to the columns are overwritten
    • To fix this you update date grid datasource and remake all the changes
    • This has the side effect that an unbound checkbox column you add is being added multiple times

    So the problem in a nutshell is how to keep the changes to columns while also only having the one column?

    The trick here is the DataGridView’s AutoGenerateColumns property.

    For sake of argument let is say that you first setup your grid during the form load – if not then you might need a boolean field to store if you have previously setup the grid.

    public Form1()
    {
        InitializeComponent();
    
        // So this is the first time we call your refresh grid with some default string query
        RefreshDataGrid(query);
    
        // Now one time only we call SetUpDataGridView()
        SetUpDataGridView();
    
        // Now once we have done that we set AutoGenerateColumns to false
        dataGridView1.AutoGenerateColumns = false;
    
    }
    
    public void RefreshDataGrid(string query)
    {
        Buisness_logic bl = new Buisness_logic();
        dataGridView1.DataSource = bl.GetDataTable(query);
        // We no longer need to call SetUpDataGridView()
        // SetUpDataGridView();
        dataGridView1.ClearSelection();
    }
    

    Your SetUpDataGridView() method is pretty much identical:

    public void SetUpDataGridView()
    {
    
        DataGridViewCellStyle style = dataGridView1.ColumnHeadersDefaultCellStyle;
        style.BackColor = Color.White;
        style.ForeColor = Color.Black;
        dataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;
        dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
        dataGridView1.RowHeadersVisible = false;
        dataGridView1.Columns[0].HeaderText = "Sr.No";
        dataGridView1.Columns[0].Width = 50;
    
        // And so on for all the columns...
    
        dataGridView1.Columns[7].Width = 100;
        DataGridViewCheckBoxColumn column3 = new DataGridViewCheckBoxColumn();
        column3.Name = "Column3";
        column3.HeaderText = "IsCheck";
        column3.ReadOnly = false;
        dataGridView1.Columns.Add(column3);
    }
    

    So now when the combboxes containing the query information change you can call RefreshDataGrid() but it will only update the data, not change your custom settings.


    One suggestion for the RefreshDataGrid() method is to add a line like so:

    public void RefreshDataGrid(string query)
    {
        Buisness_logic bl = new Buisness_logic();
        // This is the new line
        dataGridView1.DataSource = typeof(List<>);
        dataGridView1.DataSource = bl.GetDataTable(query);
        // We no longer need to call SetUpDataGridView()
        // SetUpDataGridView();
        dataGridView1.ClearSelection();
    }
    

    This can reduce flicker as column widths change and also is often necessary to make changing the DataSource show at all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have next code which creates a print page with data from datagridview. Font
I have next code which creates a print page with data from datagridview. Font
I have datagridview with paging enabled. But when I want to move from page
I have datagridview which fills data from database, there are columns where I have
I have datagridview and textboxes on form. I load records from different tables when
How to move to next page in datagridview.I have n number of pages.I enabled
I have DataGridView and I set DataSource of datagridview by using DataTables. DataTable dt
I have a datagridview, and it loads information from db. However, at run time,
I have a datagridview which is editable. I have a textbox below it. I
I have a datagridview bound to a datasource, column[0] AgencyName is a primary key

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.