Create a Details Form from a GridView c# - c#

Possibly a complex question - but here's hoping
I have a generic data grid on a single form (displays whatever table I want from a dataset) simply by swapping tables in the dataset.
I want to double-click on a given record and display a single-record view of the table data with the currently selected record displayed as default, but with the option to page through and edit/view/delete other records i.e.
I want to automatically create a details view form from a datagrid for a given table at runtime. The form should be dynamically created - displaying the dataset in details view with the option to page through single records using the binding source/binding source navigator.
My goal is to improve efficiency/maintainability of the application - rather than create and manage 10+ forms, I simply want to create and manage I generic details form in the same way as I manage I generic gridview form.
So far I have come up with:
public void CreateDetailsForm(BindingSource bs, int rowClicked)
{
Form detailsForm = new Form();
BindingSource newFormBS = new BindingSource();
//assign binding source for use on new detailsForm
newFormBS = bs;
//assign binding source to datatable
DataTable dt = (DataTable)bs.DataSource;
//create the form fields
CreateFormFields(dt); //not yet implemented
//assign form fields to form
//display form
}
Any help on the following appreciated
Generating and assigning the form fields to the form.
Thanks in advance.

it likes:
Form f=new Form();
TextBox t=new TextBox();//generate the controls u need
t.Text = "test";//set the actual value
t.Location=new Point(10,10);
f.Controls.Add(t);
DialogResult dr=f.ShowDialog();

So far I have got the col names generated on the form as follows
List colNames = GetColumnNames(dt);
int offset=25;
int xPos=50;
int yPos = 10;
foreach (string name in colNames)
{
Label l = new Label();
l.Name = name;
l.Width = 200;
l.Text = name;
TextBox t = new TextBox();
t.Name = name;
t.Width=200;
l.Location = new Point(xPos, yPos );
t.Location = new Point(xPos+250, yPos);
f.Controls.Add(l);
f.Controls.Add(t);
yPos = yPos + offset;
}
//TextBox t = new TextBox();//generate the controls u need
//t.Text = "test";//set the actual value
f.Width = 800;
f.Height = 600;
f.Show();
}
private List<string> GetColumnNames(DataTable table)
{
List<string> lstColNames=new List<string>();
if (table != null)
{
lstColNames=
(from DataColumn col in table.Columns
select col.ColumnName).ToList();
}
return lstColNames;
}
Now trying to work on getting the controls to bind to the binding source!

