Click here to Skip to main content
15,886,019 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
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:

C#
<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

After Binding the grid to that data u cn use following code to achieve ur task

C#
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 :)
 
Share this answer
 
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.

JavaScript
$('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

});
 
Share this answer
 
Comments
[no name] 29-Jan-13 7:45am    
sorry yar, i don't no about jquery if you don't mine could you please explain in C#,ASP,javascript...
Jameel VM 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.
Jameel VM 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900