How to data bind combobox inside a user control? - c#

I am working on a winforms project using the c# language. I have a problem about databinding a combobox that is inside a usercontrol. The connection is ok at design time because it can preview data. However, during run time, it will not show any bound data. By the way, I only used the combobox's smart tag because I don't have a code to bind it. :D
Just follow what I did so that you will get my point. :)
I have a windows form (Form1) and put a button (button1) and a panel (panel1) on it.
Then I added a user control (UserControl1) to the project. And then I put a combobox (comboBox1) to the usercontrol. I used the combobox's smart tag to bind data into it.
Here's what I did to the combobox using the smart tag:
I checked Use Data Bound Items. On the datasource, I clicked Add Project Data Source and the Configuration Wizard appeared. I choose Database, Dataset and I included the sensitive data to the connection string. Then I chose tblCategory with 2 columns, ID and Category, as Database Objects. I named the dataset as dbDataSet. Then I clicked finish. Then I added Category as the Display Member and ID as the Value Member.
I added a code to button1's Click event. Here's the code:
new UserControl1().Parent = this.panel1;
This code will add the user control to the form whenever the button is clicked.
But the problem is, no bound data will show during run time.
Can you give me a code that can bind data to the combobox in this kind of setup?
Thank you very much. :)

Related

Add usercontrols dynamically inside a GridView

Here's my problem. I am trying to add usercontrols dynamically to different rows of a GridView.
Let me explain the scenario further. Let's say, I have a page where the user can place orders for different kinds of items. The order form for each item is different in layout and code, and has its own usercontrol. There is an "Add More" button, which lets the user add an extra row to the GridView, while the kind of form that gets appended is selected by the dropdown list (see picture below).
What I am trying to do is - have a separate UserControl for each one of these forms, and then have the UserControl added (or maybe I should say "bound") to the GridView. Every time a new row is added, I plan to re-bind the GridView, including rows from above, to maintain all the rows.
The problem is - the main page cannot know what fields there will be inside each individual UserControl (they can be anything, really, and down the road, newer UCs could be added). So my main question is - how do I bind the data to the GridView, when I do not know what to bind? Is there any way to bind the data from inside each UserControl itself?
I hope my problem and question are clear enough. Thanks for helping.

Syncing two BindingSources

I have a BindingSource in my main form, and I have a DataGridView in a User Control. I'd like to use the main form's BindingSource on the DGV, but I want to use the designer to customize the columns.
In my attempt, I dragged the same data source onto the user control, which created a new binding source. This let me use the designer to customize the columns. Then, in the code behind, I wrote
UserControl.CompaniesBindingSource.DataSource = CompaniesBindingSource
This got the data working, but the Current property isn't syncing up.
Is this possible?
I find that trying to use the Visual Studio Designer for the datagridview and manipulate it programmatically in code is very hard. I have never been able to get both to work how I want it to. What I find programmers do is create the datagridview on the form, but they don't do anything with the designer. Then, the entire datagridview is created and manipulated programmatically which, I know, usually takes extensive programming.

silverlight4 MVVM how to editi an EF4 on a grid using two forms

C# VS2010 SL4 MVVM EF4
need to replace edit in place on a datagrid.
any one can point me to a SL4 MVVM sample code. the requirements are as follows:
I have 2 usercontrols.
One shows an Edit button and a Datagrid where each row is containing an EF object.
the other is just a usercontrol with a textboxes and a Savebutton. (is called edit form)
the user selects from grid a row containing an EF object an click the edit button to change it.
the grid is replaced by the edit form showing the contents of the selected row,
user applies changes to the data and clicks the SaveButton.
this edit forms now closes and
the changed row now shows the row with editted values.
thanks.
Try using the DataForm in the Silverlight toolkit. You can set the CurrentItem on the DataForm to the SelectedItem from the DataGrid.

how to copy data from Form to another Form using C#?

I have got 2 Forms. The first one contain Data Grid View 1 and the second one contains Data Grid View 2 and Button. I want code, using C#, that when the user clicks on Button,
the data in the Data Grid View 2 is taken and displayed in Data Grid View 1. Can anyone can tell me how to do this in details please.
Depending on what you're really trying to do, I would suggest to make use of the BindingSource class in both your forms, and bind them according to what you need with your DataGridviews, then expose your BindingSource.DataSource through a Form property which will allow you to get and set the datasource outside of your Form, hence allowing you to change or update your [DataGridView.DataSource]4 property through this Form's property.

adding a control to tabs generated dynamically in tabcontrol in C# .net

I am new to C# .NET.
Can some one help me out with the below problem:
I have a TabControl in my WindowsForm application, where the tab pages are generated dynamically. The content to be displayed on each tab would be fetched from my database. I need some kind of control (which can display the fetched data) that I can add on each tab page (which would be same for all tabs) such that I can associate some kind of event, say click, on that added control.
Can anyone tell me how to do this programmatically & write the click event for all the controls added?
Please refer the below link! You will get more detail in this regard.
Creating a tab control with a dynamic number of tabs in Visual Studio C#
I'm not sure I completely understand your problem but my initial thoughts are that you could dynamically create a datagrid or something similar for each tab that you are dynmically creating. You could then bind the datasource for the grid and then add the grid as a control to your tabpage.
Something like...
DataGridView gv = new DataGridView();
gv.DataSource = //whatever your source is
this.tabPage1.Controls.Add(gv);
You would then have all the events associated with the grid to work with.
I'm thinking data binding is going to be your best bet for displaying this information. You can create a list of objects and use a DataTemplate to format the data. You can apply the DataTemplate to a quite a few objects. I generally use the ItemsControl and ListBox
http://msdn.microsoft.com/en-us/library/ms750612.aspx
good luck

Categories

Resources