C# how to display datetimepicker control for all rows of gridview - c#

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

Related

Asp Gridview use Boundfields? or Templatefields?

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

Bind Literal control inside Gridview from codebehind.

I have a gridview which contains template fields in which every template field contains a litreal control, and I want to bind that grid view with my DataSet, please look into the code to find more.
Code to create DataSet-
DataTable Record = new DataTable();
Record.Columns.Add("zerker");
DataRow dr = Record.NewRow();
dr["zerker"] = "SomeText";
Record.Rows.Add(dr);
gvCustomres.DataSource = Record;
gvCustomres.DataBind();
Code to create GridView-
<asp:GridView ID="gvCustomres" runat="server" PageSize="4" AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField HeaderText="Zerker">
<ItemTemplate>
<asp:Literal ID="zerkername" runat="server"></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
</columns>
</asp:GridView>
Please help me to find correct way to do this.
Thanks,
Your code above is all right if you just want to bind Column "zerker" to your gridiview.
All you are missing is the Text Property for your Literal control.
<asp:Literal ID="zerkername" runat="server" Text='<%# Eval("zerker") %>'>
</asp:Literal>

Add DropDownList to a gridview cell

I'm trying to do a DropDownList in one cell of the gridview but I only can add columns.
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataValueField="Catalog" DataSourceID="SqlDataSource1"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
How can I add an only dropdownlist to a gridview?
To add DropDownList to GridView cell you should use ItemTemplates. You can find more details Here
Also another sample is here
I found a solution in asp.net forum, here let you the link if somebody need it:
Add DropDownList to a gridview cell

How to make a template field(textbox) as a part of the datatable

I have a GridView .
its data source is a data table i create it pro-grammatically in .cs to suit what i wanna to show. Now i wanna more column in this gridview as a template field containing a text box.(as a part of my data table). How to do this .Please if there is a sample or example this will be great.
Following tutorial will help you. This is on ASP.net 2.0 but there will not be a much different on latest versions.
http://msdn.microsoft.com/en-us/library/bb288032.aspx
Added Few other resources based on new comment
http://www.highoncoding.com/Articles/29_Creating_Datagrid_columns_programmatically.aspx
http://www.codeproject.com/KB/aspnet/create_template_columns.aspx?df=100&forumid=281019&exp=0&select=1726624
TemplateField bfield = new TemplateField();
bfield.HeaderTemplate = new GridViewTemplate(ListItemType.Header, col.ColumnName);
bfield.ItemTemplate = new GridViewTemplate(ListItemType.Item, col.ColumnName);
GrdView1.Columns.Add(bfield);
<asp:TemplateField HeaderText="MyTextField">
<ItemTemplate>
<asp:TextBox runat="server" id="txtField">
</ItemTemplate>
</asp:TemplateField>
More details here: http://msdn.microsoft.com/en-us/library/aa479353.aspx
However, I think you're looking to do more than just this - but I can't be certain....

Gridview Column Removing

I have a web application that I am working on(ASP.NET 2.0 C#). In it I have a GridView whose data source is an Oracle database. I get the data into the gridview in my codebehind, and don't set the datasource directly.
I wanted to create a hyperlink field (NAME) that takes me to a details page about a specific record. What ends up happening is that it creates the Hyperlink field as well as the regular field that it gets from the datasource, which I don't want. If I remove the field from my SELECT statement, it gives an error saying something like: "NAME" not found in datasource.
How can I eliminate the regular field, and get a hyperlink field instead? I have tried Gridview.Columns.Remove(columnlocation), but that won't work coz the columns don't exist there originally.
Please Help
Thank you.
Instead of removing columns, disable AutoGenerateColumns property of the gridview and set your hyperlink column manually like that :
<asp:gridview id="GridView1"
autogeneratecolumns="false"
runat="server">
<asp:HyperLinkField DataNavigateUrlFields="UserID"
DataNavigateUrlFormatString="UserDetails.aspx?id={0}"
DataTextField="UserName" />
</asp:gridview>
Sounds like you have AutoGenerateColumns property set to TRUE on your grid. This means that a column is generated for EVERY column you return from your query.
If you want to have some custom columns, you should set AutoGenerateColumns="false" and add all the columns to the GirdView as asp:BoundField and your Hyperlink column as asp:TemplateField
Let me know if I'm off the mark with this
here's some code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Name" />
<asp:BoundField DataField="Whatever" />
<asp:TemplateField>
<ItemTemplate>
<a href='<%# Eval("UserId", "URL_TO_USER?userId={0}") %>'>Details</a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Categories