How to work with templatefield checkboxes in GridView?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • idan
    New Member
    • Dec 2009
    • 1

    How to work with templatefield checkboxes in GridView?

    Here's my data: (Table: EmployeePlan)
    Code:
    EmpID     Plan      Status
    abc123    PlanA     A
    abc123    PlanB     D
    abc123    PlanC     A
    xyz789    PlanA     D
    xyz789    PlanB     D
    xyz789    PlanC     A
    ...
    where Status "A" means an employee subscribed to a certain plan and "D" otherwise.

    I have created a GridView in ASP.Net, normalising the data by the query:
    Code:
    select EmpID,
    sum(case when Plan='PlanA' and Status='A' then 1 else 0 end) as PlanA,
    sum(case when Plan='PlanB' and Status='A' then 1 else 0 end) as PlanB,
    sum(case when Plan='PlanC' and Status='A' then 1 else 0 end) as PlanC
    from EmployeePlan
    group by EmpID
    and the GridView looks like this:
    Code:
    Employee ID    Plan A        Plan B        Plan C
    abc123            x                _                x
    xyz789            _                _                x
    where "x" is a checked checkbox and "_" is a non-checked checkbox

    the checkbox is preset in templatefield by evaluating it (1 = true, 0 = false)

    My problems are:

    1. How to get the checkbox value from the GridView? (I got the object reference not set error)

    2. When a user click the Update button, how to override the UpdateCommand by updating 3 rows instead of updating a single row?
    (Update EmployeePlan set Status = @Status where EmpID = @EmpID)

    Thanks in advance.
    Last edited by Frinavale; Dec 23 '09, 02:21 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Answer for question 1)
    You use the FindControl method for the GridViewRow that contains the checkbox in order to retrieve the checkbox that you want to work with.

    Answer for question 2)
    You need to implement a method that handles the RowUpdating Event.

    All the information you need on the GridView and any other .NET controls can be found in the MSDN Library. This resource should get you pointed in the right direction....th ere are multiple articles on the GridView there. I recommend bookmarking the MSDN Library and using it as your primary resource when developing in .NET.


    -Frinny

    Comment

    Working...