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
331 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

832 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