OK - got it working now - had to take a different approach
Created a single detailsView Form
Created a static class called PassBindingSource with a static property bst for passing binding source from gridview to details form
static class PassBindingSource
{
public static BindingSource bst { get; set; }
}
On the detailsView form added the following code
try{ bs.DataSource = PassBindingSource.bst;
DataTable dt = (DataTable)PassBindingSource.bst.DataSource;
List<string> colNames = GetColumnNames(dt);
int offset = 25;
int xPos = 50;
int yPos = 50;
foreach (string name in colNames)
{
Label l = new Label();
l.Name = name;
l.Width = 200;
l.Text = name;
TextBox t = new TextBox();
t.Name = name;
t.Width = 200;
// BindingOperations.SetBinding(t, t.TextProperty, bs);
//binding operation
t.DataBindings.Add(new Binding("Text", bs, t.Name, true));
l.Location = new Point(xPos, yPos);
t.Location = new Point(xPos + 250, yPos);
this.Controls.Add(l);
this.Controls.Add(t);
yPos = yPos + offset;
// dgDetailsView.DataSource = bs;
}
//move to correct record in binding source
bs.Position = PassBindingSource.currentRecord;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private List<string> GetColumnNames(DataTable table)
{
List<string> lstColNames=new List<string>();
if (table != null)
{
lstColNames=
(from DataColumn col in table.Columns
select col.ColumnName).ToList();
}
return lstColNames;
}
SUMMARY
Now it all works - the detailsView controls generated at run-time wired up correctly to the binding source and the datagrid can call this detailsView any time using any table from the dataset by wiring the double-click event of the gridview with the following code
PassBindingSource.bst= bs;
frmDetailsView nf = new frmDetailsView();
nf.Show();
Hope this helps others. Many thanks to User2354374 for the initial steer.

Related

ComboBox added programmatically to DataGridView cell not expanding on cell click

I have a DataGridView in a C# WinForms project in which, when the user clicks on certain DGV cells, the cell changes to a DataGridViewComboBoxCell and the ComboBox is populated with some values for the user to select. Here's the form code for the DataGridView_Click event:
private void dgvCategories_Click(Object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 5 && !(dgvCategories.Rows[e.RowIndex].Cells[e.ColumnIndex].GetType().Name == "DataGridViewComboBoxCell"))
{
// Bind combobox to dgv and than bind new values datasource to combobox
DataGridViewComboBoxCell cboNewValueList = new DataGridViewComboBoxCell();
// Get fields to build New Value query
List<string> lsNewValuesResult = new List<string>();
string strCategory = dtCategories.Rows[e.RowIndex][1].ToString();
string strCompanyName = cboSelectCompany.Text;
string strQueryGetNewValuesValidationInfo = "SELECT validationdb, validationtable, validationfield, validationfield2, validationvalue2" +
" FROM masterfiles.categories" +
" WHERE category = #category";
//" WHERE category = '" + strCategory + "'";
// Pass validation info query to db and return list of New Values
db getListOfNewValues = new db();
lsNewValuesResult = getListOfNewValues.GetNewValuesList(strQueryGetNewValuesValidationInfo, strCategory, strCompanyName);
//Populate the combobox with the list of New Values
foreach (string strListItem in lsNewValuesResult)
{
cboNewValueList.Items.Add(strListItem);
}
//
dgvCategories[e.ColumnIndex, e.RowIndex] = cboNewValueList;
}
}
Here's the code in the db class that populates the ComboBox (this likely isn't necessary to include for the purposes of this question, but for the sake of completeness, I'm including it, in case is it relevant):
public List<string> GetNewValuesList(string strValidationInfoQuery, string strCategory, string strCompanyName)
{
List<string> lsValidationInfo = new List<string>();
List<string> lsNewValuesList = new List<string>();
using (NpgsqlConnection conn = new NpgsqlConnection(connString))
using (NpgsqlCommand cmd = new NpgsqlCommand(strValidationInfoQuery, conn))
{
cmd.Parameters.AddWithValue("category", strCategory);
conn.Open();
using (NpgsqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
int intReaderIndex;
for (intReaderIndex = 0; intReaderIndex <= reader.FieldCount - 1; intReaderIndex++)
{
// reader indexes 3 & 4 correspond to categories.validationfield2 and validationvalue2, which can be null
if (string.IsNullOrEmpty(reader[intReaderIndex].ToString()))
{
lsValidationInfo.Add("");
}
else
{
lsValidationInfo.Add(reader.GetString(intReaderIndex));
}
//Console.WriteLine("reader index " + intReaderIndex + ": " + reader.GetString(intReaderIndex));
}
}
}
}
string strValidationDb = lsValidationInfo[0];
string strValidationTable = lsValidationInfo[1];
string strValidationField = lsValidationInfo[2];
string strValidationField2 = lsValidationInfo[3];
string strValidationValue2 = lsValidationInfo[4];
string strQueryGetNewValues = "SELECT DISTINCT " + strValidationField +
" FROM " + strValidationDb + "." + strValidationTable +
" WHERE company_id = (SELECT id FROM company WHERE name = '" + strCompanyName + "')";
if (!string.IsNullOrEmpty(strValidationField2) && !string.IsNullOrEmpty(strValidationValue2)) strQueryGetNewValues += " AND " + strValidationField2 + " = '" + strValidationValue2 + "'";
strQueryGetNewValues += " ORDER BY " + strValidationField;
using (NpgsqlConnection conn = new NpgsqlConnection(connString))
using (NpgsqlCommand cmd = new NpgsqlCommand(strQueryGetNewValues, conn))
{
conn.Open();
using (NpgsqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
int intReaderIndex;
for (intReaderIndex = 0; intReaderIndex <= reader.FieldCount - 1; intReaderIndex++)
{
// reader indexes 3 & 4 correspond to categories.validationfield2 and validationvalue2, which can be null
if (string.IsNullOrEmpty(reader[intReaderIndex].ToString()))
{
lsNewValuesList.Add("");
}
else
{
lsNewValuesList.Add(reader.GetString(intReaderIndex));
}
Console.WriteLine("reader index " + intReaderIndex + ": " + reader.GetString(intReaderIndex));
}
}
}
}
return lsNewValuesList;
}
The combobox is getting populated, as I can access the items in lsNewValuesResult in the _Click method. The DGV Edit Mode is set to EditOnEnter. I tried EditOnKeystroke, but that didn't cause the combobox to expand on mouse click.
This is what the combobox looks like when the cell is clicked on and the CBO is populated and added to the DGV cell:
That's after I clicked each of the two cells.
[RESOLVED]
See my Answer below.
Unfortunately solving this revealed a new issue.
I'm about to publicly admit that I'm stupid:
For design and functionality reasons that are required for this project, I am manually setting the widths and names of the DGV's columns, and I also need the 2nd through 4th columns ReadOnly = true. Well, I inadvertently set the 5th column - the column that this question is about to ReadOnly = true as well.
Thank you all for your attempts at answering. This just serves to remind us how something so simple can cause a seemingly big issue and is so easy to overlook!
If I recognize your problem correctly, in my test app i add a DataGridView whit 6 column, EditMode = EditOnEnter
(Others need three time click to open dropdown, As far as I tried) and handle CellStateChanged envent.
private void dgvCategories_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
{
if (e.StateChanged == DataGridViewElementStates.Selected)
{
DataGridViewCell cell = e.Cell;
int columnIndex = cell.ColumnIndex;
int rowIndex = cell.RowIndex;
//---IF CONDITIONS--
//columnIndex == 5
// Only cells in Columns[5]
//cell.Selected
// Because this event raised two time, first for last selected cell and once again
// for currently selected cell and we need only currently selected cell.
//cell.EditType.Name != "DataGridViewComboBoxEditingControl"
// If this cell "CellStateChanged" raised for second time, only other cell types allowed
// to edit, otherwise the current cell lost last selected item.
if (columnIndex == 5 && cell.Selected && cell.EditType.Name != "DataGridViewComboBoxEditingControl")
{
DataGridViewComboBoxCell cboNewValueList = new DataGridViewComboBoxCell();
//Add items to DataGridViewComboBoxCell for test, replace it with yours.
for (int i = 0; i < 10; i++)
cboNewValueList.Items.Add($"Item {i}");
dgvCategories[columnIndex, rowIndex] = cboNewValueList;
}
}
}
NOTE: user must click two time in a cell to open drop down menu.
Edit One: As Reza Aghaei suggest for single click in cell:
private void dgvCategories_CellClick(object sender, DataGridViewCellEventArgs e)
{
DataGridViewComboBoxEditingControl editingControl = dgvCategories.EditingControl as DataGridViewComboBoxEditingControl;
if (editingControl != null)
editingControl.DroppedDown = true;
}
You might need to turn AutoGenerateColumns off:
Also, it seems to take a three clicks for the dropdown to pop.
public Form1()
{
InitializeComponent();
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = GetDataSource();
DataGridViewComboBoxColumn dgvcbc = new DataGridViewComboBoxColumn();
dgvcbc.Items.Add("R0C0");
dgvcbc.Items.Add("R1C0");
dgvcbc.Items.Add("R2C0");
dgvcbc.Items.Add("R3C0");
dgvcbc.DataPropertyName = "Col0";
dataGridView1.Columns.Add(dgvcbc);
}
DataTable GetDataSource()
{
var dtb = new DataTable();
dtb.Columns.Add("Col0", typeof(string));
dtb.Columns.Add("Col1", typeof(string));
dtb.Columns.Add("Col2", typeof(string));
dtb.Columns.Add("Col3", typeof(string));
dtb.Columns.Add("Col4", typeof(string));
dtb.Rows.Add("R0C0", "R0C1", "R0C2", "R0C3", "R0C4");
dtb.Rows.Add("R1C0", "R1C1", "R1C2", "R1C3", "R1C4");
dtb.Rows.Add("R2C0", "R2C1", "R2C2", "R2C3", "R2C4");
dtb.Rows.Add("R3C0", "R3C1", "R3C2", "R3C3", "R3C4");
return dtb;
}
Are you maybe getting an error which is not being shown for some reason?
If I use your code, DataGridViewComboBoxCell seems to be populated with values, but I get DataGridViewComboBoxCell value is not valid runtime error.
This test code is working fine for me:
private void dgvCategories_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
DataGridViewComboBoxCell cboNewValueList = new DataGridViewComboBoxCell();
List<string> lsNewValuesResult = new List<string>();
lsNewValuesResult.Add("Value1");
lsNewValuesResult.Add("Value2");
lsNewValuesResult.Add("Value3");
foreach (string strListItem in lsNewValuesResult)
{
cboNewValueList.Items.Add(strListItem);
}
dgvCategories[e.ColumnIndex, e.RowIndex] = cboNewValueList;
// Added setting of initial value
cboNewValueList.Value = cboNewValueList.Items[0];
}
So maybe try setting the initial value for your DataGridViewComboBoxCell after you add it to the DataGridView.
You can consider the following facts about DataGridView:
If you set AutoGenerateColumns to false, then you need to add columns to Columns collection manually.
If you set AutoGenerateColumns to true, when you assign the data to DataSource, the control generates columns automatically for the data source. In this case, the control looks in list of columns of the data source and for each column if there's no column in the Columns collection of the control having the same DataPropertyName as data source's column name, it will add a column to Columns collection.
DataPropertyName of the datagridviews' columns determines the bound column of the data source.
You usually want to add DataGridViewXXXXColumn to columns collection rather than using a DataGridViewXXXXCell for a cell.
If you set EditMode to EditOnEnter, then if you click on dropdown button, one click is enough. If you click on cell content, two clicks is needed.
If you would like to make it single click even if you click on cell content, take a look at this post. (Note: I haven't used this is the example, it's a bit annoying.)
you can set DisplayStyle to Nothing, then it shows the column as a combo box, just in edit mode.
A basic example on using DataGridViewComboBoxColumn
I suppose you are going to show a list of Products having (Id, Name, Price, CategoryId) in a DataGridView and the CategoryId should come from a list of Categories having (Id, Name) and you are going to show CategoryId as a ComboBox.
In fact it's a basic and classic example of DataGridViewComboBoxColumn:
private void Form1_Load(object sender, EventArgs e) {
var categories = GetCategories();
var products = GetProducts();
var idColumn = new DataGridViewTextBoxColumn() {
Name = "Id", HeaderText = "Id", DataPropertyName = "Id"
};
var nameColumn = new DataGridViewTextBoxColumn() {
Name = "Name", HeaderText = "Name", DataPropertyName = "Name"
};
var priceColumn = new DataGridViewTextBoxColumn() {
Name = "Price", HeaderText = "Price", DataPropertyName = "Price"
};
var categoryIdColumn = new DataGridViewComboBoxColumn() {
Name = "CategoryId", HeaderText = "Category Id", DataPropertyName = "CategoryId",
DataSource = categories, DisplayMember = "Name", ValueMember = "Id",
DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing
};
dataGridView1.Columns.AddRange(idColumn, nameColumn, priceColumn, categoryIdColumn);
dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = products;
}
public DataTable GetProducts() {
var products = new DataTable();
products.Columns.Add("Id", typeof(int));
products.Columns.Add("Name", typeof(string));
products.Columns.Add("Price", typeof(int));
products.Columns.Add("CategoryId", typeof(int));
products.Rows.Add(1, "Product 1", 100, 1);
products.Rows.Add(2, "Product 2", 200, 2);
return products;
}
public DataTable GetCategories() {
var categories = new DataTable();
categories.Columns.Add("Id", typeof(int));
categories.Columns.Add("Name", typeof(string));
categories.Rows.Add(1, "Category 1");
categories.Rows.Add(2, "Category 2");
return categories;
}
Learn more
To learn more about DataGridView, take a look at DataGridView Control (Windows Forms). It contains links to some documentations and useful How To articles, including:
DataGridView Control Overview
Basic Column, Row, and Cell Features in the Windows Forms DataGridView Control
Basic Formatting and Styling in the Windows Forms DataGridView Control
Column Types in the Windows Forms DataGridView Control

