Setting DisplayMemberPath of ComboBox in code - wpf-controls

In my WPF program I have:
string queryString = "Select AccountID, ProjectName from Foo where IsEnabled = 1";
SqlDataAdapter adapter = new SqlDataAdapter(queryString, sConn1);
DataSet dsAccounts = new DataSet();
adapter.Fill(dsAccounts, "Accounts");
cbAccount.ItemsSource = dsAccounts.Tables["Accounts"].AsEnumerable();
cbAccount.DisplayMemberPath = "ProjectName";
When my program runs and I dropdown the ComboBox all the rows are there but they display as blanks. When I click on a row, my SelectionChanged event handler properly identifies the selected row and picks up the proper values.
I believe my problem is with the DisplayMemberPath.
What am I doing wrong?

This is not an answer but rather a workaround. This works:
cbAccount.DataContext = dsAccounts.Tables["Accounts"];
//cbAccount.ItemsSource = dsAccounts.Tables["Accounts"].AsEnumerable();
cbAccount.DisplayMemberPath = "ProjectName";
By setting the DataContext reather than the ItemSource then the DisplayMemberPath is being set properly.
The question remains open, there must be a way to properly set the DisplayMemberPath when one has an ItemSource rather than a DataContext.

I believe the problem is that your table accounts is not serialized to objects.
If you use a list of accounts instead of your tables then it works perfect with the ItemsSource and DisplayMemeberPath.

Related

How to access a FormControl checkbox in an Excel sheet using OpenXML SDK

