I have a process that pivots data to create columns based on the UniqueIdentifier key value. The resulting data table has column names such as:
> PartNum
> [DB1A6498-7CC6-4EA0-846A-9B6EAB771777]
The portion of ASP is:
<asp:TemplateField HeaderText="Part No.">
<ItemTemplate>
<asp:Label ID="lblPartNum" runat="server" Text='<%#Eval("PartNum") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Run Out">
<ItemTemplate>
<asp:Label ID="lblRunOut" runat="server" Text='<%#Eval("DB1A6498-7CC6-4EA0-846A-9B6EAB771777") %>' />
</ItemTemplate>
</asp:TemplateField>
The Part Number displays just fine, however field based on the uniqueID results in a message:
DB1A6498-7CC6-4EA0-846A-9B6EAB771777/ is neither a DataColumn nor a DataRelation for table .
I read through the data table columns to create the grid columns, which display fine, however when attemtping to dynamically create the ItemTemplate, the error occurs. Putting the brackets "[]" around the uniqueID in the label did not work either.
Any suggestions are greatly appreciated. Thanks
Kevin...your response led me down the path of the brackets. Based on that, I found information about the DataBinder with the final result being that the Text property of the asp:label reads:
Text='<%# DataBinder.GetPropertyValue(Container.DataItem,"[DB1A6498-7CC6-4EA0-846A-9B6EAB771777]") %>'
This is displaying data from the uniqueID fields with no errors.
Related
I have created a bulk edit gridview using Itemtemplate for each column. Label for non-edit mode, Textbox for edit mode. But this works only if the structure of gridview is known(Defining templates in asp).
<asp:TemplateField HeaderText="Name" ConvertEmptyStringToNull="True">
<ItemTemplate>
<asp:Label ID="lblName" Visible='<%# !(bool) IsInEditMode %>' runat="server" Text='<%# Eval("name") %>' />
<asp:TextBox ID="txtName" ControlStyle-CssClass="wide" Visible='<%# IsInEditMode %>'
runat="server" Text='<%# Eval("name") %>' />
</ItemTemplate>
</asp:TemplateField>
Now, what I am trying to achieve is when the user selects a dropdown value, the sql query is fired and it returns result. And this result is shown in gridview. But the problem arises because the number of columns in the result for each selected value from dropdown may vary. And i want to make the gridview as editable(or read-only). This requires two templates to be defined for each column.
So i want to know how this can be done dynamically i.e defining templates depending on number of columns returned by sql.
First off, you could trigger the query and following code with this event.
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//insert code
}
For the issue of different templates, I might suggest using two GridViews. I've done this where I have two separate sets of results. If they choose a dropdownItem, it hides GridView1 and shows GridView2 (while querying GridView2.SqlDataSource).
Now are you querying different SQL Tables based on dropdown? Or are they querying from the same place?
The next code is a part of a Grid View,i am trying to bind data to hyperlink control in the grid view.
The properties of the hyperlink is changing,it depends on the choose,so i need to make them dynamic and bind data to the when the user choose the option,this is the code:
<Columns>
<asp:TemplateField HeaderText="Download">
<ItemTemplate>
<asp:HyperLink ID="Download" runat="server" HeaderText='<%# Eval("choose") %>'
DataTextField='<%# Eval("choose") %>' DataNavigateUrlFields='<%# Eval("choose") %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
"choose" is a string on server side that gets the name of my column name in the data base that i want to display.
I got this error:
'System.Data.DataRowView' does not contain a property with the name
'choose'.
I have a GridView which is editable and is bound to a database. So the datatype of each column in the GridView is Int in the database. Therefore my GridView accepts only integer.
But I want to give a notification to the user if the user enters any other character in the GridView cells other than numbers.
Basically I need to validate and give a error message.
Can someone help me how to go about it?
I have used Boundfields for my GridView.
Therefore suppose I enter any character other than a number it gives me an exception message saying : System.Data.SqlClient.SqlException: Conversion failed when converting the nvarchar value 'a' to data type int.
You best bet is to convert the BoundField into a TemplateField and add the validation control to the EditItemTemplate.
<asp:TemplateField HeaderText="Application" SortExpression="APPName">
<EditItemTemplate>
<asp:TextBox ID="txtApp" runat="server" Text='<%# Bind("APPName") %>'/>
<asp:RequiredFieldValidator runat='server' ID='requiredApp'
ErrorMessage='Application Name Cannot Be Empty' ControlToValidate='txtApp' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="labelApp" runat="server" Text='<%# Bind("APPName") %>'/>
</ItemTemplate>
</asp:TemplateField>
SOURCE
In ASP.NET usign C#, .NET3.5 F/W
Hi,
I've a Gridview with template columns containing textbox and Label controls.
By code through some queries and stored procedures i got some data to a datatable. Now i want to pass this datatable column field or names to the Gridview Label controls.
How can i set the datatable column header names to gridview label control of Header Template?
It's called Binding here is link
<asp:TemplateColumn>
<ItemTemplate HeaderText="PersonId">
<asp:Label id=Label1 runat="server"
Text='<%# Eval("NameOfColumnt") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
Try this
<asp:TemplateColumn >
<ItemTemplate>
<asp:Label id=Label1 runat="server"
Text='<%# DataBinder.Eval(Container,EmployeeName") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
For this to work you will have to give your grid view a data source:
yourGridView.DataSource = yourDataTable;
yourGridView.DataBind();
After you do this, you just have to link each control in template columns to column in datatable, for example:
<HeaderTemplate>SomeText</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="a" Text='<%# Eval("nameOfTheColumn") %>' runat="server"></asp:Label>
</ItemTemplate>
EDIT:
Regarding your other question, I suggest you to add bound fields to your grid view dynamically rather then right away in the aspx. Something like this:
foreach (DataColumn column in dt.Columns)
{
BoundField nameColumn = new BoundField();
nameColumn.DataField = column.ColumnName.ToString();
nameColumn.SortExpression = column.ColumnName.ToString();
nameColumn.HeaderText = column.ColumnName.ToString();
gridView.Columns.Add(nameColumn);
}
This way you don't have to know exactly how many columns you have, and have the ability to name them as they are in the data table.
Im using a LINQDataSource to populate a GridView of Universities.
Each University has an associated State, which is in another table and associated by a foreign key (StateID).
I have a TemplateField in the GridView so that when you view it normally it displays the StateName which comes from the State table and when you edit it shows a DDL populated with everything from the State table.
<asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="State" SortExpression="State">
<EditItemTemplate>
<asp:DropDownList ID="DropDownListStateEdit" runat="server"
DataSourceID="LinqDataSourceStates" DataTextField="StateName" DataValueField="StateID"
SelectedValue='<%#Eval("StateID") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("State.StateName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
If I debug, inside of RowUpdating, GridViewUpdateEventArgs.NewValues doesnt even have a key for State.
Question: How do I let my gridview know I want it to update this column? All the BoundFields just seem to work...
In EditItemTemplate you should use #Bind("StateID") instead of #Eval("StateID").