Solved

How to loop through all columns in RowDataBound

Posted on 2013-11-01
7
1,041 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

7 Experts available now in Live!

Get 1:1 Help Now