vb.net - ComboBox with a BindingSource and a different DataSource - database

In my form I have a DataGridView bound to a BindingSource that has a DataSet as DataSource. I also have some TextFields and ComboBoxes on the form that are bound to different columns in the DataSet through the BindingSource. The idea is that the values in the columns on the selected row in the DataGridView are reflected in the other controls on the form. Maybe I make it sound a bit complicated, but it's fairly easy to connect the TextFields, and also the ComboBoxes bound to tables in the dataset.
My problem is that this time I want to set the items in the ComboBox from an array and not from a table in the DataSet. This is what I've tried:
Me.ComboBox.DataBindings.Add(New System.Windows.Forms.Binding("SelectedValue", Me.TblBindingSource, "ColumnName", True))
Dim ItemArray(2) As String
ItemArray(0) = ""
ItemArray(1) = "Default"
ItemArray(2) = "User-set"
ComboBox.DataSource = ItemArray
Now, this seems to work partially as the ComboBox is populated correctly, and I can select a value, and it appears in the DataGridView. But it doesn't update its selected value as I change rows in the DataGridView. The column ("ColumnName") is a ComboBoxColumn that gets its item list in the way shown above, and it seams to work as expected.
If it wasn't clear; I have several ComboBoxes with similar functionality that works, but they are bound to a column in a DataTable, as follows:
Me.ComboBox1.DataBindings.Add(New System.Windows.Forms.Binding("SelectedValue", Me.Tbl1BindingSource, "WiDMethodX", True))
Me.ComboBox1.DataSource = Me.Tbl2BindingSource
Me.ComboBox1.DisplayMember = "SomeColumn"
Me.ComboBox1.ValueMember = "SomeColumn"
If it matters, the DataSet comes from an Access Database.

SelectedValue is used in conjunction with the ValueMember property, but since your array list doesn't have descriptive fields, that won't work.
Try using SelectedItem for your binding:
Me.ComboBox.DataBindings.Add(New Binding("SelectedItem", _
Me.TblBindingSource, "ColumnName", True))

I realize this questions is old, but I had similar problem I resolved. I bound the text property of the combo box to the binding source member that relates to the value member or display member of my combo box datasource. Make sure that you fill your data tables (for binding source and combo box datasource) AND bind your combo box to its datasource prior to databinding text of combo box.
Dim dtForBindingSource as DataTable
Dim bs as BindingSource
Dim dtForComboBox as DataTable
'Code to fill dtForBindingSource would go here
bs.DataSource = dtForBindingSource
'Code to fill dtForComboBox would go here
ComboBox.DataSource = dtForComboBox
ComboBox.DisplayMember = "ColumnToDisplay"
ComboBox.ValueMember = "ColumnXYZ"
'Now that datasources exist and combo box is set up I do databindings.
ComboBox.DataBindings.Add("Text", bs, "ColumnToDisplay")

Related

Why will my Datagridview not display information from the DataTable to which I bound it?

I have created a form with a datagridview on it. I bound that DGV to a datatable which is constructed from a table in a SqlServer CE database. I was able to add, edit and delete records and save changes to the database.
Then, I set AutoGenerateColumns to False so I could set up the DGV with nice formatting. To begin, I added only one column:
'/// Set Up DataGridView
With dgvMaterials
.AutoGenerateColumns = False
'.Columns.Add("MaterialName", "Material")
End With
Dim col As DataGridViewColumn
'
col = New DataGridViewColumn
With col
.Name = "Material"
.DataPropertyName = "MaterialName"
.Width = 200
'.HeaderText = "Material"
End With
dgvMaterials.Columns.Add(col)
dgvMaterials.DataSource = stuff.Materials
(Stuff is an instance of a class where I do my SQL stuff, Materials is the name of the DataTable)
Now I must have checked DataPropertyName a hundred times and I verified that there are records in the DataTable, but they will not show in the DGV. Why?
As always thanks in advance for any help offered.
Folks, the answer is simple: I put my column in as type DataGridViewColumn and for whatever reason it had to be DataGridViewTextBoxColumn to work. I switched it out, now the datagridview works like a champ.

How to bind a wpf combobox to a DataTable - but do not show duplicate values

Having some trouble figuring this out. So I have a simple combobox, and I bind it to an existing DataTable in the code-behind like so:
roomCombo.ItemsSource = ((IListSource) myDataTable).GetList();
roomCombo.DisplayMemberPath = "Number";
The combobox shows everything i have in the Number column, including duplicates, of course. I'm looking for a way to show only unique values ..
Thanks.
I found a good way to do this:
DataView view = new DataView(myDataTAble);
DataTable distinctValues = view.ToTable(true, columnName);

How do I assign selected WPF datagrid row columns to variables. (VB.NET)

