I might be doing something wrong but...
I have a datagridview. Its DataSource is BindingSource which has a table as a DataSource.
I created a combobox in the datagridview and filled it using:
col.Name = "employee_comb";
col.DisplayMember = "work";
col.ValueMember = "work_id";
col.DataPropertyName = "work";
col.DataSource = bs;
dataGridView1.Columns.Insert(3, col);
work contains string and work_id contains int.
I also have CurrentCellDirtyStateChanged event to check for changes.
The problem is: when I select another item in the combobox list, the datagridview automatically changes its CurrentRow to the CurrentRow with an "id" (first column) equal to ValueMember of combobox item. How do I stop it from switching?
It seems the problem occurred because datagridview and datagridviewcombobox had the same DataSource. I created new BindingSource and it works like charm!
Related
I have a class derived from DataSet that I'm using as a datasource. My DataGridView has 8 columns of which 3 are multiple value choices that are edited with drop down combo boxes.
If I let the Datasource generate the columns for the DataGridView, then all of the column types are DataGridViewTextBoxColumn. However the DataGridView is populated with my original row values.
If I set AutoGenerateColumns to false and add my own column types to the DataGridView I can add DataGridViewComboBoxColumn for the 3 columns that require it. However, the DataSource, while it contains the row data, does not populate the rows of my DataGridView.
Obviously I want both things to happen. The rows need to be populated from the DataSource and the column need to be correctly editable from the DataGridView as DataGridViewComboBoxColumns. Why can't I have both? What am I doing wrong.
Please note this is not a question about using a DataSource to populate the items in a ComboBox.
code fragment
alarmDataSet = new AlarmDataSet(sAlarms);
gridView.AutoGenerateColumns = false;
gridView.DataSource = alarmDataSet;
gridView.DataMember = sAlarms;
another code fragment
cm = new DataGridViewComboBoxColumn();
cm.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
cm.HeaderText = ci.Name;
cm.Name = ci.DataName;
cm.MaxDropDownItems = 3;
cm.Items.Add("Once");
cm.Items.Add("Repeat");
cm.Items.Add("Off");
gridView.Columns.Add(cm);
Thanks
if you set
DataGridView.AutoGenerateColumns = false
Then You need to tell DataGeridView about which column of DataGridView bind to which column of DataSource (Property Name in case of list)
DataGridView.Column[0].DataPropertyName = "Column Name In Your DataSource That is bound to DataGridView Column 0"
Background
I'm trying to set a column on my DGV up as a combobox. I've added all the columns to the gridview in the designer so all that is left is to bind them to the dataset.
The datatype of the Status column is varchar.
Question
However i keep getting a very unhelpful error message at run time. Am i doing something wrong?
DataGridViewComboBox Value is not valid.
The above error happens when setting the datasource of the dgv.
dataGridView1.DataSource = JoblistDataSet.Tables["Joblist"];
My Code
DataGridViewComboBoxColumn Column = (DataGridViewComboBoxColumn)dataGridView1.Columns["Status"];
Column.DataPropertyName = "Status";
DataGridViewComboBoxCell cbCell = (DataGridViewComboBoxCell)dataGridView1.Rows[0].Cells["Status"];
cbCell.Items.Add("New");
cbCell.Items.Add("Hold");
cbCell.Items.Add("Remove");
dataGridView1.DataSource = JoblistDataSet.Tables["Joblist"];
I think the problem is that you are populating DataGridViewComboBoxCell.Items for row index 0 instead of DataGridViewComboBoxColumn.Items which applies for all rows (hope you noticed Cell vs Column).
Use something like this instead
var statusColumn = (DataGridViewComboBoxColumn)dataGridView1.Columns["Status"];
statusColumn.DataPropertyName = "Status";
statusColumn.Items.Add("New");
statusColumn.Items.Add("Hold");
statusColumn.Items.Add("Remove");
// ...
I'm currently working with C# and I have the following code to set a ComboBox column inside a DGV:
// Add the values of the combo box
dgvcbGeneric.DataSource = dtDataSource;
dgvcbGeneric.ValueMember = "ID";
dgvcbGeneric.DisplayMember = "Value";
dgvcbGeneric.DataPropertyName = strColumn;
dgvcbGeneric.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
dgvcbGeneric.DisplayStyleForCurrentCellOnly = true;
// vGenericCMB.SortMode = DataGridViewColumnSortMode.Automatic
dgvcbGeneric.DefaultCellStyle.NullValue = columns[strActualColumn].nullFormat;
// Add the new ComboBoxColumn of the DGV
dgvLink.Columns.Add(dgvcbGeneric);
All works fine, but the DGV only show the member value when the cell has the focus:
Someone can explain me why this issue is happening?
I found the error by myself. Basically in the DataSource of the DGV the column "Day" was of type "Byte" (tinyint in SQL).
When the code try to create the DataTable to fill up the DGV ComboBox Column; the ID type is "Integer".
The solution was check the DataType of the ID vs the column source. If are differents; throw an exception to check previously.
I have added a lookup combobox to my datagridview. Any changed to an existing row or adding a new row changed the value in RowState on save to Modified or Added. except changing a value in the combobox. On save, the RowState remains as unmodified.
the code i use to add the combobox is.
DataGridViewComboBoxColumn cbQualification = new DataGridViewComboBoxColumn();
cbQualification.HeaderText = "Course Code";
DataSet myDataSet = GetData.GetCoursesData();
cbQualification.DataSource = myDataSet.Tables[0];
cbQualification.DisplayMember = "Code";
cbQualification.ValueMember = "ID";
cbQualification.DataPropertyName = "QualID";
grdPersonQuals.Columns.Insert (1,cbQualification);
the save event uses the code.
grdPersonQuals.BindingContext[grdPersonQuals.DataSource, grdPersonQuals.DataMember].EndCurrentEdit();
foreach (DataRow row in dsPersonQuals.Tables[0].Rows)
{
object x = row.RowState;
}
I'm guessing the focus is still in your combobox column when hitting your save button? I've always called the DataGridView's EndEdit method to trigger updating the datasource.
So in your save button event
grdPersonQuals.EndEdit();
You are calling it on the binding context, but I believe you need to call it on the grid itself so it pushes the changes in the grid down to it's datasource.
You may set rowstate if it is unmodified
foreach (DataRow row in dsPersonQuals.Tables[0].Rows)
{
row.SetAdded(); // or row.SetModified();
object x = row.RowState;
}
Scenario
I have a DevExpress DataGrid which is bound to a DataSet in C#.
I want to populate each dataset row to contain a string in the first column and a checkbox in the second. My code below doesn't work quite how I want it to, and I'm not sure why.....
The Code
As you can see I've declared a dataset, but when I try and pass in a new checkbox object to the 2nd column it just displays the system name for the checkbox.
DataSet prodTypeDS = new Dataset();
DataTable prodTypeDT = prodTypeDS.Tables.Add();
prodTypeDT.Columns.Add("MurexID", typeof(string));
prodTypeDT.Columns.Add("Haganise",typeof(CheckBox));
//WHY DOES THIS NOT WORK?
//(Displays "System.Windows.Forms.CheckBox, CheckState: 0")
//Instead of a checkbox.
CheckBox c = new CheckBox();
prodTypeDS.Tables[0].Rows.Add("Test",c);
//This doesn't work either....
prodTypeDS.Tables[0].Rows.Add("Test",c.CheckState);
......I hope this is just because it's a DevExpress datagrid....
Why do you use a Checkbox column in your DataSet ?
You should try adding a bool column in your DataSet, when binding the DataSet to the grid, it will automatically use a Checkbox to display the item value.
When you dont have a bool column in your datatable you can also manually/in code set the editor for the column.
Example in code:
Say you add a devex grid column called "fCol".
The value for checked = "YES", unchecked = "NONO";
GridColumn fCol = gridView1.Columns.Add();
RepositoryItemCheckEdit fCheckEdit = new RepositoryItemCheckEdit();
fCheckEdit.ValueChecked = "YES";
fCheckEdit.ValueUnchecked = "NONO";
fCol.ColumnEdit = fCheckEdit;
fCol.FieldName = "Haganise";
Of course you can also do this in the designer.