Saturday, April 18, 2009

Add numbering in gridview [VB / C#]

I also have the same problem when i first using the gridview. Gridview is a very powerful control (we do not have to code table like in PHP). So, after doing a research on several web resources, I've found this method. With this method you can add numbering in your gridview. Insert this code in your page inside the gridview codes (.aspx)

VB
--

<Columns>
<asp:TemplateField>
<ItemTemplate>
<%#Container.DataItemIndex + 1 & "."%>
</ItemTemplate>
</asp:TemplateField>
</Columns>



C#
--

<Columns>
<asp:TemplateField>
<ItemTemplate>
<%#Container.DataItemIndex + 1 + "."%>
</ItemTemplate>
</asp:TemplateField>
</Columns>

Get Date and Time separately from SQL

Situation: You have a column in your table type DateTime. But you just want to view the date or time only. This is how it can be done.

SQL query

Select convert(varchar, [your column date], 3)
from [your table name]

will return result in format dd/mm/yy

Select select convert(varchar, [your column date], 8 )
from [your table name]

will return result in format hh:mm:ss

Here are list of format which you can refer:

Date Formats
Format No. SQL Query Output
1 select convert(varchar, [your column date], 1)
from [your table name]
12/30/08
2 select convert(varchar, [your column date], 2)
from [your table name]
08.12.30
3 select convert(varchar, [your column date], 3)
from [your table name]
30/12/08
4 select convert(varchar, [your column date], 4)
from [your table name]
30.12.08
5 select convert(varchar, [your column date], 5)
from [your table name]
30-12-08
6 select convert(varchar, [your column date], 6)
from [your table name]
30 Dec 08
7 select convert(varchar, [your column date], 7)
from [your table name]
Dec 30, 08
10 select convert(varchar, [your column date], 10)
from [your table name]
12-30-08
11 select convert(varchar, [your column date], 11)
from [your table name]
08/12/30
101 select convert(varchar, [your column date], 101)
from [your table name]
12/30/2008
102 select convert(varchar, [your column date], 102)
from [your table name]
2008.12.30
103 select convert(varchar, [your column date], 103)
from [your table name]
30/12/2008
104 select convert(varchar, [your column date], 104)
from [your table name]
30.12.2008
105 select convert(varchar, [your column date], 105)
from [your table name]
30-12-2008
106 select convert(varchar, [your column date], 106)
from [your table name]
30 Dec 2008
107 select convert(varchar, [your column date], 107)
from [your table name]
Dec 30, 2008
110 select convert(varchar, [your column date], 110)
from [your table name]
12-30-2008
111 select convert(varchar, [your column date], 111)
from [your table name]
2008/12/30


Time Formats
Format No. SQL Query Output
8 or 108 select convert(varchar, [your column date], 8 )
from [your table name]
00:40:50
9 or 109 select convert(varchar, [your column date], 9)
from [your table name]
Dec 30 2006 12:40:50:840AM
14 or 114 select convert(varchar, [your column date], 14)
from [your table name]
00:40:50:840

How to use dictionary in [VB / C#]

This is how to declare, add / insert and read / view dictionary in C# and VB .net. But you have include library for reference
VB
'In header
Imports System.Collections.Generic

'In your codes section
Dim items As Dictionary(Of String, String) = New Dictionary(Of String, String)()

items.Add("The Key", "The Value")

For Each item As KeyValuePair(Of String, String) In items
Label1.Text = Label1.Text & "Key: " & item.Key.ToString() & ", Display: " & item.Value.ToString() & "
"

Next


C#
// In header
Using System.Collections.Generic;

// In your codes section
Dictionary<string, string> items = new Dictionary<string, string>();


items.Add("The Key", "The Value");

foreach (KeyValuePair<string, string> item in items)
{
Label1.Text = Label1.Text + "Key: " + item.Key.ToString() + ", Display: " + item.Value.ToString() + "<br>";
}


I prefer using dictionary rather than array. Dictionary is suitable to be used when we know its data type