Solved

How to loop through all columns in RowDataBound

Posted on 2013-11-01
7
1,073 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

910 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now