show blank in combobox bind to data base - combobox

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>

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];

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";

DataGridViewComboBoxCell Value Not Updating When Set Programmatically C# 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)
{
}

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

Set DisplayMemberPath and SelectedValue in WPF with Code

I have a DataTable that is coming from a Web Service, which I need to bind to a ComboBox. I have not grokked doing binding in XAML yet so this question is about binding in code instead. So far I have tried
cboManager.DataContext = Slurp.DistrictManagerSelect().DefaultView;
cboManager.DisplayMemberPath = "Name";
cboManager.SelectedValuePath = "NameListId";
cboManager.SetBinding(ComboBox.ItemsSourceProperty, new Binding());
And I have tried
DataTable tbl = new DataTable();
tbl = Slurp.DistrictManagerSelect();
cboManager.ItemsSource = ((IListSource)tbl).GetList();
cboManager.DisplayMemberPath = "[Name]";
cboManager.SelectedValuePath = "[NameListId]";
DataContext = this;
In both cases I get the list of managers to show but when I select from the ComboBox I get [Name] and [NameListId] and not the values I am expecting. What am I doing wrong (other than not using XAML's DataBinding)?
Edit added after answers to my original post came in.
So (based on Rachel's response) try number three looks like this:
using (DataTable tbl = Slurp.DistrictManagerSelect())
{
List<ManagerList> list = new List<ManagerList>();
foreach (var row in tbl.Rows)
{
list.Add(new ManagerList
{
NameListId = (int)row[0],
Name = row[1].ToString()
});
}
}
Assuming I am doing what she meant the correct way I am no getting this error Cannot apply indexing with [] to an expression of type 'object'
Have you tried to just bind to the DataTable directly? Do you have columns Name and NameListId? Leave off the DataContext (you already assigned the ItemsSource).
DataTable tbl = Slurp.DistrictManagerSelect();
cboManager.ItemsSource = tbl;
cboManager.DisplayMemberPath = "Name";
cboManager.SelectedValuePath = "NameListId";
When you cast to the IListSource I suspect it is combining all the columns. If you want to bind to a list then you need to create items that have properties Name and NameListID.
I think that's because your ItemsSource is a DataTable, so each ComboBoxItem contains a DataContext of a DataRow, and the DataRow class doesn't have properties called Name or NameListId
Basically, you're trying to tell the ComboBox to display DataRow.Name, and set the value to DataRow.NameListId, both of which are not valid properties.
I usually prefer to parse data into objects, and bind the ItemsSource a List<MyObject> or ObservableCollection<MyObject>
foreach(DataRow row in tbl.Rows)
list.Add(new MyObject { Name = row[0].ToString(), NameListId = (int)row[1] });
cboManager.ItemsSource = list;

Resources