LINQ Combobox Databinding behave weird - linq

I'm having a WinForm on witch I've a DataGridView that display a list of sites. bellow that grid i've a TextBox and a Combobox, using DataBinding the textbox show the current value "Code" grid column and the combobox show the province. The combobox is filled with a list for province
The weird thing is this:
When I change the combobox selected province for another one, at the time when the focus leave the combobox, the original value of the of the combobox return. I can't find a way for the combobox to keep the changed value.
what am I doing wrong?
Here my the code of my workbench project;
private void Form1_Load(object sender, EventArgs e)
{
context = new GMR_DEVEntities();
lSite = from t in context.tblSites where t.Actif == true select t;
this.dataGridView1.DataSource = lSite;
this.dataGridView1.AutoGenerateColumns = true;
Guid ProvinceId = Guid.Parse("00000000-0001-0000-0008-000000000001");
IQueryable<tblDomVal> provinces = from prov in context.tblDomVals where prov.pTypeDomValID == ProvinceId select prov;
comboBox1.DataSource = provinces;
comboBox1.ValueMember = "ID";
comboBox1.DisplayMember = "DescrFr";
textBox1.DataBindings.Add(new Binding("Text", lSite, "Code"));
comboBox1.DataBindings.Add(new Binding("SelectedValue", lSite, "pProvinceID", false, DataSourceUpdateMode.OnPropertyChanged));
}
Any help would be welcome :)
Hugo

Try creating a new binding context for you combo
BindingContext oBC = new BindingContext();
comboBox1.BindingContext = oBC;
comboBox1.DataBindings.Add(new Binding("SelectedValue", lSite, "pProvinceID", false, DataSourceUpdateMode.OnPropertyChanged));

While searching for a solution, I've discover that the problem have something to do with the Datatype (All my tables IDs are GUID), I've change the Binding to bind with a varchar field instead of the guid, and thing went good. So the problem have something to do with GUID. But I'll change my approach, and work with manual bind

Related

C# DataGridView Event to Catch When Cell (ReadOnly) Value has been changed by user

I have a Winforms app written in C#.
In DataGridView, I have set a column(DataGridViewTextBoxColumn) called 'Name' to ReadOnly = true.
When the user right click on a Cell of the 'Name' column -> Display a form to set a value -> I want the application to know: Cell's value of the 'Name' column has been changed.
I have tried many events but can not do, such as CellEndEdit, CellValueChanged
I'm only interested in catching user changes to data in the 'Plan' column where ReadOnly = true.
Which is the best event to use for this?
I attach information as below:
① Image Description
② Source Code
The CellEndEdit and CellValueChanged properties only work when the user changes manually the content of the DataGridView. What you have to do is just to read the value of the TextBox in the Set_Value_Of_Name form and compare it with the value of the DataGridView cell.
Add the following line :
btnOK.Click += (senderq, eq) =>
{
frmValueOfName.Close();
result = true;
//Add this line
if (txtValue.Text != dataGridView[currentMouseOverCol,currentMouseOverRow].Value.ToString())
{
MessageBox.Show("Value has been changed!");
}
};

Trying to set the first text of a combobox with dropdownlist style C#

combobox.text = "blabla";
doesnt work
combobox.SelectedValue = number;
doesn't work
I'm trying to set a text like "select an item" on a dropdownlist style combobox, nothing i've read so far actually works, because i set it like a dropdownlist (works as a read-only combobox)
any hint?
It actually works, I was just misplacing the code
public Form1()
{
InitializeComponent();
combobox.text = "blabla";
}
As long as item blabla exists in the collection, it will display.

Adding scrollbar to Telerik GridViewDataControl for expandable grid item

