DataGridViewComboBoxCell Value Not Updating When Set Programmatically C# Winforms - winforms

I've read all the similiar posts about this darn control (DataGridViewComboBoxCell) and setting it's value programmatically but implementing all the suggestions hasn't worked. I could probably change the UI to get around this problem but I don't like to be beat!
private void PopulateAreaForRoleAssociation()
{
// If businessRoleList is null then no data has been bound to the dgv so return
if (businessRoleList == null)
return;
// Ensure businessArea repository is instantiated
if (businessAreaRepository == null)
businessAreaRepository = new BusinessAreaRespository();
// Get a reference to the combobox column of the dgv
DataGridViewComboBoxColumn comboBoxBusinessAreaColumn = (DataGridViewComboBoxColumn)dgvBusinessRole.Columns["BusinessArea"];
// Set Datasource properties to fill combobox
comboBoxBusinessAreaColumn.DisplayMember = "Name";
comboBoxBusinessAreaColumn.ValueMember = "Id";
comboBoxBusinessAreaColumn.ValueType = typeof(Guid);
// Fill combobox with businessarea objects from list out of repository
comboBoxBusinessAreaColumn.DataSource = businessAreaRepository.GetAll();
// loop through the businessRoles list which the dgv is bound to and get out each dgv row based upon the current id in the loop
businessRoleList.Cast<BusinessRole>().ToList().ForEach(delegate(BusinessRole currentRole)
{
DataGridViewRow currentRowForRole = dgvBusinessRole.Rows.Cast<DataGridViewRow>().ToList().Find(row => ((BusinessRole)row.DataBoundItem).Id == currentRole.Id);
// Get a reference to the comboBox cell in the current row
DataGridViewComboBoxCell comboBoxCell = (DataGridViewComboBoxCell)currentRowForRole.Cells[2];
// Not sure if this is necessary since these properties should be inherited from the combobox column properties
comboBoxCell.DisplayMember = "Name";
comboBoxCell.ValueMember = "Id";
comboBoxCell.ValueType = typeof(Guid);
// Get the business area for the current business role
BusinessArea currentAreaForRole = businessAreaRepository.FetchByRoleId(currentRole.Id);
// if the role has an associated area then set the value of the cell to be the appropriate item in the combobox
// and update the cell value
if (currentAreaForRole != null)
{
foreach (BusinessArea area in comboBoxCell.Items)
{
if (currentAreaForRole.Id == area.Id)
{
comboBoxCell.Value = area.Id;
dgvBusinessRole.UpdateCellValue(2, comboBoxCell.RowIndex);
}
}
}
});
}
The dgv is first bound to a binding list holding BusinessRole objects, then the combobox column is bound to a basic list of BusinessArea objects that come out of a repository class. I then loop through the bindinglist and pull out the row of the dgv that is bound to the current item in the bindinglist loop.
With that row I make a database call to see if the BusinessRole entity is associated with a BusinessArea entity. If it is then I want to select the item in the combobox column that holds the BusinessAreas.
The problem is that when the grid is loaded, all the data is there and the comboboxes are populated with a list of available areas, however any values that are set are not displayed. The code that sets the value is definately getting hit and the value I am setting definately exists in the list.
There are no data errors, nothing. It's just refusing to update the UI with the value I programmatically set.
Any help would be greatly appreciated as always.
Thanks

This code works for my first combo box I have in my row with the following code, but I try it with the next one in the row and it doesn't work. I could use the help with the rest I have 3 more to do. I do set the combo boxes on a second form from this form with the same code as is on the last line of the try block but using the cell information instead of the dataset
try
{
string strQuery = "select fundCode from vwDefaultItems where IncomeType = '" + stIncome + "'";
SqlDataAdapter daDefault = new SqlDataAdapter(strQuery, conn);
DataSet dsDefault = new DataSet();
daDefault.Fill(dsDefault);
strDefFund = dsDefault.Tables[0].Rows[0].ItemArray[0].ToString();
dgvCheckEntry.Rows[curRow].Cells[7].Value = dsDefault.Tables[0].Rows[0].ItemArray[0].ToString();
}
catch (Exception eq)
{
}

Related

Immediately update DataGridViewCheckBoxCell

