Solved

How to loop through all columns in RowDataBound

Posted on 2013-11-01
7
1,088 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

815 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