I have a datagrid with customer data such as ID, name, email, phone etc.
When I select a row (with a button or selectionchanged) I want to store that row's columns in variables like
dim email as string
dim name as string
email = dgCustomers.theselectedrow.theselectedcell
name = dgCustomers.theselectedrow.theselectedcell
If I have a datatable with only one row I know I can get column data with:
dim email as string = CustomerDataTableInstance.rows(0).Item("Email")
I don't know how to get the selected row number though when I have several rows and the user clicks one/uses the keyboard.
The datagrid is bound to a datatable, not a class with object collections.
Any help is appreciated!
Did you try the SelectedItem or SelectedIndex properties?
I don't know much about using datatables, but I suspect you can get some kind of row object that represents a row from the datatable, by using SelectedItem, which you can then use to get each of the columns you want--which is just like binding to a collection of objects works.
If that doesn't work, try the SelectedIndex property. I'm pretty sure it exists, but I might be wrong since I never use it--I always just bind my data to SelectedItem.
I just answered a similar question for someone else. Please see VB.NET WPF How to get the column value from datagrid? for more detail, but Benny Jobigan is correct, you would use the SelectedIndex to get at your row, then use the Item collection to get at your column values. It would look something similar to this:
Dim View As DataView = TryCast(DataGrid1.ItemsSource, DataView)
If View IsNot Nothing Then
Dim ViewRow As DataRowView = View.Item(DataGrid1.SelectedIndex)
Dim ColumnValue As Object = ViewRow.Item("ID") ' or ViewRow.Item(0) for positional value.
' do something with ColumnValue here.
End If
Hope that helps!

reordering rows in a databound datagridview

I have a DataGridView that is bound to a DataTable, is there a way to reorder the rows in the DataGridView and have the changes reflected in the bound DataTable?
Use a DataView or a BindingSource between the DataTable and the DataGridView, they both have a Sort property :
DataView view = new DataView(table);
view.Sort = "Name ASC, Age Desc";
dataGridView.DataSource = view;
You can also filter the results using the DataView.RowFilter property (or BindingSource.Filter).
The DataGridView will automatically reflect the changes

WPF DataGrid sort by ComboBox field

I have WPF datagrid with combox column (ID is real value, Desc is displayed value) and when I click on header of that column automatically sorts by real value (ID). I want to sort by displayed value.
My WPF datagrid has 4 columns: IdPerson, DescSchool, IdSchool and School. Column "School" is comboBox with this values:ItemSource = schoolTable.DefaultView, SelectedValueBinding = new Binding("IdSchool"), SelectedValuePath="IDSchool", DisplayMemberPath = "DescSchool"
schoolTable is a table with 2 columns - IDSchool and DescSchool. That table is used just as datasource for combobox.
I tried the solution when I have set SortMemberPath = "DescSchool" and initially, this works - when I click on the header of the combobox column sorting is done by displayed value (because it read value of the other column) and not by real value. But, if I change the value of the combobox, value of the column "DescSchool" is still the same so after that sorting doesn't work anymore properly.
Any idea?
Setting SortMemberPath="Desc" (or what your property is called) on the DataGridComboBoxColumn should do the trick.
fall into similar problem recently.
try something like :
SortMemberPath="School.DescSchool"
Hope it will help you or someone else!
I have also had this problem and had to implement IComparable on the type that is being sorted. So in your case I think it is the School type. Inside IComparable, return this:
return this.Desc.CompareTo((obj as School).Desc);
This is the only way I was able to get this to work, and judging by the lack of responses, not many people know a better way...sorry.
Also, this will only work if you have access to the types. If this is a datatable or something like that (as opposed to Entity Framework for example) this solution will not work.
I had similar problem to solve with WinForm and its DataGridView displaying relational data in a combobox column in the grid. Wanted to sort the column but the sorting would sort by the ValueMember (an int ID field) and not the DisplayMember (text field being displayed).
I was using a typed dataset and put its typed table into a DataView and use that DataView as the Datasource for a BindingSource which was then the Datasource for the grid. :-)
After reading several posts on the web, the solution that worked for me was the following:
In the code-behind of the Typed dataset, I added some custom properties to the Typed Row of my main typed table.
Example:
public string ProvinceAtRow
{
get
{
string result = string.Empty;
if (!this.CustomerInfoRow.IsProvinceDescriptionNull())
{
result = this.CustomerInfoRow.ProvinceDescription;
}
return result;
}
}
Next in the code-behind of the WinForm that has an instance of my typed Dataset, I added a DataColumn to the typed table.
Example (code in the Load event of the winForm):
this.DSGrowth.LocalGrowthFactor.Columns.Add(
new DataColumn("Province", typeof(string)));
Next when have data in the dataset, must fill in the column(s) that were added.
Example:
//if we have data then have to fill in the colums we added to the TDS
if (this.DSGrowth.LocalGrowthFactor.Rows.Count > 0)
{
//fill columns so that can display on datagrid as columns that can be sorted
// ref http://www.daniweb.com/forums/thread283915.html
Array.ForEach<dsLocalGrowthFactor.LocalGrowthFactorRow>( this.DSGrowth.LocalGrowthFactor.Rows.OfType<dsLocalGrowthFactor.LocalGrowthFactorRow>().ToArray(),
row =>
{
row["Province"] = row.ProvinceAtRow;
}
);
//accept changes on TDS so not to be prompted to save changes
this.DSGrowth.AcceptChanges();
}
//set default sort
DataView dvFactors = new DataView(this.DSGrowth.LocalGrowthFactor);
dvFactors.Sort = "GrowthFactor DESC";
this.BSLocalGrowth.DataSource = dvFactors;
this.DgvGrowth.DataSource = this.BSLocalGrowth;
if (this.DgvGrowth.Rows.Count > 0)
{
//select first row
this.BSLocalGrowth.MoveFirst();
this.DgvGrowth.Rows[0].Selected = true;
}
No more ComboBox columns and sorting works!
Hope this helps all out there who are in similar situation.

Resources