graph line (X,Y,Z) control

Hello I am searching for a graph that will allow me to make Z Line Graph control now I have X (Payment_Date) & Y (collected) line how to make Z (username) line
I want every username to have his line alone
Can someone help me?
I want to make the program on normal C# win form application
My Form
SqlDataReader reader = sqlcomm.ExecuteReader();
DataTable sd = new DataTable();
sd.Load(reader);
dataGridView1.DataSource = sd;
reader.Close();
chart1.Series["collected"].XValueMember = "Payment_Date";
chart1.Series["collected"].XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Date;
chart1.Series["collected"].YValueMembers = "collected";
chart1.Series["collected"].YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Int32;
chart1.DataSource = sd;
chart1.DataBind();
Step one: Create a separate BindingSource for each element you want to bind and set an appropriate Filter so it will only display the data of one user each.
Step two: Show and hide the Series you want to.
I have created a DataTable dt with 3 columns:
dt.Columns.Add("User", typeof(string));
dt.Columns.Add("Date", typeof(DateTime));
dt.Columns.Add("Value", typeof(double));
and filled it randomly. Next I pull out the distinct users:
var users = dt.Rows.Cast<DataRow>()
.Select(x => x.Field<string>("User"))
.Distinct()
.OrderBy(x => x)
.ToList();
Most likely you will reverse this and start with a list of users.
Now I create a Series with its own filtered BindingSource for each user:
for (int i = 0; i < users.Count; i++)
{
Series s = chart1.Series.Add(users[i]);
s.ChartType = SeriesChartType.Line;
BindingSource bs = new BindingSource();
bs.DataSource = dt;
bs.Filter = "User='" + users[i] + "'";
s.Points.DataBindXY(bs, "Date", bs, "Value");
}
Now I bind my DGV:
BindingSource bsdgv = new BindingSource();
bsdgv.DataSource = dt;
bsdgv.Filter = "";
dataGridView1.DataSource = bsdgv;
I have hidden the Series by making them Transparent. This way the names still show in the Legend. The trick is to keep the original colors in the Series' Tags..:
void hideAllSeries(Chart chart)
{
chart.ApplyPaletteColors();
foreach (Series s in chart.Series)
{
if (s.Color != Color.Transparent) s.Tag = s.Color;
s.Color = Color.Transparent;
}
}
To show or hide a Series I code the MouseClick event:
private void Chart1_MouseClick(object sender, MouseEventArgs e)
{
var hitt = chart1.HitTest(e.X, e.Y);
if (hitt.ChartElementType == ChartElementType.LegendItem)
{
Series s = hitt.Series;
if (s.Color == Color.Transparent)
{
s.Color = (Color)s.Tag;
}
else
{
s.Tag = s.Color;
s.Color = Color.Transparent;
}
}
}
Let's see it at work:
Instead of this solution you may want to add a User selection Checklist and set the DGV's BindingSource's Filter..

