change Date format in VB.Net - asp.net

How to change the Date format in a asp grid displaying date format populated from Database.
The Database stores value in '2009-08-23 10:24:00.000' format but the value in grid is displayed as 'Sun 23-08-2009 10:24:00'
I want to display it as '23-AUG-2008'
VB .Net Code
"Dim dv As DataView1 = ds.Tables(0).DefaultView"
Markup is
<asp:BoundColumn DataField="DateFormat" HeaderText="Day&Date">
<ItemStyle Wrap="False" />
How can i display as '23-AUG-2008'?

You can use DataFormatString property of the BoundColumn. There just specify the desired format.
In your case:
<asp:BoundColumn DataField="DateFormat" DataFormatString="{0:dd-MMM-YYYY}" HeaderText="Day&Date">
Btw, DataGridView is a control from Winforms, not webforms...In webforms it's called GridView. And this has nothing to do with javascript or vb.net, so I've removed the tags as well.. The solution is specific to GridView control, which belongs to asp.net as whole no matter the code-behind language. Yes, you could use javascript to format those string, but...

Related

How to fetch value from datagrid to another form in asp.net

I have a Datagrid on one page, when user clicks on cell of datagrid, it should open another form with labels and textboxes and it should fill it with relevant data fetching from the SQL Database.
I have searched and found out
response.querystring["string"]
But due to its visibility cant use it.
I am pretty new with coding in asp, done some on WINFORM using C#, so I was trying to do through creating an object but it didn't work, so what is the correct way of doing it?
Thanks
I didnt' work with data grid but if it's anythign like gridview then you have an option to use template fields and customize the links for these.
Here is an example template field from grid view that can be customized to suite your needs
<asp:TemplateField HeaderText="Customer Status" SortExpression="IsCustomer">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<a href='yournewpage.aspx?param=<%# Eval("Column_name")) %>' alt="" />
</ItemTemplate>
</asp:TemplateField>

Publish two types of field information on same column in ASP.Net grid view