I have a spreadsheet that has a number of check boxes in various cells on the sheet that I need to get the value of (checked/unchecked) from within a c# program.
I'm using the OpenXML SDK v2.5 and the associated toolbox.
Using the toolbox I can see the check box controls as part of the AlternateControlParts collection. These are not ActiveX checkboxes but are form controls added via the developer tab in Excel.
When I use the SDK I can also see the WorkSheetPart which has a ControlPropertiesParts collection on it which lists all the checkboxes.
My problem is, how do I find which checkbox is in which cell or at least related to which cell?
I have also found the collection
wsPart.ControlPropertiesParts.First().DrawingsPart
.WorkSheetDrawing.DrawingsPart.WorkSheetDrawing
This collection appears to have the alternate content of each of the checkboxes and if I drill down further I can find the anchor points which appear to give the location of the checkboxes relative to the cells on the sheet. However, the col and row Id’s don’t appear to exactly match up and I suspect that the Offset values may also have something to do with it.
If someone can point me in the right direction on how to map the checkboxes to the correct row/cells I would be very grateful.
Thank you for any help.
Regards
Paul
I have a solution, it contains only the logic (The property FormControlProperties is available since Office 2010:
SpreadsheetDocument document;
string sheetName = "sheetName";
string controlName = "Option Button 5";
...
var wbPart = document.WorkbookPart;
var theSheet = wbPart.Workbook.Descendants<Sheet>().FirstOrDefault(s => s.Name == sheetName);
var wsPart = (WorksheetPart)wbPart.GetPartById(theSheet.Id);
var control = wsPart.Worksheet.Descendants<DocumentFormat.OpenXml.Spreadsheet.Control>().FirstOrDefault(c => c.Name == controlName);
var controlProperies = (ControlPropertiesPart)wsPart.GetPartById(control.Id);
bool isChecked = controlProperies.FormControlProperties.Checked == "Checked";
But it is simplier to map the FormControl value to a cell and read the cell value if the you can edit the excel file.

Access to value of a Table Item

I am doing a Vaadin project and maybe it's easy question but my brain is stop working right now.
I have a Table which code is like:
Table table = new Table();
table.addContainerProperty("Value", String.class, "");
table.addContainerProperty("combo", ComboBox.class, null);
table.addItem(new Object[]{"asd123", combo1}, 1);
table.addItem(new Object[]{"asd1234", combo2}, 2);
combo1 and combo2 are ComboBoxes, their code is like:
ComboBox combo1 = new ComboBox();
combo1.addItem("Choice 1");
combo1.addItem("Choice 2");
ComboBox combo2 = new ComboBox();
combo2.addItem("Girls");
combo2.addItem("Boys");
I want to access an item on table, and hold this as String.
Assume that there is a String str and String str2 variables, and I want that (str = asd123) and (str2 = combo2's selected element).
How can I do that?
If you can help, I appreciate that.
table.getItem(itemId);
// or
combo2.getValue();
However, these functions both return Object values so you have to typecast them to String before using them properly.
String str = (String) table.getItem(itemId);
For further reference see Vaadin Table and Vaadin ComboBox.

Adding content control throws an exception dynamically

I am fairly new to Word Addin development. Fortunately I was able to do almost everything but stuck at some simple issue I belive.
I want to insert plain text controls dynamically at the selected range. For this I am using the following:
currentDocument = application.ActiveDocument;
foreach(var field in myFieldsList)
{
Microsoft.Office.Interop.Word.Range rng = currentDocument.ActiveWindow.Selection.Range;
object oRng = rng;
var contentControlPlain = application.ActiveDocument.ContentControls.Add(Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlText, ref oRng);
contentControlPlain.Tag = formField.FormFieldId.ToString();
contentControlPlain.SetPlaceholderText(null, null, " <" + formField.FormFieldName + "> ");
contentControlPlain.LockContentControl = (formField.TypeName.Trim() == "Blank");
}
Code seems to be working fine but when I try to insert the second field it complains saying:
This method or property is not available because the current selection partially covers a plain text content control.
I understand that addin is trying to insert next content control into the previously inserted plain text control. But I tried giving some other range and could not fix it.
Any help is greatly appreciated.
Thanks.
After adding every content control use
Application.Selection.Start = lastControl.Range.End+1

ColumnCount in DataGridView remains 0 after assigning data source

I am having trouble with the following simple code
BindingList<Car> tempList = new BindingList<Car>();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = tempList;
dgTempView.DataSource = bindingSource;
Here, dgTempView is a data grid view
After the above lines execute, the column count in the datagrid view remains 0. And when I try adding a Car instance in tempList, I get an error saying that 'no row can be added to a datagridview control that does not have columns' . I am not able to understand what am I missing here
Found my mistake. The members of the Class Car were public instance variables and not properties. The moment I changed them to Auto Properties it worked :)

How to get the value in an Excel dropdown using C#

I am looking for code to open and read an Excel file, any version of Excel, including 2010. One of my columns has a dropdown in it. I need to get the value of the selected item in the dropdown. I would eventually want to populate these values into a business object.
If anyone has some code to share please let me know.
I am using C# and Visual Studio 2010.
Thanks.
I know the VBA for both the ActiveX combo and the forms dropdown, and based on that, I can give you some very inexpert notes for c# for the forms dropdown, the combo eludes me as yet.
Working with notes from: http://support.microsoft.com/kb/302084
//Get a new workbook.
oWB = (Excel._Workbook)(oXL.Workbooks.Open("C:\\Docs\\Book1.xls"));
//3rd Sheet
oSheet = (Excel._Worksheet)oWB.Sheets.get_Item(3);
//This will return an index number
var i = oSheet.Shapes.Item("Drop Down 1").ControlFormat.Value;
//This will return the fill range
var r = oSheet.Shapes.Item("Drop Down 1").ControlFormat.ListFillRange;
oRng = oSheet.get_Range(r);
//This will return the value of the dropdown, based on the index
//and fillrange
var a =oRng.get_Item(i).Value;
//Just to check
textBox1.Text = a;
This may help with an ActiveX combo, but I have only half got it to work:
using MSForm = Microsoft.Vbe.Interop.Forms;
<...>
Excel.OLEObject cbOLEObj = (Excel.OLEObject)workSheet.OLEObjects("ComboBox1");
MSForm.ComboBox ComboBox1 = (MsForm.ComboBox) cbOLEObj.Object;
Console.WriteLine(ComboBox1.Text);
From: http://www.eggheadcafe.com/community/aspnet/66/10117559/excel-get-value-from-a-combobox.aspx

Resources