Link to home
Start Free TrialLog in
Avatar of WorknHardr
WorknHardr

asked on

Jquery ASP Gridview Loop Rows Exclude Specific Columns?

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.
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

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

ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WorknHardr
WorknHardr

ASKER

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

Give us a link to the fiddle and tell us what's wrong.
The code I showed you colours all the rows red except the last one, as per your original question.
>> except the last row
>> except the first 3 columns.
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
I must have VS2010 debug issues, works in js fiddle...
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 :)
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);
        }
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
Ok, its working now. Nuts, I had to reboot my dev pc, oh well, thanks for ALL your help... thx
Excellent, thx again...