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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 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

19 Experts available now in Live!

Get 1:1 Help Now