I am programmatically updating my WinForm DataGridView
Problem, DataGridViewCheckBoxCell doesn't get updated !!!
I was google, it seams like knowing case but whatever I've tried did not help yet.
private void InitializeFunctionsDataGrid()
{
System.Data.DataSet ds = func.GetFunctions();
this.FunctionsDataGrid.DataSource = ds.Tables[0];
this.FunctionsDataGrid.Columns["FunctionId"].Visible = false;
this.FunctionsDataGrid.Columns["DESCRIPTION"].Width = 370;
DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
column.Name = "enable";
column.HeaderText = "enable";
column.FalseValue = 0;
column.TrueValue = 1;
FunctionsDataGrid.Columns.Add(column);
foreach(DataGridViewRow row in FunctionsDataGrid.Rows)
{
(( DataGridViewCheckBoxCell)row.Cells["enable"]).Value = 1;
}
FunctionsDataGrid.CurrentCell = null;
}
enable is an unbound column. This means that you need to provide cell value yourself.
You can set the VirtualMode property to true and handle the CellValueNeeded event.
If you want to enable the user to check a cell then you need to handle the CellValuePushed event.
DataGridView samples that are part of the DataGridView FAQ has a specific example of an unbound checkbox column along with databound columns.
OK basically easiest way for me was to work with datasource.
I've add the column to the DataTable and fill it with data.
And then last thing this.FunctionsDataGrid.DataSource = ds.Tables[0];

show blank in combobox bind to data base

i am binding my combobox to the database and i am able to populate the combobox from my database.But when i am opening the page the combobox contains one value by default from the database. I want to show blank in my combobox, How can do that?
my code:
datatable dt;
DataAccess.Connect();
dt = DataAccess.Select("select * from CompanyMainActivity");
cmbMainActivity.DisplayMember = ("name");
cmbMainActivity.ValueMember = "MainActivityCode";
cmbMainActivity.DataSource = dt;
I try this code, but in dosent work
cmbMainActivity.Items.Insert(0, "select name from CompanyMainActivity");
You can add a blank value at the first.
dt = DataAccess.Select("select name, MainActivityCode from CompanyMainActivity");
//Insert default row
DataRow dr = dt.NewRow();
dr["name"] = String.Empty;
dr["MainActivityCode"] = -1; //I have set -1 because it should not be duplicate and this record is fake. so, we can determine that the record is fake from the -1 value.
dt.Rows.InsertAt(dr, 0); //Insert the blank record at the top of the all records.
cmbMainActivity.DisplayMember = ("name");
cmbMainActivity.ValueMember = "MainActivityCode";
cmbMainActivity.DataSource = dt;
I guess what u r looking for are called place holders.Please refer these few links which might help.
[Combo box's different properties][1](http://www.dotnetperls.com/combobox)
[If u r using wpf controls][2](http://pwlodek.blogspot.in/2009/11/watermark-effect-for-wpfs-textbox.html)
[check this also][3](http://msdn.microsoft.com/en-us/library/windows/apps/bg182878.aspx)
Assuming you are using WPF
On the combobox's section on your xaml file add a SelectedIndex
<ComboBox>
...
SelectedIndex = -1
...
</ComboBox>

problems setting XtraGrid with in-place editor-s (using GridLookUpEdit)

I need an XtraGrid control that has GridLookupEdit to enter column values.
What I managed to do so far:
1) I have configured XtraGrid control
columns: ID, Name, Number
in-place GridLookUp Editors for Name and Number columns
2) on the form Load event I load data from database and set XtraGrid datasource and both repositoryItem(..) datasource, valuemember and displaymember
a) Data are loaded in XtraGrid, I can activate cell but can not choose values from dropdown (from repositoryItem(..)) or enter values manually (i can activate cell in "*" new row but it has the same problem - can not choose from the dropdown or enter values manually) => why?
b) If I don't set datasource for XtraGrid, then both dropdown's are active and i can select values using GridLookupEdit, but when i exit the cell, the cell gets cleared => i believe this is because in this case no xtraGrid.DataSource is set?
var model = (from TableA a in _dbE.TableA select new {ID, Name, Number}).ToList();
//if i comment this line out, then i can choose values from GridLookUpEdit
gridControl1.DataSource = model;
repositoryItemNosaukums.DataSource = model;
repositoryItemNosaukums.ValueMember = "ID";
repositoryItemNosaukums.DisplayMember = "Name";
repositoryItemPieteikumaNr.DataSource = model;
repositoryItemPieteikumaNr.ValueMember = "ID";
repositoryItemPieteikumaNr.DisplayMember = "Number";
What am I missing?
The problem was, that my data source "model" was a List, but it should have been BindingList with properties AllowNew==True and AllowEdit==True.
After DevExpress support asked if the data source is read only, i found this post which finally opened my eyes:
A Problem in DataGridView : datagridview seems readonly to user (WinForms)
correct version of code
//no projections (select new ...) for IBindingList<T>..
//var model = (from TableA a in _dbE.TableA select new {ID, Name, Number}).ToList();
var model = (from TableA a in _dbE.TableA).ToList();
//convert to BindingList
var bindingModel= new BindingList<TableA>(model);
bindingModel.AllowNew=true;
bindingModel.AllowEdit=true;
//bind BindingList to datagrid
gridControl1.DataSource = bindingModel; //model;
//... no changes to repositoryItem(..) stuff
repositoryItemNosaukums.DataSource = model;
repositoryItemNosaukums.ValueMember = "ID";
repositoryItemNosaukums.DisplayMember = "Name";
repositoryItemPieteikumaNr.DataSource = model;
repositoryItemPieteikumaNr.ValueMember = "ID";
repositoryItemPieteikumaNr.DisplayMember = "Number";

