Hi how do i pass in a class object from UI Form to the user control datagridview?
I created a user control which it has a datagridview, a search textbox on top of datagridview, and a label to show total count. and i would like to have the following feature to be written in that control.
Show Total count for that object bounded to datagridview.
TextChange event to filter the data in datagridview.
I would like the above features to be written in Usercontrol as i felt it is redundant to repeat that in UI Form.
For this, I created a datasource in the usercontrol
public object Datasource { get; set; }
1) If my class object is e.g. User, how do i get the type of the datasource converted to User and get the Count of total User and bind to the label in the usercontrol?
2) I felt it redundant to write this below in every form. How do i simplify this and make this run in the userControl itself by using the datasource i pass in to it.
private void BaseCRUDForm_GridViewTextChangedSearch(object sender, EventArgs e)
{
if (sender.ToString().Length > 0)
{
Users = Users.Where(x => x.Name.StartsWith(sender.ToString())).ToList();
}
else
{
Users = Repository.UserRespository(false).GetAllUsers();
}
UserBindingSource.DataSource = Users;
}
Please advice.
While creating the object of user control, There are two ways to forward the data in user control
Initialize the property. The Property will have the object type which can be cased later while binding to grid.
pass the data to constructor.
I feel that you should have constructor i.e point 2.
Now after casting this object, you can populate the label as well as the grid.
I hope this will help you.
Related
I have an object something like this:
public class Item
{
public string Name;
public int Id;
public int Quantity;
public decimal Volume;
public decimal Cost;
}
I'm wanting to create a reusable user control which would take a list of these objects and display them in a datagrid. The trick is that I want to specify which of the properties are shown in the datagrid for each instance of the control. Unfortunately my WPF skills are not up to the tasks and I don't want to create a specific control for each combination that I will want to use, as that feels like a lot of work for very similar code items. Any help to pointers of how to accomplish this would be greatly appreciated.
You can use the Columns property of your datagrid to interact with the columns and hide the columns you don't want.
datagrid.Columns.RemoveAt(IndexOftheColumn);
or if you named your columns
datagrid.Columns.RemoveAll(x => x.Name = "column name");
or if it is possible that you will need to column later
datagrid.Columns[IndexOftheColumn].Visibility = Visibility.Collapsed;
It is also possible to do this with pure Xaml with bindings but since you are new to Wpf I would suggest doing it in the code behind (the .cs of your Wpf control) first.
As for selecting which columns to hide you can pass a list of names for the columns you want to remove in the constructor of the control or with a binding. There are a lot of ways to do it.
Or you could do the opposite and add columns dynamically based on your needs.
Note that this question has been asked before
I have created a form that has a GridControl in it that is bound to a BindingSource upon instantiation. I intend to have the user be able to have multiple instances of this form open at the same time, while being able to have different filters applied on the separate views. This all currently works great. One thing I'd like to be able to do is disassociate the row selection between the forms. When I click a row in one GridControl, the same row is selected in all other instantiations of the form.
Is there a way to do this? I don't want to create a copy of the BindingSource because having to manage updates to any of the sources across all of them is going to be a real mess considering the complexity and size of the data to begin with. Can I have multiple GridControls bind to the same BindingSource and be able to select rows independently between them?
Update:
I call this method in my Forms constructor:
public virtual void UpdateDataSource()
{
if (_dataFeatures != CurrentInspectionFile.BoundFeatureList)
{
gridControl1.BeginUpdate();
_dataFeatures = CurrentInspectionFile.BoundFeatureList;
DetachEventHandlers();
AttachEventHandlers();
gridControl1.EndUpdate();
}
SetFeatureDataBindings();
gridControl1.DataSource = _dataFeatures;
UpdateLookupLists();
UpdateGridColumns();
}
_dataFeatures is set to a BindingSource object that is constructor in the getter for the BoundFeatureList property. Each Feature is a very complex object in and of itself.
Thanks
C#, VS-2010, winforms
Generally I want to create user control with a bindable MyProperty. But I met some problem: Data row become modified when current row changed.
You can download C# project and try it yourself:
test_bind.zip
To make it simple I did follow:
create test form with a
dataGridView (set it ReadOnly),
TextBox,
some typed DataSet with some Table
bindingSource
bind bindingSource to the table
bind dgv to bindingSource
bind TextBox.Text to bindingSource to some text column ("name")
on form load fill table with some data. (If you add rows manually do not forget AcceptChanges())
add event handler for table xxx_ColumnChanging(...) and set breakpoint there
all standard steps, nothing special
Run
as expected - rows in dgv, current name in textBox, click on different rows in grid...
and only when I modify text in textbox I stop in breakpoint.
Lets modify program, and bind textBox to different property instead of Text - lets say Tag.
Set DataSourceUpdateProperty "OnValidation" or "OnPropertyChanged".
Run
Now current row become modified. !!!
Same happens when instead of textBox I use UserControl with a dummy property
String MyProperty { set; get; }
How to bind custom property in user control with the same behaviour as TextBox.Text?
Note:
Binding property itself is not a problem. Problem is: when I bind to standard TextBox.Text property - everything Ok Wen I bind to ANY OTHER property (Tag) or to custom Property in User Control - then DATASOURCE ALWAYS BECOME MODIFIED FOR CURRENT ROW
Short answer is:
When bind to simple property, declared like
[Bindable(true, BindingDirection.TwoWay)] // I need TwoWay
public String MyProp {set;get;}
then datasource will be updated on current position change. You may count it as Bug or Feature...
To change behavior - I need magic word. Declare event in the control as
public event System.ComponentModel.PropertyChangedEventHandler MyPropChanged;
This name is special: "YourPropertyName" + "Changed" = YourPropertyNameChanged
Other word will not be magic.
As soon as event declared - datasource would not be updated every time as before...
and now I can control the process...
public void OnMyPropChanged(System.ComponentModel.PropertyChangedEventArgs e)
{
Binding bb = this.DataBindings[0] as Binding;
bb.WriteValue();
if (MyPropChanged != null) MyPropChanged(this, e);
}
private void TxtChanged(object sender, EventArgs e)
{
if (load) { return; }
_my_prop = Txt.Text; // ...
OnValueTwoChanged(new PropertyChangedEventArgs("MyProp"));
}
Here is the example project with more details, where I create user control with String property ValueTwo
which split in two different TextBoxes using some custom logic, and combine value and update when user change the text.
test_bind_fixed.zip
And this is article which help me
http://support.microsoft.com/kb/327413
I have following situation in a C# Windows Forms (.NET 4.0) application:
I have a bindingsource which datasource is set to a collection of objects of a custom class.
Most properties of this class can be represented by a textbox or checkbox, as they are strings or numbers or bools.
However, there is one property, called Ratings, which is another custom object and must be represented by a custom control I developed.
I have bound a datagridview to the bindingsource, and obviously the dgview displays the numbers, strings and bools correctly. But on the same form I also have an instance of the custom control I developed, and so want I want to do now is bind my custom control to the same bindingsource as the datagridview uses, and to the specific datamember called Ratings. But I don't have the slightest clue on how to start. I suspect I must implement some interface which implements DataSource and DataMember on my control, but not sure where to begin. So basically what I want is each time the selected item is changed by means of selecting another row in the datagridview, the according Ratings property is represented in the custom control.
Right now the endresult that I wish to achieve is working, but I do this by refreshing my custom control each time the OnSelectedRowChanged event of the datagridview is firing, and associating the Ratings property of the selected item to the custom control.
But I was wondering if I could achieve this purely by the databinding mechanism in .NET.
Thanks for any replies !
Mathieu
ps: to further clarify, as it doesn't seem so clear in pure text:
Psuedo-code representing the object that I use as datasource for the bindingsource is:
public class Car
{
private string number;
private string name;
...
private Ratings ratings;
public string Number
{
get { return this.number; }
set { this.number = value; }
}
public string Name
{
get { return this.name; }
set { this.name = value; }
}
...
public Ratings Ratings
{
get { return this.ratings; }
set { this.ratings = value; }
}
}
So I want all the string and number properties from the bindingsource to bind to the datagridview, but I need the Ratings property to bind to the custom control.
I understood that you'd like to have a special column in the DataGridView with your custom control. To do this, you need to create a subclass of DataGridViewCell and DataGridViewColumn and implement IDataGridViewEditingControl.
See MSDN for an example.
You can read this Microsoft article about interfaces related to data binding. The ones of your interest are at the bottom under "Interfaces for Implementation by Component Authors" section.
I've several web user controls on my asp.net page and I wanna pass values between them. For example :
There is a dropDownList in one of them and when user selects any item from dropDownList, it can pass the selected value to the other user control which includes GridView to show related data of selected item value from the user control which contains the dropdownlist. (woow pretty awkward sentence tho)
Thanks and Regards..
P.s : Can we use User controls as class in the way to return values ?
On the DropDownList onChange event you could then assign the selected item value to the property of the other user control.
Ok, your UserControls should have properties that allow data to be set. For example in Control2 you would have:
public string needData {
get { return MyData; }
set {
MyData = value;
//do whatever you need to do with that data here
}
Now in your page you would capture the DropDownList OnChange event and inside that you would something like this:
myControl2.needData = myDropDownList.SelectedValue;
Does that make more sense?
Use delegates, example here
Hope this helps