BindingContext problem - c#

I'm having problems with BindingContext in a .NET application. I need to populate 6 comboboxes with the same datasource but the first 3 comboboxes must be indepedent from the last 3 comboboxes.
I coded the following:
combo_Bancos_cheque.DataSource = bancos;
combo_Bancos_cheque.DisplayMember = "Nombre";
combo_Bancos_cheque.ValueMember = "IDBanco";
combo_ctas_cheque.DataSource = bancos;
combo_ctas_cheque.DisplayMember = "NoCuenta";
combo_clabe_cheque.DataSource = bancos;
combo_clabe_cheque.DisplayMember = "CLABE";
combo_Bancos_dep.BindingContext = new BindingContext();
combo_Bancos_dep.DataSource = bancos;
combo_Bancos_dep.DisplayMember = "Nombre";
combo_Bancos_dep.ValueMember = "IDBanco";
combo_ctas_dep.DataSource = bancos;
combo_ctas_dep.DisplayMember = "NoCuenta";
combo_clabe_dep.DataSource = bancos;
combo_clabe_dep.DisplayMember = "CLABE";
The first 3 comboboxes work fine, when combo_Bancos_cheque changes combo_ctas_cheque and combo_clabe_cheque also change which is the expected behaviour. Then I create a new BindingContext to unbind 4,5 & 6 combobox which also use the same datasource.
The problem here is: when combo_Bancos_cheque value changes, combo_ctas_dep & combo_clabe_dep also change but I don't want this, I need these comboboxes to change only when combo_Bancos_dep changes.
I'm new to BindingContexts, what am I missing?

This is a quick guess, but don't you need to point combo boxes 5 and 6 to the same binding context as combo 4 (combo_Bancos_dep) ?
What if you create the new binding context into a variable, and then set it to CBs 4,5, and 6 ?
EDIT:
I just checked to confirm, and the above is correct. You were really close, you just needed to set your new BindingContext to combo_ctas_dep and combo_clabe_dep.
Here is what you posted with the change:
combo_Bancos_cheque.DataSource = bancos;
combo_Bancos_cheque.DisplayMember = "Nombre";
combo_Bancos_cheque.ValueMember = "IDBanco";
combo_ctas_cheque.DataSource = bancos;
combo_ctas_cheque.DisplayMember = "NoCuenta";
combo_clabe_cheque.DataSource = bancos;
combo_clabe_cheque.DisplayMember = "CLABE";
BindingContext oBC = new System.Windows.Forms.BindingContext();
combo_Bancos_dep.BindingContext = oBC;
combo_Bancos_dep.DataSource = bancos;
combo_Bancos_dep.DisplayMember = "Nombre";
combo_Bancos_dep.ValueMember = "IDBanco";
combo_ctas_dep.BindingContext = oBC;
combo_ctas_dep.DataSource = bancos;
combo_ctas_dep.DisplayMember = "NoCuenta";
combo_clabe_dep.BindingContext = oBC;
combo_clabe_dep.DataSource = bancos;
combo_clabe_dep.DisplayMember = "CLABE";

From what I can infer, I believe the behavior is expected - you are binding the same source to the comboboxes.
I believe this statement:
combo_Bancos_dep.BindingContext = new BindingContext();
does not do what you wish it to do - that is bind to a new source.
If "bancos" is a DataTable, you can remove the above line and just do a Copy on "bancos" to create a new DataTable and use that to bind your other combos:
DataTable copyOfbancos = bancos.Copy();
combo_Bancos_dep.DataSource = copyOfbancos;
combo_Bancos_dep.DisplayMember = "Nombre";
combo_Bancos_dep.ValueMember = "IDBanco";

Related

Can't get WPF Binding data to a ListView using GridView to work

