I put data in a data table e.g.
dt.TableName = "SA1";
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
Now I'm not sure if I should use boundfield (For all columns)
<asp:BoundField DataField="Unit" HeaderText="Unit" SortExpression="Unit" />
or use
<asp:TemplateField>
<HeaderTemplate>
Units
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox runat="server" ID="txbUnits" Text='<%# Eval("Unit")%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
and add the data as i go along, the gridview's purpose is only to display data
If you want to display rows without anything fancy, or any particular design, you would use BoundField. However if you would like to design the displaying of the record in a different manner than the default - you would need to create your own row template, by using the TemplateField.
Check out these links - they briefly explain the differences, but it is basically default VS customised presentation.
http://forums.asp.net/t/1369418.aspx?boundfield+vs+template+field+in+gridview
http://www.c-sharpcorner.com/Interviews/answer/1751/difference-between-boundfield-and-templatefield
If you want to just display the data then you should use bound field attribute.
The BoundField displays the value of specified DataSource field as
text.
The TemplateField allows for a mix of HTML markup, Web controls,
and data-binding syntax.
Your purpose is only to display data.So i think you need BoundField Here
Related
It seems like there is HeaderTemplate and ItemTemplate and EditItemTemplate within the TemplateField at the code which I am examining.
What else can exists with <asp:TemplateField> apart from HeaderTemplate and ItemTemplate and EditItemTemplate?
They are in effect a means to "drop in" standard asp.net controls into quite a few of the data aware controls.
So, GridView, Datagrid, Repeater and a few more require this to allow dropping in of standard controls to render.
So, for a Gridview, you can drop in plane jane asp.net controls, but to do so, you require to wrap those plane jane controls in what is called a "Template".
so for a grid, you might see this:
Note the template field for a simple check box. So, when we run this, we get this:
So, the template has featuers like "HeaderText" etc.
However, if you use a newer ListView? Then you don't need to use Template fields, and if you only have say a few template fields, then a GridView is fine. However, if you have say a boatload of asp.net controls you want in that Grid, then I perfer a ListView.
So, this template field lets you define header text, formatting, justification etc.
so, for example, the above check box don't look good - not center, so you can add this to the markup:
<asp:BoundField DataField="HotelName" HeaderText="HotelName" />
<asp:TemplateField HeaderText="Active" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"
Checked='<%# Eval("Active") %>' />
</ItemTemplate>
</asp:TemplateField>
And now we get this:
So for a data repeater, Dataview, Gridview and a few more, then they have build in data bound controls (BoundField), but often we want to use controls like a drop down list, a picture, or whatever in the markup - so you have to wrap the plane jane asp.net control(s) each in a template field, and this lets you define the heading for the grid, the justification etc.
I am trying to get values of string that I keep inside GridView.
When I use regular BoundFields, things work get. I get whatever is needed with:
string my_value = myGrid.Rows [rowIndex].Cells[1].Text;
However, one grid needs to have hyperlinked entries in one of the columns. I did:
<asp:BoundField DataField="domainName"
HeaderText="Domain"
SortExpression="domainName"
HtmlEncode="false"
DataFormatString="<a href=DomainConfiguration.aspx?suffix={0}>{0}</a>"
My ASPX page shows the correctly formed hyperlinks. However, if I retrieve Text for the cell, it returns "<\a href=DomainConfiguration.aspx?suffix=example.com>example.com</a>" [without the two extra slashes], instead of "example.com"
What do I need to do to get GridView working the way I want? [Yes, I would rather use GridView and not another control.]
Thank you.
Instead of asp:BoundField to show hype link it is better use asp:HyperLinkField like:
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="domainName" HeaderText="domainName" DataNavigateUrlFormatString="DomainConfiguration.aspx?suffix={0}" DataTextField="domainName" />
</Columns>
and to get string value of that column you should try this:
string my_value= ((HyperLink)myGrid.Rows[rowIndex].Cells[1].Controls[0]).Text;
instead your asp:Bounfield use
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="editLink" runat="server" onclick='<%#Eval("EditLink") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
I've read multiple sources that say Gridview's do not persist the Gridview.DataSource property on postback. My understanding is that in term's of ASP.NET, a postback is any page load that is not the first pageload (see MSDN).
I've got a situation with 2 very similar gridviews.
GvOne.DataSource is null on postback.
GvTwo.DataSource is NOT null on postback.
The only big difference outside of a few differing columns is GvOne is populated with the Entity Framework and LINQ. GvTwo is populated by a DataTable filled by a SqlDataAdapter.
Further, GvOne and GvTwo have a TemplateField with a TextBox that I use to gather user input. Both use the same code to pull the TextBox.Text on postback:
TextBox tb = (TextBox)GvOne.Rows[i].FindControl("actualTxt");
GvOne properly gathers tb.Text. GvTwo always finds the tb.Text value to be 0.
Basic Gridview code:
<asp:GridView ID="GvOne" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Return">
<ItemTemplate>
<asp:TextBox id="actualTxt" runat="server" Text='0' Width="40px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
...
</Columns>
</asp:GridView>
<asp:GridView ID="GvTwo" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Order">
<ItemTemplate>
<asp:TextBox id="actualTxt" runat="server" Text='0' Width="40px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
...
</Columns>
</asp:GridView>
Changing GvTwo to use Entity Framework and LINQ is a potential solution, albeit a major undertaking. Does anyone know what is going on here?
UPDATE (See my comment on Joel Etherton's Answer)
Due to popular demand here is the code to populate the gridview within Page_Load event for GvTwo (GvOne is similar):
ordersGV.DataSource = dataSetObject.Tables["activeParts"];
ordersGV.DataBind();
Searching through the code behind I found no other references to ordersGv.Datasource and no other events that are hooked into associated with the page life cycle.
Gridviews do not persist the datasource across postbacks. If you have a gridview that has a non-null datasource then you must be filling that datasource somewhere in your code. It would be instructive to travel through your event cycle to find where exactly the population of the datasource is occuring on postback.
what does your Page_load code look like?
GridView does not keep DataSource property populated over the postbacks for performance issues
Maybe the second gridview is rebinding the datasource on postback?
In Insert Mode I want to show one kind of Template and Edit Mode I want to show another Kind of Template in Devexpress Control in C#.NET
This can be implemented using the following approach:
You should define the EditFormTemplate so that it contains a different set of editors for the Insert and Edit functionality. Handle the HtmlRowCreated event to hide non required editors based on the ASPxGridView's IsNewRowEditing property value.
Check out the sample project on this issue which demonstrates the solution based on user controls and binding expressions. In this solution, the EditForm template contains two user controls with the Visible property bound to the ASPxGridView.IsNewRowEditing property:
[HTML]
<uc1:Edit id="Edit1" runat="server" Visible="<%# !Container.Grid.IsNewRowEditing %>"></uc1:Edit>
<uc2:Insert id="Insert1" runat="server" Visible="<%# Container.Grid.IsNewRowEditing %>"></uc2:Insert>
Btw, this issue might also be helpful. If you need more help, please contact the DevExpress support team here.
Use an ASP template field and individual item templates inside your gridview
<asp:TemplateField id="test" runat="server">
<ItemTemplate>
<ItemTemplate>
test
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Text="test" runat="server"/>
</EditItemTemplate>
</ItemTemplate>
</asp:TemplateField>
I did the above from memory. Heres a link to an example i found as well:
http://programming.top54u.com/post/ASP-Net-GridView-Edit-ItemTemplate-Mode.aspx
In my application I am creating rows and columns dynamically. I created a column of type System.DateTime. After this i want to display datetimepicker control for all rows in that column.
I created a column using
dataTable.Columns.Add("CreatedOn", Type.GetType("System.DateTime"));
and i am adding rows as
foreach(String filename ......)
dataTable_FileProperty.Rows.Add(filename,//here i want to add dateTimePicker
So, what is a solution for this.
EDIT: Please provide some code snippet. I am new to C#.net.
Thanks.
By default you have just these columns available for you:
DataGridViewTextBoxColumn, DataGridViewCheckBoxColumn, DataGridViewImageColumn, DataGridViewButtonColumn, DataGridViewComboBoxColumn, DataGridViewLinkColumn
If you want to show a datetimepicker control then you have to implement a custom column.
Check this out: http://msdn.microsoft.com/en-us/library/7fb61s43.aspx
Hope this helps.
use the item template of the gridview and place a datetimepicker there. A good example is here
For implementing it you have to implement the ITemplate interface.
Another example is this
An easy implementation of it is given in this msdn article. But the code is in VB.net.
DataTable.Rows contains data, i.e file name, date, some strings.
GridView.Columns contains controls to display data.
So if you're using DataRowCollection.Add(Object[]) :
DataTable DataTable1 = new DataTable();
DataTable1.Columns.AddRange(
new DataColumn[] {
new DataColumn("file", typeof(string)),
new DataColumn("date", typeof(DateTime)) });
foreach (string f in System.IO.Directory.GetFiles(#"c:\windows"))
DataTable1.Rows.Add(f, System.IO.File.GetCreationTime(f));
GridView1.DataSource = DataTable1;
GridView1.DataBind();
And the markup of GridView:
<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="File" DataField="file" />
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<asp:Calendar runat="server" ID="Calendar1" SelectedDate='<%# Bind("date") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Also you need to read more about Calendar.SelectedDate and Calendar.VisibleDate