We help IT Professionals succeed at work.

gridview enable /disable a checkbox

dotnet0824
dotnet0824 asked
on
374 Views
Last Modified: 2013-12-17
Hi,
I have a gridview bound to a dataTable.
I have records as
Checkbox CustomerId   name     Comments
                   1                 John          Hello
                   2                 Patrick      
                    3                 Kim           Hello1
Where ever comment is NULL Or empty, checkbox should be disabled and whenever Comments r not null (in above case 1 and 3) the checkbox should be enabled allowing the user to check those.

Can an example be shown similar to this or code be posted please
Comment
Watch Question

can u provide the aspx of the gridview?
Here I have added a template control with check box as the first column.
Then in the rowdatabound event put the below code
protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
 
    {
 
        if (e.Row.Cells[2].Text.ToString()=="")
 
        { 
 
        CheckBox chk= (CheckBox) e.Row.Cells[0].FindControl("chk1");
 
        chk.Enabled = false;  
 
        
 
        }
 
    }

Open in new window

Author

Commented:
here is the aspx

 <asp:GridView
            ID="CustomerGrid" runat="server" DataKeyNames="Customer_Id"
            Width="100%"
            CssClass="standardTable"
            RowStyle-CssClass="row1" AlternatingRowStyle-CssClass="row2"
            AutoGenerateColumns="false"
            OnRowDataBound="CustomerGridItems_RowDataBound">
        <Columns>
         <asp:BoundField  DataField="Customer_ID" HeaderText="Customer"
         InsertVisible="False"  ReadOnly="True" />

         <asp:TemplateField>
             <ItemTemplate>
               <asp:CheckBox ID="chkSelect"  runat="server" OnCheckedChanged="chkStatus_OnCheckedChanged"/>
            </ItemTemplate>
         </asp:TemplateField>
        <asp:BoundField HeaderText="Customer" DataField="Customer" />
        <asp:BoundField HeaderText="First Name" DataField="FName" />
        <asp:BoundField HeaderText="Last Name" DataField="LName" />
        <asp:BoundField HeaderText="Status" DataField="Response" />
       <asp:HyperLinkField
        DataNavigateUrlFields="CustomerID"
        DataNavigateUrlFormatString="CustomerA.aspx?ID={0}"
        DataTextField="Message"
        HeaderText="Message"/>                    
        </Columns>
        </asp:GridView>

Author

Commented:
hi spprivate,
Can u post your aspx Grid code

Author

Commented:
i tried but it doesnot work
 DataNavigateUrlFormatString="CustomerA.aspx?ID={0}"
        DataTextField="Message"
        HeaderText="Message"/>           (This is the field "Message" which has data in it coming from DB)

for each row it has to look if message exists and toggle the checkbox on or Off
J K

Commented:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataGridView1.AllowUserToAddRows=False
Dim dt As New DataTable
        dt.Columns.Add("CustomerId")
        dt.Columns.Add("name")
        dt.Columns.Add("Comments")
        dt.Rows.Add()
        dt.Rows(0)(0) = 1
        dt.Rows(0)(1) = "John"
        dt.Rows(0)(2) = "Hello"
        dt.Rows.Add()
        dt.Rows(1)(0) = 2
        dt.Rows(1)(1) = "Patrick"
        dt.Rows(1)(2) = ""
        dt.Rows.Add()
        dt.Rows(2)(0) = 3
        dt.Rows(2)(1) = "Kim"
        dt.Rows(2)(2) = "Hello"
Dim col As New DataGridViewCheckBoxColumn
        DataGridView1.Columns.Add(col)
        DataGridView1.DataSource = dt
End sub
Private Sub DataGridView1_DataSourceChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.DataSourceChanged
        For i As Int32 = 0 To DataGridView1.Rows.Count - 1
            If DataGridView1.Rows(i).Cells(3).Value.ToString = "" Then
                DataGridView1.Rows(i).Cells(0).ReadOnly = True
            End If
        Next
    End Sub
ROMA CHAUHANProject Lead
CERTIFIED EXPERT

Commented:
where is comment column in grid view in the code which you have posted???
J K

Commented:
fourth column of DataGridview1 and third column of dt is the commencolumn.

DataGridView1.Rows(i).Cells(3).Value.ToString  gives the value of the cell having comment
ROMA CHAUHANProject Lead
CERTIFIED EXPERT

Commented:
sorry jalpa_144 i m not asking to you.
i m asking to dotnet0824. In the very first post he said that ha had CustomerId   name     Comments  fields but in the source of grid view there is not any column named comment.

dotnet0824 can u please explain what is your problem, what you want to do?/
In your case the check box name is chkselect.Change that in my sample code.Also
As roma asked,there is no comment column in the grid.So on what basis do you want to check this condition then.
protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
 
    {
 
        if (e.Row.Cells[2].Text.ToString()=="")
 
        { 
 
        CheckBox chk= (CheckBox) e.Row.Cells[0].FindControl("chkSelect");
 
        chk.Enabled = false;  
 
        
 
        }
 
    }

Open in new window

No SpPrivate, i tried ur code.. it dint work for me 2.
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
ok,sorry for jumping ahead little.Just realized that if you have empty data in database it might return "&nbsp;" instead of empty string.So put a break point and check what is coming from the db when it is empty(comment column).I made a slight change in the code
  if (e.Row.Cells[2].Text.ToString()=="&nbsp;")
        { 
        CheckBox chk= (CheckBox) e.Row.Cells[0].FindControl("chk1");
            if(chk!=null) chk.Enabled = false;  
        
        }

Open in new window

oops,EE editor didnt accept my nbsp check.I have put it in a file attached.
ee.txt

Author

Commented:
thanks
Im Sorry spprivate, if i sounded rude.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.