I am trying to display my List in a ListView using a GridView. I am using dynamic binding as my list view changes. it's gridview for other purposes.
I have already used similar code twice and had no issues. I have checked every variable using a debugger and executed the code step by step and everything seemed normal.
// the code of the function
public void loadPairsIntoListView(object sender, RunWorkerCompletedEventArgs args)
{
XmlComparator xComp = XmlComparator.getInstance();
List<ComparePair> listOfPairs = xComp.getListOfPairs();
multiCompareListOfCompared.ItemsSource = listOfPairs;
multiCompareModelLabel.Content = listOfPairs[0].model.trueName + " " + listOfPairs[0].model.envName.ToUpper();
GridView myGridView = new GridView();
myGridView.ColumnHeaderToolTip = "Objet recap";
GridViewColumn gvc1 = new GridViewColumn();
gvc1.DisplayMemberBinding = new Binding("compared.trueName");
gvc1.Header = "nom";
gvc1.Width = 100;
myGridView.Columns.Add(gvc1);
GridViewColumn gvc2 = new GridViewColumn();
gvc2.DisplayMemberBinding = new Binding("compared.envName");
gvc2.Header = "environnement";
gvc2.Width = 100;
myGridView.Columns.Add(gvc2);
GridViewColumn gvc3 = new GridViewColumn();
gvc3.DisplayMemberBinding = new Binding("anomalies.Count");
gvc3.Header = "Ecarts";
gvc3.Width = 100;
myGridView.Columns.Add(gvc3);
multiCompareListOfCompared.View = myGridView;
Log.S();
}
// the class i'm trying to bind
public class ComparePair
{
public XmlFile model;
public XmlFile compared;
public List<int> anomalies;
private const int diffOnlyMarge = 10;
/// Methods ...
}
//
I am getting an output where the List is actually binded to the ListView (i can click on and use every row) but the rows are actually empty. I am getting all the three columns with their names but their rows has no "visual content" while still has the object bind to it.
What i am expecting is to see the actual values i have bind to each column.
I hope i have been clear enough. Tell me if you need more precision.

How to create two object with the same properties - Cloning not working

I have to create two datagrid with the same properties:
var dtg = new DataGrid
{
Margin = new Thickness(10),
EnableColumnVirtualization = false,
EnableRowVirtualization = false,
Background = Brushes.Transparent,
Foreground = new SolidColorBrush(m_ExeCfg.GetForeground()),
};
and then dtg2 with the same properties...
but Datagrid dtg = dtg2 = {...} is not allowed
therefore I have used this cloning:
string dtgXaml = System.Windows.Markup.XamlWriter.Save(dtg);
StringReader stringReader = new StringReader(gridXaml);
System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(stringReader);
DataGrid dtg2 = (DataGrid)System.Windows.Markup.XamlReader.Load(xmlReader);
so how can I create two objects with exactly the same properties?
Looks like you are sererializing to dtgXaml, but that is never used.
Instead you use some gridXaml variable.
string dtgXaml = System.Windows.Markup.XamlWriter.Save(dtg);
StringReader stringReader = new StringReader(gridXaml);

DevExpress.XtraEditors.CheckedComboBoxEdit Items are displaying as bold after migration

I have migrated my application to DevExpress 15.2.I am getting my DevExpress.XtraEditors.CheckedComboBoxEdit items are displaying in bold.I have set following properties.Please let me know if I missed any property.I tried with Font property but selected Item is only changing not all the Items.
this.cmbTemplates.Location = new System.Drawing.Point(503, 618);
this.cmbTemplates.Name = "cmbTemplates";
this.cmbTemplates.Properties.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] { new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)});
this.cmbTemplates.Properties.SelectAllItemCaption = "Select All";
this.cmbTemplates.Size = new System.Drawing.Size(259, 20);
this.cmbTemplates.TabIndex = 93;
Try this :
cmbTemplates.Properties.AppearanceDropDown.Font = new
Font(cmbTemplates.Properties.AppearanceDropDown.Font, FontStyle.Regular);

Databinding---Inserting empty row in a combo-box with DropDownList

I'm new to Visual Studio and fixing a small bug in an application.
The combo box which exist is editable with DropDown eventhough it is has databinding. Because If I deleted a value, it would not save the change, I made it into a DropDownList; however, now I do not have the option of using null.
I have searched around and have been reading that I can insert an item before databinding.
I have the bits and peices of the codes from different functions for this combo box. Not sure where to exactly make the change.
if someone could point me in the right direction, that'd be great.
private System.Windows.Forms.ComboBox cmbSecCSR;
//----------------
this.cmbSecCSR = new System.Windows.Forms.ComboBox();
//---------------------
// cmbSecCSR
//
this.cmbSecCSR.AccessibleRole = System.Windows.Forms.AccessibleRole.TitleBar;
this.cmbSecCSR.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
this.cmbSecCSR.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
this.cmbSecCSR.DataSource = this.csrBindingSource2;
this.cmbSecCSR.DisplayMember = "Name";
this.cmbSecCSR.FormattingEnabled = true;
this.cmbSecCSR.Location = new System.Drawing.Point(112, 26);
this.cmbSecCSR.Margin = new System.Windows.Forms.Padding(0);
this.cmbSecCSR.MaxDropDownItems = 10;
this.cmbSecCSR.Name = "cmbSecCSR";
this.cmbSecCSR.Size = new System.Drawing.Size(184, 21);
this.cmbSecCSR.TabIndex = 2;
this.cmbSecCSR.ValueMember = "Username";
this.cmbSecCSR.TextChanged += new System.EventHandler(this.comboBox_TextChanged);
this.cmbSecCSR.Enter += new System.EventHandler(this.cmbBox_Entered);
//
// csrBindingSource2
//
this.csrBindingSource2.DataMember = "CSR";
this.csrBindingSource2.DataSource = this.productionDS;
//-------------------------
//loadUnboundData();
cmbSecCSR.DataBindings.Add("SelectedValue", productionMasterBindingSource, "CSR2", true, DataSourceUpdateMode.OnPropertyChanged);
My Problem is similar to this: http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/fa277629-a65b-4799-9400-364f6f771739/
which is why I decided to change it to DropDownList;however, I don't know how to add the NULL Value in the DropDownList as it is bounded.

