Accessing GridView Items programmatically

June 30, 2008 by vijeyashobana

A gridview, like we all programmers know, is heavy and used for editing, paging and sorting. GridView can be accessed declaratively(in the Markup section) or programmatically(in code-behind). It is upto the developer’s choice where he wants to access Gridview from.

I prefer programmatic access, i.e, accessing the gridview from code-behind. To access the gridview control programmatically, you can see many articles, tips, tricks online. Here we are going to see a simple technique to access a gridview.

Important points to remember:

1. a gridview is made up of textboxes, meaning if a gridview row has 5 columns, it means that gridview row has 5 textboxes in it.

2. every row of gridview can be accessed as a GridViewRow thus:

GridViewRow gvr = GridView1.Rows[idx];

3. every column within a GridViewRow is accessed as a Cell thus:

int n = gvr.Cells.Count; //gives the number of cells in a gridview row.

4. To access the value stored within any cell,

TextBox t = new TextBox(); //dynamically creating a new textbox just to hold the Item’s value

t.text  = gvr.Cells[idx].Text; //our new textBox holds the specific gridview cell’s value.

5. If you have set Select, Edit, Delete buttons on the gridview, gridview counts each of these as a cell and returns ” ” value. This is not what we usually expect. We expect the item’s value. So we should have the statement as,

t.text  = gvr.Cells[3].Text;  // Select[idx=0], Edit[idx=1], Delete[idx =2], required Item[idx=3]

SPLIT DATETIME IN SQL SERVER

June 29, 2008 by vijeyashobana

SQL statement:

SELECT HIRE_DATE FROM EMPLOYEE

1992-08-27 00:00:00.000

Now, I need 1992-08-27 only. To split this date from the result date use this:

SELECT CONVERT(VARCHAR(11),EHIRE_DATE,106) FROM EMPLOYEE.

I get

27 Aug 1992.

Depending on the requirements, I might need the date in different formats. SQL Server provides with different options to obtain desired results:

–MM/DD/YYYY
SELECT CONVERT(VARCHAR(10),EMP.HIRE_DATE,101) FROM EMPLOYEE EMP

–YYYY.MM.DD
SELECT CONVERT(VARCHAR(10),EMP.HIRE_DATE,102) FROM EMPLOYEE EMP

–DD/MM/YYYY

SELECT CONVERT(VARCHAR(10),EMP.HIRE_DATE,103) FROM EMPLOYEE EMP

–DD.MM.YYYY
SELECT CONVERT(VARCHAR(10),EMP.HIRE_DATE,104) FROM EMPLOYEE EMP

–DD-MM-YYYY
SELECT CONVERT(VARCHAR(10),EMP.HIRE_DATE,105) FROM EMPLOYEE EMP

–DD MON-YYYY
SELECT CONVERT(VARCHAR(11),EMP.HIRE_DATE,106) FROM EMPLOYEE EMP

–MON DD, YYYY
SELECT CONVERT(VARCHAR(12),EMP.HIRE_DATE,107) FROM EMPLOYEE EMP

–MM-DD-YYYY

SELECT CONVERT(VARCHAR(10),EMP.HIRE_DATE,110) FROM EMPLOYEE EMP

–YYYY/MM/DD
SELECT CONVERT(VARCHAR(10),EMP.HIRE_DATE,111) FROM EMPLOYEE EMP

Reference:

www.dotnetspider.com/forum/59261-how-retrieve-date-part-from-datetime-datetype-SQL-Server.aspx

Checking for System.DBNull

June 29, 2008 by vijeyashobana

Say the result of a SELECT statement gives an empty column. By this I mean, the column has no values in it. And say this column is of type ‘decimal’, and if I want the column to display 0.0 instead of empty cells, the best option is to check if the column has values of type System.DBNull. This is because in this particular situation, I cannot check for ‘null’, or ‘NULL’. So the code goes thus:

if (DBNull.Value.Equals(ds.Tables[0].Rows[0][2]))
ds.Tables[0].Rows[0][2] = 0.0;

courtesy: http://forums.msdn.microsoft.com/en-US/csharpgeneral/thread/750319d5-da21-4fa2-94f0-58e0dff745c7

XmlDataSource

June 28, 2008 by vijeyashobana

When using XmlDataSource, it’s imperative to give an .xslt file to transform, and XPath expression

if the required value is an attribute of the xml file, well and good. simply use the xslt file.

if the required value is an element of the xml file, transform the xml file such that the required value is an attribute. Do this transformation inside the xslt file.

For further information, check this link:http://weblogs.asp.net/rajbk/pages/431322.aspx

VWD 2008 EE Toolbox Standard controls missing

June 28, 2008 by vijeyashobana

In VWD 2008 EE, sometimes the toolbox controls may not be available. By this I mean, say, Data controls or Standard controls may not be available in Toolbox. One of the easiest ways to obtain all the default controls in the Toolbox is

Tools->Import/Export Settings->Reset Settings.

Even if you have added .dll control files using Choose Toolbox Items, they will not be removed.