Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hi everybody,
 
My GridView is look like this..
 
col1    col2     col3 
1        A        1.2
2        B        3.4
         C        2.3
3        A        2
 
 
Here col1 is PK column now i'm doing checkBox selection for each and every row of gridview.
 
javascript:
function CheckAll(oCheckbox) 
{
    var gdDocument = document.getElementById("<%=gdDocument.ClientID %>");
 
    for (i = 1; i < gdDocument.rows.length; i++) {
               gdDocument.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked = oCheckbox.checked;
    }
 
}
 
code behind:
 
<asp:TemplateField>
      <HeaderTemplate>
             <input id="chkSelect" type="checkbox" onclick="CheckAll(this)"                runat="server" />
      </HeaderTemplate>
       <ItemTemplate>
           <asp:CheckBox ID="chkSelect" runat="server" />
       </ItemTemplate>
</asp:TemplateField>
 
this code for gridview all rows selection. But i want based upon PK column i want to display the checkBox.
 
ex:
 
in row 3 there is no record for col1. so, i don't want to display checkbox in that row .
 

How to do this can any one help me...
Posted 29-Jan-13 2:15am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

After Binding the grid to that data u cn use following code to achieve ur task
 
for (int i = 0; i < GridView1.Rows.Count; i++)
       {
           if (GridView1.Rows[i].Cells[0].Text == string.Empty)
           {
               CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("chkSelect");
               chk.Visible = false;
           }
       }
 
Enjoy coding ...
have fun Smile | :)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Using jQuery you can easily remove the checkbox. ASP.NET Gridview generating mark up as HTML table in the browser. By using that html table id you can loop through all rows and cols like below using jquery.
 
$('table#yourTable tr').each(function(){
  var isEmpty = true;
  // Process every column
  $(this).children('td').each(function(){
    // If data is present inside of a given column let the row know
    if($.trim($(this).html()) == '') {
     $('input:checkbox').remove();
    }
  });
  // If the whole row is empty remove it from the dom

});
  Permalink  
Comments
JemiM at 29-Jan-13 8:07am
   
By using jQuery and Javascript you can easily manipulate the DOM(each element in your page) elements. If you are a webdeveloper you should study jQuery and Javascript. So you can avoid a lot of server side code that you have written inside the code behind.that increase your web application performance because the client script run the browser itself.
JemiM at 29-Jan-13 8:11am
   
http://jquery.com/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 BillWoodruff 85
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 29 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100