Datagridview horizontal scroll does not go all the way across data

I often run into this problem where when using a datagridview, if I need to make use of a horizontal scroll, it will not go to the end of the form. The last column is then rendered inaccessible. I can usually cheat by extending the size of the dgv or starting the form maximized, but if the vertical scroll works as expected (goes all the way down to the last record in the dataset), why doesn't the horizontal?
private void InitializeMemberGrid()
{
// Set grid properties
this.grdMembers.AutoGenerateColumns = false;
this.grdMembers.RowHeadersVisible = true;
this.grdMembers.Columns.Clear();
mPicWaiting = PartD.Windows.Common.Utility.AddWaitingImage(this.grdMembers);
mPicWaiting.Visible = false;
// Member ID Column
var colMemberID = new DataGridViewTextBoxColumn();
colMemberID.Name = "MemberID";
colMemberID.HeaderText = "Member ID";
colMemberID.DataPropertyName = "ID";
this.grdMembers.Columns.Add(colMemberID);
//// Contract ID Column
var colContractID = new DataGridViewTextBoxColumn();
colContractID.Name = "ContractID";
colContractID.HeaderText = "Contract ID";
colContractID.DataPropertyName = "ContractID";
this.grdMembers.Columns.Add(colContractID);
// SSN Column
var colSSN = new DataGridViewTextBoxColumn();
colSSN.Name = "SSN";
colSSN.HeaderText = "SSN";
colSSN.DataPropertyName = "SSN";
this.grdMembers.Columns.Add(colSSN);
// HICN Column
var colHICN = new DataGridViewTextBoxColumn();
colHICN.Name = "HICN";
colHICN.HeaderText = "HICN";
colHICN.DataPropertyName = "HICN";
this.grdMembers.Columns.Add(colHICN);
// Name Column
var colName = new DataGridViewTextBoxColumn();
colName.Name = "Name";
colName.HeaderText = "Name";
colName.DataPropertyName = "LastNameFirstName";
colName.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
this.grdMembers.Columns.Add(colName);
// DOB Column
var colDOB = new DataGridViewTextBoxColumn();
colDOB.Name = "DOB";
colDOB.HeaderText = "DOB";
colDOB.DataPropertyName = "DateOfBirth";
colDOB.DefaultCellStyle.Format = "MM/dd/yyyy";
this.grdMembers.Columns.Add(colDOB);
// Retirement Date Column
var colRetireDate = new DataGridViewTextBoxColumn();
colRetireDate.Name = "RetireDate";
colRetireDate.HeaderText = "Retirement";
colRetireDate.DataPropertyName = "RetireDate";
this.grdMembers.Columns.Add(colRetireDate);
// RelCode Column
var colRelCode = new DataGridViewTextBoxColumn();
colRelCode.Name = "RelCode";
colRelCode.HeaderText = "Rel";
colRelCode.DataPropertyName = "RelCode";
this.grdMembers.Columns.Add(colRelCode);
// Resend Column
var colResend = new DataGridViewTextBoxColumn();
colResend.Name = "Resend";
colResend.HeaderText = "Resend";
colResend.DataPropertyName = "Resend";
this.grdMembers.Columns.Add(colResend);
// Sent Column
var colSent = new DataGridViewTextBoxColumn();
colSent.Name = "Sent";
colSent.HeaderText = "Sent";
colSent.DataPropertyName = "SubmissionCount";
this.grdMembers.Columns.Add(colSent);
// Status Column
var colStatus = new DataGridViewTextBoxColumn();
colStatus.Name = "Status";
colStatus.HeaderText = "Status";
colStatus.DataPropertyName = "Status";
this.grdMembers.Columns.Add(colStatus);
}
You have probably set the width of the DataGridView instead of docking the control within the form.
Make sure the control looks right in the designer and then anchor it on the right hand side. This will stop the end column being chopped off if the form is resized. Better still, add a panel to embed your control in and dock it accordingly.
I hope this helps.
Setting the last column's "AutoSizeMode" property to "AllCells" fixed it for me:
column.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;

Categories