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
330 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
1 Comment
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now