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.
Related
I am making a WPF GUI where I have a datagrid with several columns. I would like to make cell type in one of the columns vary depending on the selection in another cell. A cell would need to swap between Text and ComboBox.
In the image below the datagrid can be seen. I want functionality where when user checks the IsConstant checkbox, then he can enter a constant numeric value, otherwise he would be presented with a ComboBox from which he could select an option he likes. The checkbox column is bound to a bool value in codebehind.
However I am not sure how and If this is possible. Any suggestions are welcome. Thanks.
I think the only way to achieve what you want is through the use of a DataGridTemplateColumn. You can define a custom template for this column type, which contains controls for each type of edit option you want to provide, with the visibility of each control switching depending on the conditions you have.
I did a search, and found This answer which deals with this very issue, with an example provided. I think you could probably make a simpler template than is given in this example, but the idea is the same.
Edit:
Here's an alternate example which uses a template selector (too long to post inline here).
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
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.
I read somewhere that one of few weaknesses in data binding model is that you never explicitly handle or create the data object that’s bound to the control and as a result you don’t have a chance to add an extra item. But I’m not sure what is meant by that.
What or how exactly would you be able to add an extra item if you were allow to explicitly create a data object? Would you basically be able to bind control to data source and let the runtime automatically fill the control with data source’s data , and additionally that control could also get an extra item from data source by manually creating it?! How would it be able to do that?!
thanx
When you use the OnItemDataBound event, you have full access to the underlying datasource object via e.Item.DataItem. You can use this data to populate any controls in the ItemTemplate via e.Item.FindControl("controlname"). You can also use functions inside the <%# %> tags to format text or calculate values.
What you have read, in my estimation, is pure crap. Up until the point of binding, I can alter the objects in question. One common scenario, for example, is adding a column to rows in a DataTable object (which is actually a collection of rows and columns). I can, in fact, alter by adding a column (let's say sum) to each row.
I can, with some restrictions on classes, do the same with other types of collections and objects.
After I have bound the object, I can still add items to the output by using the databinding method for a row, so I am still not restricted.
In general, I find those that are expounding this garbage are defending using ASP style code in an ASP.NET page.
I have a reportviewer and i want a field to act as a hyperlink. The hyperlink must look like: page.aspx?id=1 But how do i achieve this?
I have entered in the properties window, navigation tab, radio "Jump to URL": page.aspx?id=sum(Field!field.value)
This doens't work :(
What do i have to do to get this work?
Thnx in advance
Martijn
PS: I also have EnableHyperlinks set to true;
Your expression under "Jump to URL" should be:
="page.aspx?id=" & sum(Fields!field.value)
Although I see 2 potential issues with that. First of all, if I remember correctly, the URL must be an absolute path (e.g. http://www.test.com/page.aspx). Secondly, I'm not sure why you're summing on a field. If you mean to only get the "current" value of some field, you don't need the aggregate function, but you have to be sure you are inside a control that repeats data for each row of a dataset, e.g. a detail row of a table.