I am pretty shocked no one asked this question before as when I searched I couldn't anything related,
https://stackoverflow.com/search?q=what+is+a+bound+field+c%23
Anyway my question is,
Can someone explain what a bound field is please in easy words and when we use it with example.
Research I did
We use it in a GridView or DataView but why we can't use the default option for displaying data then using Bound Fields.
Well Data Binding in general is the principle of declaratively stating that some user interface element's value will come from some source, and be populated by the runtime rather than the developer manually setting and getting values from controls in codebehind files.
So in WPF, for example, you can set the DataContext property of an entire window to an object, and then for each control on that window say from which properties of that object the WPF runtime should get their value.
For example, for an Employee viewmodel with Forename and Surname properties, you might create an EmployeeView window with two textboxes, where one is "bound" to the Forename property and the other is "bound" to the Surname property. At runtime, the framework will look at the bindings on each control, fetch the value from the data automatically and populate the control's value field. Likewise, when the value in the control is modified by the user, data-binding can push the new value to the data model it is bound to.
This is in contrast to the typical approach in the days of VB6, where setting those textboxes' content would be done in the codebehind of the form (e.g. forenameTextBox.Text = employee.Forename). Data binding in VB6 (and WinForms, for that matter) is different, where the framework does what I described above, but automates getting data from a database in the process. That's fallen out of favour in recent years, though (and for good reason).
The BoundField class is used by data-bound controls (such as GridView
and DetailsView) to display the value of a field as text. The
BoundField object is displayed differently depending on the data-bound
control in which it is used. For example, the GridView control
displays a BoundField object as a column, while the DetailsView
control displays it as a row.
For more visit MSDN Help Bound Field Description
Related
I am creating a server control (inheriting from CompositeControl).
The control comprises of several data bound drop down lists (and some textboxes etc).
The drop down lists form a hierarchy, so are dependent on each other... eg
ddlCountry
-ddlCounty
--ddlCity
When ddlCountry is selected, this should refresh the list of ddlCounty... and so on with the ddlCity etc.
I would like to access the value of the ddlCountry within the control, so I can set the data source for second ddl (ddlCounty) accordingly,
but in CreateChildControls() the viewstate does not seem to have loaded the ddlCountry user selection yet into the control, so I get an empty string.
I also need to provide these ddl values as a public property.
This is getting quite messy, with the use of ReCreateChildControls() etc... which I'm sure is wrong.
My Question:
Can anyone suggest the correct pattern / set of rules to abide by to create this server control
eg
initialise controls OnInit
DataBind on OnLoad
Put EnsureChildControls() at beginning of any property: get or set
I assume there must be a correct way of implementing this, but cannot find any documentation or example to get this working as you would expect of a standard control.
Thanks
I have 400+ textboxes on a page. The page is meant to be an exact replica of a paper form. The user is going to edit all of the fields and then save the data to the DB. The data is being pulled down into a DataTable from a SQL DB.
I'm planning on saving the data via the same DataTable or just via a bulk update. Not 100% on that. But, I need to get access to that data. And maybe I'm not doing this next best part the best and if I'm not, I'd appreciate it if I was informed of a better way.
When the DataTable gets the data, I assign each field into the appropriate control. Below is an example of how the data is being added.
foreach (DataRow CDR in ClaimDataTable.Rows){
if (CDR["BoxNumber"].ToString() == "1.1")
this.Box1_1.Text = CDR["DataValue"].ToString();
if (CDR["BoxNumber"].ToString() == "1.2")
this.Box1_2.Text = CDR["DataValue"].ToString();
if (CDR["BoxNumber"].ToString() == "1.3")
this.Box1_3.Text = CDR["DataValue"].ToString();
I wrote some code to automatically create that code. So I didn't manually write all 400+ lines.
What I was thinking, was that I could add a LostFocus event to each TextBox. Then when the control loses focus, I would create a class with a box name and the box value. Add that to a list and when they're ready to save, just loop through the list and do the bulk update with the BoxNumber and the box data.
Would that be feasible? Or is there a better method?
Thanks!
Look into event routing.
You should not create and access individual text-boxes but let the framework create them via datatemplating a collection, your above code is ze horror.
When the DataTable gets the data, I assign each field into the
appropriate control.
WPF is data binding and one does not generally load controls as you mentioned. Why is the code not binding to each control to a property which adheres to INotifyPropertyChanged?
Have the page's DataContext (DC) point to an instantiated class which adheres to InotifyPropertyChanged interface. The setting of the DC in that fashion will allow all controls on the page to use its datacontext by default.
In the class to be instantiated for the DC create a member property which calls PropertyChanged when the value is set using the INotifyPropertyChanged system.
Bind each TextBox Text property to that property on the class.
Then each control which needs that value will display it automatically after it has been set such as in this example.
<TextBlock Name="tbHeader"
Text="{Binding CDRData}" />
Then one only has to write the value to the property named CDRData once and all textboxes bound get the value.
I want to add a tool tip for items in a Property Grid. When the user hovers over a property I want the tooltip to display a hint about the usage of that property. The hint string should be different for each different value of the property — for example if one property is a list of strings each string would have a different hint.
Is this possible?
The PropertyGrid is not very flexible and doesn't expose any of the individual controls on it. You can access the control (textbox or dropdown) that you're looking to show the tooltip on via reflection but that is far from trivial, especially since all the control classes are unique and internal to the property grid.
Using the Description attribute is by far the best value. If your list of strings for that property aren't obvious enough to portray their meaning without providing a tooltip, perhaps you should revisit the string text you are showing for each item in the list.
Can anyone tell me if what is detailed in this example is possible with GridView using TemplateColumns. http://www.codeproject.com/KB/webforms/CustomDataGridColumn.aspx
I need to dynamically generate a GridView that may need not have one datatype in a column. Say Column 1 could have Checkbox or TextBox control (based on a logic, of course)
I tried implementing ITemplate and adding custom controls in InstantiateIn(), but as far as I understand - the binding here on on a per column level and not on per Item level.
How can I acheive this?
Thanks in advance. I can stub out the code I have if the question needs more clarification.
Yes, Telerik's RadGrid supports this.
The same code will work with only minor changes. For example, ListItemType becomes GridItemType. DataGridItem becomes GridDataItem, etc. Also, the way Telerik creates the controls means some of the indices need to change. For example, their ItemDataBound event has this code:
string dataType = e.Item.Cells[0].Text;
but with a Telerik RadGrid, that needs to be:
string dataType = e.Item.Cells[2].Text;
Other than those kinds of minor changes, it all works with RadGrid.
Note: When I see code samples from Telerik, they generally go a different route. The way I've seen them provide custom content for a cell is to put your customization logic in the ItemCreated event. That will be called for each row. So you can have the column's template contain all the possible controls and then in ItemCreating event, modify the instance of that template by setting the correct control to Visible based on your logic (this is the approach used in the article you linked to). Or you could create only a single specific control in that event for the correct type of control. Here is the documentation for the ItemCreated event and also a code sample from Telerik that modifies a LinkButton for each row but the same approach can be used.
If you bind a control in a FormView using two way binding (such as Text='<% #Bind("FieldName") %>'), how do you retrieve the field name "FieldName"? There are several things I want to do with this information; for example, I have a database table that contains a dump of all the field definitions from ERWin and I want to programmatically create some sort of context-sensitive help for each individual field (there are hundreds spread across dozens of forms).
This is pretty much an exact duplicate of a question asked a year ago but the answer didn't make much sense to me. First, the answer seemed to be for a GridView and not a FormView (e.Row.Cells[0] gave it away). Second, what does SortExpression have to do with anything? Third, it mentions an event argument, but for what event? In OnDataBound, EventArgs e is empty.
There does not appear to be any way to get at this information from a FormView, as the column name is not stored at the level you want it.
However, I must admit that I do not understand why you want to retrieve at runtime something that you know at compile time. Why is it not possible to just write the code you need? Even if you want your code to be more generic, you can create a dictionary of control names and their associated bound column names to pass to your class that does whatever it needs to do.
Regarding the answer to your last question - the GridView stores the column name in the SortExpression property, so that it knows what column to sort by when the user resorts the grid. Hence, in a GridView, you can access the column name through the SortExpression.
An easy way of doing this is to programatically assign the name of the data field to the Tooltip property of your Formview controls, then the data field names will be shown to the user as they mouse over these controls. If you want a more specific answer, please specify if this is what you are trying to accomplish.