How to in VB

C

ckerns

I want to use this to format the date field in a GridView control.
How would I do this in vb?


-- aspx page --

<asp:GridView runat="server" ID="gridView"
AutoGenerateColumns="true"
OnRowDataBound="gridView_RowDataBound">

-- end aspx page --


-- code beside --

protected void gridView_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow row = ((DataRowView)e.Row.DataItem).Row;

int index = GetDateColumnIndex(row);

e.Row.Cells[index].Text = ((DateTime)
row[index]).ToString("dd/MM/yyyyyyyy-MM-dd");
}
}

private int dateColumnIndex = -1;
private int GetDateColumnIndex(DataRow row)
{
if (this.dateColumnIndex == -1)
{
this.dateColumnIndex =
row.Table.Columns.IndexOf("ItemRecieved");

if (dateColumnIndex < 0)
{
throw new Exception(
"datasource does not contain the
MyDateColumn column");
}
}
return this.dateColumnIndex;
}

-- end code beside --
 
M

Mike

go to the properties of the Gridview, go to the column you want the date to
be formatted and enter in:
{0:mm/dd/yyyy}

and change htmlencode = true to htmlEncode = false and your date column will
be formatted and done in the HTML not the code behind the the language
doesn't matter.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top