Solved

How to loop through all columns in RowDataBound

Posted on 2013-11-01
7
1,111 Views
Last Modified: 2014-01-27
I am using C# in  my code behind, with 3.5 environment.  

I have  a gridview with a Select command.  The gridview columns are generated by code, but the columns may change depending on which table is selected.
I have date and date/time fields and need to format them when the gridview is populated so the correct data format shows.

I built my RowDataBound function, and am trying to loop through the columns to get the data type, but the only column seen is the CommandField column.  How can I get to the rest of the columns that are in my dataset?

Here is my code:

private void LoadGrid()
        {

            
            string strSQL = "";
            strSQL = " SELECT   RecNum, " + GetAllFields() + " " +
                     " FROM     " + txtTableName.Text.Trim() + 
                     " ORDER BY " + (string)Session["ViewOrder"];

                if (strSQL != "")
                {
                    SqlDataAdapter da = new SqlDataAdapter(strSQL, strConnection);
                    DataSet ds = new DataSet();
                    da.Fill(ds);
           
		    grdvwReview.DataSource = ds.Tables[0];
                    
                    grdvwReview.DataBind();

                    if (ds.Tables[0].Rows.Count <= 0)
                    {
                        lblFeedback.Text = "There are no Records to view at this time.";
                    }
		}
}

 protected void grdvwReview_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            // If it is a date or datetime or time, get the format and format the output accordingly

            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                string l_type = "";
                string strt = "";
                string l_format = "";

                for (int i = 0; i < grdvwReview.Columns.Count; i++)
                {

                        l_type = CheckDataType(grdvwReview.Columns[i].ToString().Trim());

                        if (l_type == "Date")
                        {                        
                            .....
                        }
                        else if (l_type == "Time")
                        {
                           ..... 
                        }
                        else if (l_type == "DateTime")
                        {
				.....
                        }

                    }
                
            }
        }

Open in new window



This is my gridview:

<asp:GridView ID="grdvwReview" runat="server" AllowPaging="True" 
        AllowSorting="True" BackColor="White" BorderColor="#CC9966" BorderStyle="None" 
        BorderWidth="1px" CellPadding="4" 
        PageSize="20" style="text-align: left" 
        Width="95%"  OnRowDataBound="grdvwReview_RowDataBound" onpageindexchanging="grdvwReview_PageIndexChanging" 
        onselectedindexchanging="grdvwReview_SelectedIndexChanging" 
        onsorting="grdvwReview_Sorting">
        <RowStyle BackColor="White" ForeColor="#330099" />
        <Columns>
            <asp:CommandField ShowSelectButton="True">
            <ItemStyle Width="50px" />
            </asp:CommandField>
        </Columns>
        <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
        <PagerSettings Mode="NumericFirstLast" PageButtonCount="5" Position="TopAndBottom" />
        <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Left" />
        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
        <AlternatingRowStyle BackColor="#FFFF99" />
    </asp:GridView>

Open in new window

0
Comment
Question by:ptslv
  • 4
  • 3
7 Comments
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 39617068
You didn't say what format but  you can do it directly in gridview

DataFormatString="{0:MM-dd-yyyy hh:mm tt}" 

Open in new window

0
 

Author Comment

by:ptslv
ID: 39617119
Thanks for the quick response.  That will not work for me.  As I stated above, my columns are generated through the code because I do not know what the columns will be.  So I need to go through the RowDataBound event to format the output.
0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 39617194
Ok sure but what format do you want it to be in?
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:ptslv
ID: 39617215
Date = MMddyyyy
DateTime = MMddyyyy hh:mm
Time = hh:mm or hh:mm:ss

Each is based on 24 hour time and is dependent how the user set the mask.
0
 
LVL 28

Accepted Solution

by:
sammySeltzer earned 500 total points
ID: 39617216
             if (l_type == "Date")
                        {                        
                             myDate.ToString("MM/dd/yyyy");
                        }
                        else if (l_type == "Time")
                        {
                            myDate.ToString("HH:mm:ss");
                        }
                        else if (l_type == "DateTime")
                        {
		 myDate.ToString("MM/dd/yyyy HH:mm:ss");
                        }

Open in new window


You can adjust accordingly
0
 

Author Comment

by:ptslv
ID: 39617231
I asked the wrong question and have a request in to fix it.  The main problem I have is traversing the columns.  After adjusting my code a bit, I am getting empty strings for my columns.

My adjusted code in my RowDataBound event is:

if (e.Row.RowType == DataControlRowType.Header)
 {
     for (int i = 0; i < e.Row.Cells.Count; i++)
     {

       l_type = CheckDataType(e.Row.Cells[i].Text.Trim());
           //..........formating code in here
     }
}

Open in new window

0
 

Author Comment

by:ptslv
ID: 39617252
Sammy,

I would love to test what you sent, but I am unable to get through my loop in the RowDataBound event to see what the columns are.  :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question