Gridview BoundField Remove Time from DateTime In Edit/Update Mode - asp.net

I have the following GridView:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="EVENT,TERM_CODE" DataSourceID="OracleDataSource" ShowHeaderWhenEmpty="True"
EnableViewState="False">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="EVENT" HeaderText="Event" ReadOnly="True"
SortExpression="EVENT" />
<asp:BoundField DataField="TERM_CODE" HeaderText="Term Code" ReadOnly="True"
SortExpression="TERM_CODE" />
<asp:BoundField DataField="SNAPSHOT_DATA_DATE" HeaderText="Snapshot Date"
SortExpression="SNAPSHOT_DATA_DATE" DataFormatString="{0:d}" />
</Columns>
</asp:GridView>
The SNAPSHOT_DATA_DATE column is a DateTime field in the database.
I only want to show the date not both the date and time ex. 7/25/2012
So I added DateFormatString="{0:d}" to the BoundField and this works great when viewing the Gridview. However, if I click the edit link to update the date it displays as: 7/25/2012 12:00:00 AM The DateFormatString is ignored in edit mode. Can someone please tell me how to remove the time when in edit / update mode? I don't want users dealing with the time only the date.

Make sure you have set the following properties for formatting to work in Edit Mode in Bound field.
DataFormatString = ({0:d})
ApplyFormatInEditMode = True
In your code if you just modify like below, it should work fine
<asp:BoundField DataField="SNAPSHOT_DATA_DATE" HeaderText="Snapshot Date" ApplyFormatInEditMode="true" SortExpression="SNAPSHOT_DATA_DATE" DataFormatString="{0:d}" />

I specifically wanted to format my date as yyyy/MM/dd without the time equivalent to the following:
string dateString = DateTime.Now.ToString("yyyy/MM/dd");
To do this I just added the format on the DataFormatString attribute for the BoundField:
<asp:BoundField DataField="DatePosted" HeaderText="Date" DataFormatString="{0:yyyy/MM/dd}" />

Related

Format error with a date in a asp.net bounded field?

