Solved

How to loop through all columns in RowDataBound

Posted on 2013-11-01
7
1,136 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

697 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