I want to publish two types of information on same column in ASP grid view.
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:hyperlinkfield text="Detail" datanavigateurlfields="name" datanavigateurlformatstring="LeadInformation.aspx?name={0}" />
The first column is a BoundField and second one is hyperlinkField. These two types of information will display separately on two columns.
or else I can do like this.
<asp:HyperLinkField DataNavigateUrlFields="name" DataNavigateUrlFormatString="LeadInformation.aspx?name={0}"
DataTextField="name" HeaderText="User Name" SortExpression="name" />
Then it will display all information on one column as a hyperlink.
I just want to display name as it is. (not as a hyperlink). And in same column,add a hyperlink like above.
Please help me.
Make it a TemplateField. Then add a Label and a Hyperlink. Then for the Text of the Label and the NavigateURL for the hyperlink use Bind("ColumnName"). (or on the design mode right click and select Databindings and enter Bind("ColumnName") for the properties.
You can refer the below MSDN link which explains how to show two values in a single column (FirstName and LastName in this case)
MSDN
The answer is absolutely correct. A template field allows you to display anything you like in a gridview, rather than the standard ASP:NET gridview column controls. The price you pay is that it then becomes a bit harder to get access to the values in the controls. You will find it's often useful to use the CommandName and CommandArgument properties to get at values of the controls.
You have a fair learning curve ahead of you, I'm afraid, and certainly the question is too broad to be answered here. Try this article, or searching for "ASP.NET gridview templatefield".

ASP.NET 4 GridView - Pulling a Hyperlink out of a Database

I have a GridView bound to a SqlDataSource.
I'm pulling hyperlinks which point to Job Descriptions stored in a separate web space, out of a database and placing them in the GridView.
These are full Hyperlinks such as "Http://stackoverflow.com/"
Originally the GridView column was a simple BoundField like this:
<asp:BoundField DataField="JobDescription" HeaderText="JobDescription"
SortExpression="JobDescription" />
So I started trying to convert it into a hyperlink field.
<asp:HyperLinkField DataNavigateUrlFields="JobDescription"
DataTextField="JobDescription"
HeaderText="JobDescription"
SortExpression="JobDescription"
Target="_blank"
NavigateUrl="{0}" />
This produced the desired result, but I can no longer edit that column in the GridView. When it was a BoundField I could edit the item, but could find no way to make it into a hyperlink.
Either way will work...
I either need the HyperLinkField to be updatable, or I need the BoundField to be formatted as a Hyperlink with what it pulls directly from the database.
I appreciate the help.
Use a Template Field. So your can define your normal view and editing view.
Grrr found the answer:
<asp:BoundField DataField="JobDescription" HeaderText="Job Description"
SortExpression="JobDescription"
DataFormatString="<a target='_blank' href='{0}'>Text</a>"
HtmlEncode="False" />
You don't need a template field. That HtmlEncode property must be set to false in order for html in DataFormatString to be rendered as html, otherwise it changes all of your characters into the equivalent of stuff like...
The Entity Numbers here: http://www.w3schools.com/tags/ref_entities.asp

Building a status calendar - ASP.NET

I need to build a status dashboard that reads Events (ID, Desc, Time) out of a database, and puts a marker on a day that drills down to detail, if there is an event - and just a checkmark if there is no event. Just like http://www.google.com/appsstatus.
I am a Jr. developer witha PHP background, I am using C# & ASP.NET. I chose to use the UpdatePanel/ScriptManager components for the grid even though there is some bandwidth costs.
My question is about the best way to build this table. In PHP, I would use a string and just keep adding HTML to it via nested iteration, like:
foreach($row in $rows){
MyHtml .= "<td>" . $variableFromDB[i] . "</td>";
i++
}
return MyHtml; //Output built HTML
In this case I need to query an array to see if an event exists for that day, if so, generate a link containing URL parameter (field1=value1&field2) so that I can display the corresponding detail information.
Since I'm using .NET, I'm wondering if there is a more elegant way to do this without querying for each cell to check for an event, and an easier way to build a table. I see .NET classes like "HtmlTableCell" & "HtmlTableRow", but I haven't seen any compelling examples that require a query in each cell.
Any guidance from you senior .NET folks would be great...
It sounds like you're wanting a grid or table layout to display your data. If that's the case, and you're using ASP.NET WebForms, consider using a GridView instead of building HTML manually. The advantage of ASP.NET's server controls is the ability to bind a collection of your business objects from your model to your controls.
Suggest you don't follow the HtmlTableCell and HtmlTableRow approach. You can get this job done much quicker by using server controls and data binding.
Here are a few steps in getting started with databinding.
On your .aspx markup page, write an <asp:GridView runat=server id=statusCal>.
Determine the columns that you want to show on your grid. Just like the HTML table you'd have built by hand, what data did you want to show? You'll end up with something like this:
<asp:GridView ID="statusCal" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:BoundField DataField="Date" HeaderText="Status Date" />
<asp:BoundField DataField="Status" HeaderText="Status" />
<asp:BoundField DataField="UserName" HeaderText="User" />
<asp:BoundField DataField="Email" HeaderText="Email" />
</Columns>
</asp:GridView>
Is your data coming from a database? Somehow you'll want a class to hold your status calendar. Wherever you're getting the data from, you'll want to have a class to bind to your grid. Perhaps something like:
public class UserStatusCalendar
{
public string UserName {get;set;}
public string Email {get;set;}
public string Status {get;set;}
public string Date {get;set;}
}
Then it's a matter of:
Creating and populating an array or List<> of your UserStatusCalendar objects.
List<UserStatusCalendar> show = new List<UserStatusCalendar>();
show.Add(new UserStatusCalendar
{ UserName="foo", Email="foo#bar.com",
Status="Good", Date="Jan 1 2010 10:09"});
Bind your collection to the grid.
statusCal.DataSource = show;
statusCal.DataBind();
When navigating to the page, you'll see that your grid shows your data. You won't have had to mess with any lines of HTML markup. You can then dive into fancier tasks for your grid: creating <asp:HyperLink> fields instead of BoundFields, and all the rich easy editing support that the GridView allows you.
Here's a working test application to demonstrate the concepts. Name your page test.aspx.
C# code-behind: http://pastebin.com/DWaxRNmj
ASPX Markup: http://pastebin.com/XwrnGB0D
You'll end up with this grid:

ASP.NET: How to assign ID to a field in DetailsView?

I have a master-detail page, in which I use GridView to display multiple rows of data, and DetailsView + jQuery dialog to display the details of a single records. only one DetailsView is open at a time.
I need to be able to pull out a single field of the open DetailsView, for manipulation using JavaScript. Is there a way to give a unique ID to a given field in DetailsView, so I can use getElementByID? Or is there another way to accomplish what I'm trying to do?
Thank you in advance.
If you are using a bound textbox in a template field in your detailsview you can then select it by:
$("[id$='MyTextBox']");
Which will find the textbox bound to MyFieldName as below.
<asp:DetailsView ID="DetailsView1" runat="server">
<Fields>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="MyTextBox" Text='<%# Bind("MyFieldName")%>' runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
Whatever guff asp.net adds onto the begining of the id won't matter because jQuery $= mean "ends with".
there may be a better way, but when I've needed an id available for js work, I just convert the bound field to a templated field. you can then give the textbox the id of your choice. keep in mind when rendered the id will be expanded with the id of the parent control.

Resources