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
and this is the image after i change the combo box values a few times
*(sorry, having trouble with images) i really need some help with these, thanxx
Before the answer I'm just going to restate the problem in steps (so that my solution hopefully makes more sense):
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.