I have a date in a bounded field as shown below:
<asp:BoundField DataField="closeDate" HeaderText="End Date" SortExpression="closeDate" htmlencode="false" dataformatstring="{0:d}" >
<ItemStyle CssClass="georgeTest"></ItemStyle>
<HeaderStyle CssClass="georgeTest"> </HeaderStyle>
</asp:BoundField>
I'm trying to format it in this manner: "12/18/2012" with the dataformatstring attribute using both the format shown above and dataformatstring="{0:dd/MM/yyyy}}" they both give me the same value '1/1/0001' all the time and I can't figure out why this happens.
I am working in MS Visual Studio 2010 and ASP.NET 4.0 .
The field is of the DateTime type.
For reference going forward I've posted the link content from my comment as the answer:
How To Set a Date Format In GridView Using ASP.NET 2.0 (Using HtmlEncode Property)
Posted by Peter Kellner in .Net 2.0, ASP.NET 2.0
(AKA, the DataFormatString="{0:M-dd-yyyy}" Problem)
A very common desire is to set a column of a GridView to display just the month, day and year of a DateTime type. The problem is the by default, the HtmlEncode property of the BoundField attribute (
<columns>
<asp:BoundField headertext="CreationDate" dataformatstring="{0:M-dd-yyyy}"
datafield="CreationDate" />
</columns>
You have two choices to make this work as you would expect. The first choice is to simply set HtmlEncode to false as follows:
<asp:GridView id="GridView1" runat="server" >
<columns>
<asp:BoundField headertext="CreationDate" dataformatstring="{0:M-dd-yyyy}"
datafield="CreationDate" HtmlEncode="false" />
</columns>
The second choice is to make the column a template and simply set the format string directly in the Label or Text Fields as follows.
<asp:GridView id="GridView3" runat="server" >
<columns>
<asp:TemplateField headertext="CreationDate" >
<edititemtemplate>
<asp:Label id="Label1" runat="server" Label.Text='<%# Eval("CreationDate", "{0:M-dd-yyyy}") %>'>
</asp:Label>
</edititemtemplate>
<itemtemplate>
<asp:Label id="Label1" runat="server" Label.Text='<%# Bind("CreationDate", "{0:M-dd-yyyy}") %>'>;
</asp:Label>
</itemtemplate>
</asp:TemplateField>
</columns>
</asp>

Custom message for BoundField in gridview in HTML markup

I have a BoundField in my gridview. Some of these BoundField are empty if there is no data in the datasource. For such type of fields I want to show N/A, but from the HTML markup only. Is there any way to do so???
This is the code for my grid view which has empty values and for what I want to show N/A
<asp:BoundField DataField="POCreatedDate" HeaderText="Created On" DataFormatString=" {0:MM/dd/yyyy}"
HtmlEncode="false" SortExpression="POCreatedDate"/>
<asp:BoundField DataField="PODueDate" HeaderText="Due Date" DataFormatString="{0:MM/dd/yyyy}"
HtmlEncode="false" SortExpression="PODueDate"/>
NullDisplayText property is all you need :
<asp:BoundField DataField="POCreatedDate" HeaderText="Created On"
DataFormatString="{0:MM/dd/yyyy}" HtmlEncode="false"
SortExpression="POCreatedDate"
NullDisplayText="N/A" />

don't want to add column name in datatable

I am doing something like this
dt.Columns.Add("SomeCoumnName");
But I don't want to display this column name after binding it to the gridview.
Just want to show a blank header for particular column.
Set AutoGenerateColumns="false" in Gridview and create custom header like in given sample. If you don't want to show header for a particular column then set HeaderText="" in <asp:BoundField ...>
<asp:GridView ID="grdSearch" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField HeaderText="County"
DataField="Prop_County" SortExpression="Prop_County" ItemStyle-Width="70px" HeaderStyle-Height="25px">
</asp:BoundField>
<asp:BoundField HeaderText="Sale Date"
DataField="Prop_Sale_Date" ItemStyle-Width="55px"></asp:BoundField>
<asp:BoundField HeaderText="Sale Time"
DataField="Prop_Sale_Time" ItemStyle-Width="55px"></asp:BoundField>
<asp:BoundField HeaderText="Bid Amount"
DataField="Prop_Bid_Amnt" ItemStyle-Width="100px"></asp:BoundField>
</Columns>
</asp:GridView>

How to display data from gridview

I simply added grid view and added columns and gave headertext
But when i run the application i am not able to see any grid,,atleast i should see grid column names
Do i need to do any thing more
Verify that you have everything wired up properly and are assigning a DataSource and doing a DataBind(). Once you have verified that these two things are happening then make sure that your DataSource is returning some type of result set with at least one item.
A GridView will not display anything at all unless there is at least 1 item in the result set. If you bind to a DataSet or some type of object list and there are not items in it then the grid will no display at all. Not even the headers. In this case you should setup the EmptyDataText property to display something.
If not if this helps, please post your GridView markup and the code where you bind your grid and I'll see if I can figure out what the issue is.
check aspx page code
<asp:MyGridView runat="server" DataKeyNames="pkey" AutoUpdateAfterCallBack="true"
Width="100%"
ID="grduser" AutoGenerateColumns="False">
<Columns>
<asp:BoundField HeaderText="Sr No." DataField="rownumber" ReadOnly="true" HeaderStyle-Width="10px"
ItemStyle-Width="10px" />
<asp:BoundField HeaderText="FirstName" DataField="FirstName" SortExpression="FirstName"
ReadOnly="true" HeaderStyle-Width="120px" ItemStyle-Width="120px" />
<asp:BoundField HeaderText="LoginName" DataField="LoginName" SortExpression="LoginName"
ReadOnly="true" HeaderStyle-Width="120px" ItemStyle-Width="120px" />
<asp:BoundField HeaderText="Email" DataField="Email" SortExpression="Email" ReadOnly="true"
HeaderStyle-Width="120px" ItemStyle-Width="120px" />
<asp:BoundField HeaderText="Role" DataField="Role" SortExpression="Role" ReadOnly="true"
HeaderStyle-Width="30px" ItemStyle-Width="30px" />
<asp:BoundField HeaderText="Reportingto" DataField="Reportingto" SortExpression="Reportingto"
ReadOnly="true" HeaderStyle-Width="120px" ItemStyle-Width="120px" />
<asp:BoundField HeaderText="MobileNo" DataField="MobileNo" SortExpression="Mobile_no"
ReadOnly="true" HeaderStyle-Width="30px" ItemStyle-Width="30px" />
</Columns>
</asp:MyGridView>
Cs file code to bind grid
DataSet ds = new DataSet();
ds = //get dataset form the database
DataView dv = new DataView(ds.Tables[0]);
this.grduser.DataSource = dv;
this.grdusers.DataBind();
have a look on http://msdn.microsoft.com/en-us/library/ms972948.aspx
Easiest way is as Kelsey says:
<emptydatatemplate>
No Data Found.
</emptydatatemplate>
Other techniques:
1) Override the CreateChildControls (example: http://forums.asp.net/t/1003306.aspx)
2) Manually insert a row (example: http://geekswithblogs.net/dotNETvinz/archive/2009/03/11/tiptrick-show-header-and-footer-of-gridview-when-no-data.aspx)

Adding a validator to the gridview textbox, created in edit-mode of a bound field

take a look at this sample code: (question bellow)
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" DataSourceID="SqlDataSource2"
AutoGenerateColumns="False" onrowupdated="GridView1_RowUpdated"
DataKeyNames="Product_Id">
<Columns>
<asp:ImageField DataImageUrlField="Image_Name" HeaderText="Image_Name"
ReadOnly="True" >
<ItemStyle Width="50px" Height="50px" Wrap="true"/>
</asp:ImageField>
<asp:BoundField DataField="Product_Id" HeaderText="Product_Id"
InsertVisible="False" ReadOnly="True" SortExpression="Product_Id">
</asp:BoundField>
<asp:BoundField DataField="Product_Name" HeaderText="Product_Name"
SortExpression="Product_Name" />
<asp:BoundField DataField="Category_Name" HeaderText="Category_Name"
SortExpression="Category_Name" ReadOnly="true" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:BoundField DataField="Size" HeaderText="Size"
SortExpression="Size" />
<asp:BoundField DataField="Price" HeaderText="Price"
SortExpression="Price" />
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
Assume I initialize an SqlDataSource, add a parameter and so on.
The thing is, that when a user clicks edit we get a textbox to edit the colnumn value.
I want to validate the data enter by the user before the update is performed and the new
data is propagated back to the server.How?
10x a lot!
You need to convert the BoundField into a TemplateField. Then you can add a validator to the actual TextBox control.
Option 1:
But from the UNKNOWN answer, the Microsoft recommends the same.. as he told.
ref: http://msdn.microsoft.com/en-us/library/bb426882.aspx#aspnett19_vldcntredtuics_topic2
Option 2:
But, we can do.
You need to add the validation either the javascript validation or server side validation
control, when the GridView's DataBound event is happening at runtime on the particular
TableCell of the Gridview rows.
Hence, when you click the update button that custom generated javascript or the validation
control will check for the validation on editing the values.
This process is more harder than the converting boundfield to templatefield
refer: http://www.aspdotnetcodes.com/GridView_Dynamic_Validation.aspx
Option 3:
And you can go for server side validation on the values instead of client side validation:
refer: http://msdn.microsoft.com/en-us/library/bb332383.aspx

Resources