Solved

Jquery ASP Gridview Loop Rows Exclude Specific Columns?

Posted on 2014-02-04
13
1,027 Views
Last Modified: 2014-02-05
I have ASP Gridview I need to loop through all rows except the last row and all columns except the first 3 columns. The dynamic column count will vary from 10 to 15.
0
Comment
Question by:WorknHardr
  • 6
  • 6
13 Comments
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
Can you post some code of what you have so far?

With Jquery it can be as simple as
<!doctype html>
<html>
<head>
<title>Test</title>
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
$(function() {
  var rows = $('#grid tr');
  for(i=0;i<rows.length-1;i++) {
    var cols = $('#grid tr:eq(' + i + ') td');
    for(j=3;j<cols.length;j++) {
      var cell = $('#grid tr:eq(' + i + ') td:eq(' + j + ')');
      $('#result').append(cell.html());
    }
  }
});

</script>
<style type="text/css">
</style>
</head>
<body>
<table id="grid">
  <tr>
    <td>Row1 Col 1</td>
    <td>Row1 Col 2</td>
    <td>Row1 Col 3</td>
    <td>Row1 Col 4</td>
    <td>Row1 Col 5</td>
  </tr>
  <tr>
    <td>Row2 Col 1</td>
    <td>Row2 Col 2</td>
    <td>Row2 Col 3</td>
    <td>Row2 Col 4</td>
    <td>Row2 Col 5</td>
  </tr>
  <tr>
    <td>Row3 Col 1</td>
    <td>Row3 Col 2</td>
    <td>Row3 Col 3</td>
    <td>Row3 Col 4</td>
    <td>Row3 Col 5</td>
  </tr>
  <tr>
    <td>Row4 Col 1</td>
    <td>Row4 Col 2</td>
    <td>Row4 Col 3</td>
    <td>Row4 Col 4</td>
    <td>Row4 Col 5</td>
  </tr>
</table>
<div id="result"></div>
</body>
</html>

Open in new window

0
 
LVL 42

Accepted Solution

by:
Chris Stanyon earned 405 total points
Comment Utility
Have a look at this:

//loop through each of the Table Rows except the last
$('tr:not(:last-child)').each(function() {
    $(this).addClass('red')

    //loop through each of the TDs, except the first 3 - gt (greater than) is zero-based, so 2 is third
    $('td:gt(2)', this ).each(function() {
        $(this).addClass('white');
    });
});

Open in new window

Here's a working demo : http://jsfiddle.net/ChrisStanyon/H5vjR/
0
 

Author Comment

by:WorknHardr
Comment Utility
I placed my table in jsfiddle and get the top two rows as red. Must be the TH tags?

<table cellspacing="0" rules="all" border="1" id="GridView1" ">
<tr align="center" style="font-family:Arial;font-size:12px;">
<th scope="col">State</th>
<th scope="col">List Name</th>
<th scope="col">EffDate</th>
<th scope="col">Jones-Bob</th><th scope="col">&nbsp;</th><th scope="col">Smith-Jim</th><th scope="col">
        Doe-Jane</th>
		</tr><tr align="center" style="font-family:Arial;font-size:10px;">
			<td>AK</td><td>List 1</td><td>7/31/2012</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>
            &nbsp;</td>
		</tr><tr align="center" style="font-family:Arial;font-size:10px;">
			<td>AK</td><td>List 1b</td><td>7/26/2013</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>
            Not Priced</td>
		</tr><tr align="center" style="background-color:Gainsboro;font-family:Arial;font-size:10px;">
			<td>AL</td><td>List 2</td><td>7/1/2012</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>$90.00</td>
		</tr><tr align="center" style="background-color:Gainsboro;font-family:Arial;font-size:10px;">
			<td>AL</td><td>List 2b</td><td>11/15/2013</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>$101.00</td>
		</tr><tr align="center" style="font-family:Arial;font-size:10px;">
			<td>FL</td><td>List 3</td><td>4/1/2013</td><td>&nbsp;</td><td>&nbsp;</td><td>$20.00</td><td>$92.00</td>
		</tr>
	</table>

Open in new window

0
 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
Give us a link to the fiddle and tell us what's wrong.
0
 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
The code I showed you colours all the rows red except the last one, as per your original question.
0
 

Author Comment

by:WorknHardr
Comment Utility
>> except the last row
>> except the first 3 columns.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
The code I provided will loop through all the rows except the last one, and for each row it loops through, it will loop through all the TDs except the first 3. That's exactly what you asked for so I'm struggling to see what the problem is.

Can you be a little clearer on what you need.

I've updated the fiddle
0
 

Author Comment

by:WorknHardr
Comment Utility
I must have VS2010 debug issues, works in js fiddle...
0
 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
In the HTML you posted, you had the row background colour set inline (style="background-color:Gainsboro;") so adding a class that set the background colour will get ignored - inline styles take precedence over class styles, so although the .red class was being added to each of the rows, it had no effect on the last 2.

The fiddle example I've just updated has removed that styling and instead of adding the class to the rows, it now only adds it to the cells.

Maybe why you had a problem with your version :)
0
 

Author Comment

by:WorknHardr
Comment Utility
I have dynamic columns built in the code-behind like so. It seems the HtmlTextWriter is do some auto-formatting with the row.Backcolor.... Help


 protected override void Render(HtmlTextWriter writer)
        {
            if (GridView1.Rows.Count != 0)
            {
                Table gridView = (Table)GridView1.Controls[0];

                foreach (GridViewRow row in GridView1.Rows)
                {
                    row.Cells[6].Text = Convert.ToDateTime(row.Cells[6].Text).ToShortDateString();

                    int rowIndex = gridView.Rows.GetRowIndex(row);

                    if ((rowIndex - 1) % 4 > 1) //Every 2 rows white
                    {
                        row.BackColor = System.Drawing.Color.Gainsboro;
                    }
                }
            }
            base.Render(writer);
        }
0
 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
Help with what?

You clearly have a line in there that sets the background colour of a row (TR). If you don't want it, remove it (row.BackColor = System.Drawing.Color.Gainsboro;)

If you want to override the colour for the selected TDs then just add the background colour to the TDs, and not the TRs.

Can you explain in clear terms exactly what you want. I've shown you the code to select all rows except the last, and all cells except the first 3 as you requested. You haven't at any point told us what you intend to do with the selected cells.

If you want specific answers, you're going to have to ask specific questions
0
 

Author Comment

by:WorknHardr
Comment Utility
Ok, its working now. Nuts, I had to reboot my dev pc, oh well, thanks for ALL your help... thx
0
 

Author Closing Comment

by:WorknHardr
Comment Utility
Excellent, thx again...
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

771 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

11 Experts available now in Live!

Get 1:1 Help Now