C# Windowsform updating dynamic generated combobox

I think I am overseeing something.
I dynamically generate a few ComboBoxes with this code (I do the same for other controls like TextBox, Label etc)
private ComboBox addControlsComboBox(string Id, string TBName, int point_X, int point_Y, int SizeWidth, DataTable DT)
{
ComboBox combobox = new ComboBox();
combobox.Text = TBName;
combobox.Location = new Point(point_X, point_Y);
combobox.Size = new Size(SizeWidth, 20);
combobox.Name = Id + TBName;
combobox.DataSource = DT;
combobox.DisplayMember = "key";
combobox.ValueMember = "value";
combobox.Enabled = true;
return combobox;
}
When I automatically want to set the selected value, for the controls all the values are set correct except for the ComboBox. Not 1 comboBox is updated but all the ComboBoxes.
I use a nested dictionary object to store all the values that i need to match.
See part of the used update Code
foreach (Control gb in GroupPanel.Controls)
{
foreach (Control childc in gb.Controls)
{
if (DataCollection[GroupNames].ContainsKey(childc.Name))
{
KeyName = childc.Name;
numberLessKeyName = SL.RemoveDigits(childc.Name);
TextValue = DataCollection[GroupNames][KeyName];
switch (NumberLessKeyName)
{
case "Name":
int IntTextValue = Convert.ToInt32(TextValue);
TextValue = IntTextValue.ToString("d2");
break;
}
switch (childc.GetType().ToString())
{
case "System.Windows.Forms.TextBox":
childc.Text = TextValue;
break;
case "System.Windows.Forms.ComboBox":
// Not Working
ComboBox combobox = (ComboBox)childc;
combobox.SelectedValue = TextValue;
//Also not Working
// --> childc.Text = TextValue;
break;
case "System.Windows.Forms.CheckBox":
CheckBox chChildc = (CheckBox)childc;
if (TextValue == "Yes")
{
chChildc.Checked = true;
}
break;
};
}
}
}
What I am doing wrong?
Can somebody help me please?
[EDIT 1]
Thanks to Karol
I added The Following Lines + interface ICloneable and it worked. Many Thanks.
DataTable DT = new DataTable();
DT = DTAttribute;
DataTable DTClone = (DataTable)DT.Clone();
For those searching [C# Object Clone Wars][1] link
[EDIT 2]
A other Idea is to use COPY (works also)
DataTable DT = new DataTable();
DT = DTAttribute;
DataTable DTClone = DT.Copy();
I think You bind all ComboBox same DataTable.
You must have diffrent instance of DataTable for each ComboBox. You can do this in two ways:
- Once again SELECT data from database.
- Use deep copy to make new instance of DataTable.

How do I add values to a DataGridViewComboBoxColumn DataSource?

I have an issue wherein I have a DataGridViewComboBoxColumn in a DataGridView and I want to add an item to the DataSource.
I've initially set the DataSource property to a List<string> which works fine. Later I'll add an item to this list, which works fine. But when I try to choose this item in a combobox, I get a data validation error,
System.ArgumentException: DataGridViewComboBoxCell value is not valid.
Further, I can't actually set the combobox to the newly added value.
Here is a fully working example.
public partial class Form1 : Form
{
List<string> Data { get; set; }
public Form1()
{
InitializeComponent();
// Populate our data source
this.Data = new List<string> { "Thing1", "Thing2" };
// Set up controls
var gvData = new System.Windows.Forms.DataGridView();
var col1 = new System.Windows.Forms.DataGridViewComboBoxColumn();
var button = new System.Windows.Forms.Button();
gvData.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { col1 });
// Set the column's DataSource
col1.DataSource = this.Data;
col1.HeaderText = "Test";
col1.Name = "col1";
// Set up a button which adds something to the source
button.Text = "Add";
button.Location = new System.Drawing.Point(0, 200);
button.Click += (e, s) => this.Data.Add("Thing3");
this.Controls.Add(gvData);
this.Controls.Add(button);
}
}
How can I add items to the DataSource for my DataGridViewComboBoxColumn?
Changing
button.Click += (e, s) => this.Data.Add("Thing3");
to
button.Click += (e, s) =>
{
col1.DataSource = null;
this.Data.Add("Thing3");
col1.DataSource = Data;
};
has worked for me.

I am having some trouble with adding checkbox column in a my datagridview

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):
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.
You need to setup the gridview only once.
A good place to do this is after InitializeComponent().
Since this is a fixed column, you could also add it through the designer (Edit Columns).
That way the code would get add to InitializeComponent().
InitializeComponent() is usually called in the constructor of your form.
At present you are adding this line
dataGridView1.Columns.Add(column3);
Will cause the checkbox column to be added several times.

Categories

Resources