?
Solved

I would like a gridview to show a checkbox as true if there is data in a field

Posted on 2011-03-02
1
Medium Priority
?
335 Views
Last Modified: 2012-05-11
Can someone show me the code to making an ASPX.NET feild to be set to true open loading the data in C#.  I need to check for a feild to be populated then set this to true.  It keeps showing my template field in error.
if (License1 != string.Empty || License1 != null)
        {
           // I change the Item gridview name...
            CBAddLA1.Checked = true;



        }
        else
        {
            CBAddLA1.Checked = false;
        }

When I load the grid I would like to change the check mark to true if there is data in the field.  Otherwise, I want it to be false.  There seems to be no way to grab turn the checkbox on or off before binding the grid.

 <asp:TemplateField HeaderText="Advanced Clinical">
                    <EditItemTemplate>
                        <asp:CheckBox ID="CBAddLA1" runat="server" text='<%# Bind("LicenseAssociation1Default") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="CBAddLA1" runat="server" 
                            Enabled="false" />
                    </ItemTemplate>
                    <ControlStyle CssClass="standard-text" Width="100px" />
                    <FooterStyle CssClass="standard-text" />
                    <HeaderStyle BackColor="#996633" CssClass="standard-text" ForeColor="White" 
                        HorizontalAlign="Left" />
                    <ItemStyle CssClass="standard-text" />
                </asp:TemplateField>

Open in new window

0
Comment
Question by:kimmie8000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 35016958
You didn't say where you are trying to run that code from. But in order to act on row specific data you need to have code that runs at the point that each row of data gets bound to the grid. To do that you need to handle the RowDataBound event for the grid which will give you a context on which to act:

More details and sample:

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx

The sample only shows writing a dumym value to a cell, but in your case I would imaging you want something more like:
  void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  {
      if(!string.e.Row.RowType == DataControlRowType.DataRow)
      {
            DataRowView view = e.Row.DataItem as DataRowView;
            if (view != null)
            {
                  if (!string.IsNullOrEmpty(view["SomeColumn"].ToString()))
                  {
                       CheckBox cb = e.Row.FindControl("CBAddLA1");
                       cb.Checked = true;
                  }
            }
      }
  }

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month8 days, 20 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question