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.
Related
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.
I need to create a dynamically populated two-dimensional array of Button or ImageButton controls.
I want to be able to set the number of buttons in a row.
The table will receive the data from a DataSource control or from code behind, and populate each cell with a Button control with the corresponding value.
For example, if I set then number of columns to 3:
Value1 Value2 Value3
Value4 Value5 Value6
Value7 Value8
What ASP.NET control is best for implementing this?
Thank You.
You can do it by using Repeater Control.
.aspx Page
<div style="width:269px">
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:Button runat="server" ID="btn" Text='<%# Eval("Value") %>' />
</ItemTemplate>
</asp:Repeater>
</div>
Here you can set the width of div upon your requirment.
.cs page
DataTable dt = new DataTable();
dt.Columns.Add("Value");
dt.Rows.Add("Value1");
dt.Rows.Add("Value2");
dt.Rows.Add("Value3");
dt.Rows.Add("Value4");
dt.Rows.Add("Value5");
dt.Rows.Add("Value6");
dt.Rows.Add("Value7");
dt.Rows.Add("Value8");
Repeater1.DataSource = dt;
Repeater1.DataBind();
The output will be look like this
You can use DataList for this. RepeatColumns will help you to set the no. of columns.
<asp:DataList ID="dlButtons" runat="server" RepeatDirection="Horizontal" RepeatColumns="3">
<ItemTemplate>
<asp:Button runat="server" ID="button" Text='Your Text' />
</ItemTemplate>
</asp:DataList>
All i need is to display data in a repeater or any other control like this:
<asp:Repeater ID="MyRepeater" runat="server" DataSourceID="MyDT">
<ItemTemplate>
<asp:Label Text="NAME OF FIRST COLUMN" runat="server" ID="NameLabel" />
<br />
<asp:Label Text="VALUE OF FIRST COLUMN" runat="server" ID="ValueLabel" />
<hr />
</ItemTemplate>
</asp:Repeater>
and then if i changed DataSourceID in code behind, repeater dynamically display new data in the same format.
Why i need to do this?
because there are many tables in my data base and i don't want to design a repeater for each table.
thank you very much.
You can get the column name in the OnItemCreated event of the Repeater.
e.g.
DataRowView rowview = (DataRowView)e.Item.DataItem;
DataRow row = rowview.Row;
foreach (DataColumn col in row.Table.Columns)
{
nameOfColumn = col.ColumnName;
// then add that to the relevant control in the row
}
If the number of columns is variable depending on table - then you will need to look at using nested repeaters and some code behind to control those.
If we add a customer header to the gridview, will it add extra row?
Currently I have a gridview with four columns and, when I add a custom header gridview coming with a five rows.
My code looks like this...
<asp:TemplateField HeaderText="" ItemStyle-Width="4%" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<HeaderTemplate>
<asp:DropDownList ID="Select" runat="server">
<asp:ListItem>Country</asp:ListItem>
<asp:ListItem>Region</asp:ListItem>
<asp:ListItem>Title</asp:ListItem>
</asp:DropDownList>
</HeaderTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="" ItemStyle-Width="4%" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:HiddenField ID="Id" Value='<%#Eval("id")%>' runat="server" />
<asp:Literal ID="ltrImage" runat="server"></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
and three other TemplateFields...
Is there any problem of adding header this way? Any other way to add customer header without having this issue?
My desired output should look like this
Search Result Search By (Dropdownlist)
Data column1 Data column2 Data column3 Data column4
The one I am getting now is
Sort By (Dropdownlist)
Data column1 Data column2 Data column3 Data column4
Could anyone help if have an idea?
Thanks in advance
You just added another column with custom header to your gridview. If you want to customize a header of the first column, just customize the header of your first template field:
<asp:TemplateField HeaderText="" ItemStyle-Width="4%" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<HeaderTemplate>
<asp:DropDownList ID="Select" runat="server">
<asp:ListItem>Country</asp:ListItem>
<asp:ListItem>Region</asp:ListItem>
<asp:ListItem>Title</asp:ListItem>
</asp:DropDownList>
</HeaderTemplate>
<ItemTemplate>
<asp:HiddenField ID="Id" Value='<%#Eval("id")%>' runat="server" />
<asp:Literal ID="ltrImage" runat="server"></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
If this dropdownlist is too big or if you want add some additional text in the header, you can always create HeaderTemplate for other TemplateFields of merge some of the column's headers in code behind (example for "merging" two first headers, Id of the gridView is gridView1):
protected void gridView1_PreRender(object sender, EventArgs e)
{
int indexOfColumnToSpan = 0;
int indexOfColumnToRemoveHeader = 1;
gridView1.HeaderRow.Cells[indexOfColumnToSpan].ColumnSpan = 2;
gridView1.HeaderRow.Cells.RemoveAt(indexOfColumnToRemoveHeader);
}
well, you have not actually added a header row, but a header column .. which is basically just another column with more controls ..
why dont you place your DropDownList before your GridView, and then you can write some code behind handling the DropDownList_SelectedIndexChanged event to sort the Data ?
I have implemented this without using the filters within the GridView. Then you can handle the events separately, and populate the GridView accordingly.
What you are actually adding is a column, instead of a row. That's why you are getting that result.
Hope this helps!
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").