I have a Telerik:RadGridView that has items in it, some of which are expandable. The decision to make the row expandable is done in the RowLoaded event by setting the IsExpandable flag based on the type of object in the row. This works great.
To handle the row expansion, I have a method to handle the DataLoading event. It looks like this:
void AssignedNumbersGrid_DataLoading(object sender, GridViewDataLoadingEventArgs e)
{
GridViewDataControl dataControl = (GridViewDataControl)sender;
if (dataControl.ParentRow != null)
{
dataControl.ShowGroupPanel = false;
dataControl.AutoGenerateColumns = false;
dataControl.CanUserFreezeColumns = false;
dataControl.IsReadOnly = true;
dataControl.SelectionMode = System.Windows.Controls.SelectionMode.Extended;
dataControl.IsFilteringAllowed = false;
dataControl.ShowInsertRow = false;
dataControl.RowIndicatorVisibility = Visibility.Collapsed;
dataControl.ChildTableDefinitions.Clear();
dataControl.Margin = new Thickness(0, 0, 0, 0);
dataControl.EnableRowVirtualization = true;
dataControl.MaxHeight = 100;
ScrollViewer.SetVerticalScrollBarVisibility(dataControl, ScrollBarVisibility.Auto);
dataControl.Columns.Add(BuildSelectColumn());
dataControl.Columns.Add(BuildNewColumn("Range Number", "DisplayAssociatedInfo"));
dataControl.Columns.Add(BuildTypeColumn());
dataControl.Columns.Add(BuildRemarkColumn());
dataControl.Columns.Add(BuildNewColumn("Status", "DisplayStatus"));
}
}
I added the code to set the max height and the attached scroll viewer but the scroll bar does not show up. the dataControl object is of type GridViewDataControl, which is not the same as a RadGridView. Does anyone know how I can get a scroll bar to show on the expanded grid? The reason I need this is that the expanded grid may contain several hundred items and it takes several seconds to build the grid if it's large. I am thinking that with row virtualization and a scroll bar, it will be much faster.
Have you tried this:
<telerik:RadGrid ..>
.....
<ClientSettings EnableRowHoverStyle="True">
<Scrolling AllowScroll="True" EnableVirtualScrollPaging="True" SaveScrollPosition="True">
</Scrolling>
</ClientSettings>
</telerik:RadGrid>
I solved the problem by using a row details feature. The row details has another grid that contains the child items. The performance for this option is very fast.

System.Web.MVC.SelectList how to set selected item upon creation

trying to have an item in a DropDownList selected by default from within my viewmodel.
I am creating the select list in my view model using teh following overload..
public SelectList ProgramsSelectList()
{
var ddlist = new SelectList(DiversionPrograms, "DiversionProgramID", "CrimeName", new {value = "1"});
return ddlist;
}
value = 1 is just hard coded for now. I am not sure the Html.DropDownList() respects this selected item.
Razor View:
#Html.DropDownList("programSelect", Model.ProgramsSelectList(), "Choose a Program to Manage")
I must be missing something here...
Figured it out...
var ddlist = new SelectList(DiversionPrograms, "DiversionProgramID", "CrimeName", DiversionProgram.DiversionProgramID);

How to set the SelectedValue attribute of Telerik RadComboBox inside the FormTemplate of a RadGrid

I have what I think should be a straightforward question. I have a RadGrid with FormTemplate editing and AJAX enabled. One of the fields in the FormTemplate is a RadComboBox filled with U.S. State selections. I can bind the RadComboBox to the data source to populate all the items, but I'm not able to set the SelectedValue attribute.
This RadComboBox is loaded when the Edit button is clicked for a row on the RadGrid. A custom FormTemplate is used and the contents of the row being edited are loaded via AJAX.
If you are DataBinding, its literally as easy as adding
SelectedValue='<%# Bind("FieldName")%>'
Inside the FormTemplate declaration of the RadComboBox.
If you however want to programmatically determine what value to select, then you need to implement ItemDataBound in the RadGrid, like the following example:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
{
GridEditFormItem editFormItem = (GridEditFormItem)e.Item;
RadComboBox combo = (RadComboBox)editFormItem.FindControl("yourControlName");
combo.SelectedValue= Somevalue;
}
}
clear all items of radcombobox initially and then add a new item manually
this is what i do to set new item when i use web service
ddl.ClearSelection()
ddl.Items.Clear()
'below i'm getting the actual value and the text to display
Using reader As IDataReader = GetClientByClientID(CInt(value))
If reader.Read Then
'adding the item will show in the dropdown
Dim item As New RadComboBoxItem(reader("DisplayName").ToString, reader("ID").ToString)
item.Selected = True
ddl.Items.Add(item)
'this line will make the combobox text to be displayed correctly
ddl.Text = reader("DisplayName").ToString
ddl.DataBind()
Else
ddl.Text = ""
ddl.ErrorMessage = "Selected Client Not Found !"
End If
reader.Close()
End Using

Resources