Edits to dynamic DataTable not updating in DataGrid

I have a DataTable and when I alter (add,modifiy..ect) it wont reflect on the DataGrid which is bound to the DataTable.
ItemsSource="{Binding TableData,Mode=TwoWay,IsAsync=True}"
Thats the binding ^
Now when I set the RowError
TableData.Rows[x].RowError = ex.Message;
HasError gets set to true... but the DataGrid does not reflect this ( I have a style that marks a row red when there is an error)
Note: My changes are not being reflected on more then setting ErrorMessages, I've also tried adding rows in the ViewModel and those added rows are not relfected either.
About the DataTable:
It has no set columns or anything, it relfects a selected Database table which the user picks.
Binding to a DataTable won't work. You need to first convert your DataTable to an ObservableCollection<>. From here:
public void ConvertDataTable( DataTable dt )
{
DataList = new ObservableCollection<ProjectWorkHours>();
//Scan and arrange data into ObservableCollection
int UserID = 0;
if ( dt.Rows.Count >0 )
{
UserID = int.Parse( dt.Rows[0]["UserID"].ToString() );
//Distill project id list
List<int> ProjectIDList = GetProjectIDList( dt );
for ( int i = 0 ; i < ProjectIDList.Count; i ++ )
{
int ProjectID= ProjectIDList[i];
//Get WorkRecord
int[] MyWorkRecord = GetWorkRecord(dt, ProjectID);
ProjectWorkHours newProjectWorkHours = new ProjectWorkHours(UserID,ProjectID,MyWorkRecord);
DataList.Add( newProjectWorkHours);
}
}
}
The link has a more full example of working with a database and using binding.
What Type is TableData?
If it is a List<> then changing the items in the list will NOT update the binding
If it is an ObservableCollection<> then changing the items in the collection will update the binding

DataSet created in code not Binding to ListView

I have a WPF User Control with a ListView in it that is created based on the DataSet that is passed to it:
public void PopulateList(DataSet ds) {
listView.View = CreateGridViewColumns(ds.Tables[0]);
listData.DataContext = ds.Tables[0];
}
private GridView CreateGridViewColumns(DataTable dt) {
// Create the GridView
var gv = new GridView {AllowsColumnReorder = true};
// Create the GridView Columns
foreach (DataColumn item in dt.Columns) {
var gvc = new GridViewColumn
{
DisplayMemberBinding = new Binding(item.ColumnName),
Header = item.ColumnName,
Width = Double.NaN
};
gv.Columns.Add(gvc);
}
return gv;
}
Now I create the user control in code and call it's PopulateList with the appropriate dataset and this is where the problems are starting:
If I pass in a dataset that was created from a call to the database the list view shows all the data but if i pass in a DataSet that i created in code the ListView shows the Columns but will not show the data
//This is a function that hides the DB call return type is DataSet
var dsPatientSmokingStatusHistory = DataRepository.PatientSmokingStatusProvider.GetHistory(PatientId);
//radGridViewPatientSmokingStatus.DataSource = dsPatientSmokingStatusHistory.Tables[0];
var dt = new DataTable();
string c1 = "Date".PadLeft(23).PadRight(23);
string c2 = "Status".PadLeft(20).PadRight(50);
dt.Columns.Add(c1);
dt.Columns.Add(c2);
int i = 0;
foreach (DataRow row in dsPatientSmokingStatusHistory.Tables[0].Rows) {
var dataRow = dt.NewRow();
dataRow[c1] = ((DateTime)row["Date"]).ToString("MM/dd/yyyy");
dataRow[c2] = row["Status"].ToString();
dt.Rows.Add(dataRow);
dt.Rows[i].AcceptChanges();
i++;
}
DataSet ds = new DataSet();
dt.TableName = "Table";
ds.Tables.Add(dt);
ds.AcceptChanges();
smokingStatusGrid.GridWidth = 455;
smokingStatusGrid.GridHight = 97;
//This line does not show data
smokingStatusGrid.PopulateGrid(ds);
//This line will show data
smokingStatusGrid.PopulateGrid(dsPatientSmokingStatusHistory);
Is there a difference between these two datasets that i don't know about that is preventing me from databinding to it?
Also the user control is being used as an ElementHost in a WinForms application (not sure if this makes a difference)
Your code says:
DisplayMemberBinding = new Binding(item.ColumnName)
This binding constructor takes a string paramter which as per MSDN is "The initial Path for the binding" and is of datatype System.Windows.PropertyPath. I guess, since system tries to find a property with the same name in your class, and your string (item.ColumnName) has spaces at start, it runs into a problem (properties can't start with a space).
Would recommend you to take off the padding that you are doing in column name of your table. Apply any padding/margins in the Header of your GridView.

Resources