Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Working with GridView in asp.net

Posted on 2014-01-14
3
Medium Priority
?
489 Views
Last Modified: 2014-02-10
I have a gridview control. Within this there are is a EditTemplate that contains a textbox called "txtDescribeDMG". When I click the Edit link to put the gridview into EditMode I would like to turn the textbox blue (if certain conditions are true) .

 I keep getting Object reference not set to an instance of an object”. error. I am assuming it is because the textbox does not exist at that point in time. My question is how would I achieve this then? Code is below. Any insight would be most appreciated.

  <asp:GridView ID="gvStolenUnits" runat="server" onrowediting="gvStolenUnits_RowEditing" AutoGenerateColumns="False" Width="800px"    OnRowCommand="gvStolenUnits_OnRowCommand" OnRowDataBound="gvStolenUnits_OnRowDataBound" DataSourceID="SqlDataSourcegvStolenUnits"  BackColor="#EEEEEE" DataKeyNames="SeqNo" BorderStyle="Solid" BorderWidth="1px"  BorderColor="#000000" AllowPaging="False"  SelectedRowStyle-BackColor="AliceBlue">
                                       <RowStyle  BorderStyle="Solid" BorderWidth="1px"   CssClass="UnitsRowStyle" BackColor="White" />
                                            <HeaderStyle CssClass="UnitsHeaderStyle" />
                                            <SelectedRowStyle BackColor="Cornsilk" BorderColor="#990000" BorderStyle="Solid" BorderWidth="2px"  CssClass="UnitsRowStyle" />
                                          
                                            <EmptyDataTemplate>
                                             
                                           </EmptyDataTemplate >
                                        
 <Columns>
<asp:CommandField    runat="server"     EditText="Edit"  CancelText="Cancel" ShowCancelButton="true" ShowDeleteButton="false"  ShowEditButton="true" >
                                                     
 <HeaderStyle  HorizontalAlign="Center"  CssClass="UnitsHeaderStyle"  />
 <ItemStyle  HorizontalAlign="Center" VerticalAlign="Top" wrap="false" width="54px" CssClass="UnitsRowStyle"   />
 </asp:CommandField>

 <asp:BoundField HeaderText="SeqNo"   DataField="SeqNo" ReadOnly="true" Visible="true"  SortExpression="SeqNo" ItemStyle-Width="10px"></asp:BoundField>

<asp:BoundField HeaderText="Unit"  DataField="Unit" ReadOnly="true" SortExpression="Unit" ItemStyle-Width="20px"></asp:BoundField>

<asp:BoundField HeaderText="Make"  DataField="Make" ReadOnly="true"  SortExpression="Make" ItemStyle-Width="20px"></asp:BoundField>
                                                   <asp:BoundField HeaderText="Model"  DataField="Model" ReadOnly="true"  SortExpression="Model" ItemStyle-Width="20px"></asp:BoundField>
                       
<asp:TemplateField HeaderText="Recovered?" ItemStyle-Width="20px">
 <ItemTemplate><%# Eval("Recovered")%></ItemTemplate>
  <EditItemTemplate>
    <asp:DropDownList ID="ddlIsUnitRecovered"  selectedvalue='<%# Bind("Recovered") %>' OnSelectedIndexChanged="ddlIsUnitRecovered_OnSelectedIndexChanged" runat="server" AutoPostBack="True" >
                                    <asp:ListItem Text="Please Select" Value="" Selected="True"></asp:ListItem>
                                                                <asp:ListItem Text="Yes" Value="Y"></asp:ListItem>
                                                                <asp:ListItem Text="No" Value="N"></asp:ListItem>
                                                        </asp:DropDownList>
                                                     
                                                        </EditItemTemplate>
                                                </asp:TemplateField>
                                               
                                                <asp:TemplateField HeaderText="Is Unit Damaged?" ItemStyle-Width="20px">
                                                       <ItemTemplate><%# Eval("IsUnitDamaged")%></ItemTemplate>
                                                       <EditItemTemplate>
                                                      
                                                            <asp:DropDownList ID="ddlInsertIsUnitDamagedTheft" selectedvalue='<%# Bind("IsUnitDamaged") %>' OnSelectedIndexChanged="ddlInsertIsUnitDamagedTheft_OnSelectedIndexChanged" runat="server" AutoPostBack="True"  runat="server">
                                                               <asp:ListItem Text="Please select" Value="" Selected="True"></asp:ListItem>
                                                               <asp:ListItem Text="Yes" Value="Y"></asp:ListItem>
                                                               <asp:ListItem Text="No" Value="N"></asp:ListItem>
                                                            </asp:DropDownList>
                                                       
                                                     
                                                        </EditItemTemplate>
                                                </asp:TemplateField>
                                               
                                             <asp:TemplateField HeaderText="Describe Damage">
                                                       <ItemTemplate><%# Eval("Damage")%></ItemTemplate>
                                                       <EditItemTemplate>
                                                      
                                                            <asp:TextBox ID="txtDescribeDMG" Text='<%# Bind("Damage") %>' runat="server" Width="100%"></asp:TextBox>
                                                       
                                                     
                                                        </EditItemTemplate>
                                                </asp:TemplateField>

                                              <asp:BoundField HeaderText="UnitID"  DataField="UnitID" ReadOnly="true"  SortExpression="UnitID" ItemStyle-Width="20px" Visible="true"></asp:BoundField>
                                              
                                            </Columns>       

                    </asp:GridView>  

Open in new window


      protected void gvStolenUnits_RowEditing(object sender, GridViewEditEventArgs e)
        {
            // Get the currently selected row using the SelectedRow property.

               int index = Convert.ToInt32(e.NewEditIndex);
               GridViewRow row = gvStolenUnits.Rows[index];
               ClaimInfo ci = new ClaimInfo();
               int unitid = Convert.ToInt32(row.Cells[8].Text);
               DataSet ds = ci.GetSelectedUnit(claimid, unitid);
               string rec = ds.Tables[0].Rows[0]["Recovered"].ToString();
               string isunitdmg = ds.Tables[0].Rows[0]["IsUnitDamaged"].ToString();

      ///THIS IS WHERE IT FAILS !!!!!!
          TextBox txtDDMG = (TextBox)row.FindControl("txtDescribeDMG"); 


               if (rec == "Y" && isunitdmg == "Y")
               {
                   txtDDMG.ForeColor = Color.Blue;
                   txtDDMG.ReadOnly = false;


               }



        }

Open in new window

0
Comment
Question by:jazzcatone
[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
3 Comments
 
LVL 27

Accepted Solution

by:
Shaun Kline earned 1000 total points
ID: 39780178
You can use the RowCreated or RowDataBound events to perform this action. Compare the e.Row.RowIndex to the GridView.EditIndex so that your code only run your code if the bound/created row is the row being edited.
0
 
LVL 2

Assisted Solution

by:Aijaz Chauhan
Aijaz Chauhan earned 1000 total points
ID: 39793471
Use Row Data bound event of grid view  

protected void gvStolenUnits_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
       if (e.Row.RowState == DataControlRowState.Edit)
       {
           TextBox txtDDMG = (TextBox)e.Row.FindControl("txtDDMG ");
           if( txtDDMG  != null)
           {
              txtDDMG.ForeColor = Color.Blue;
              txtDDMG.ReadOnly = false;
            }
       }
    }
}
0
 

Author Closing Comment

by:jazzcatone
ID: 39847381
thanks
0

Featured Post

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

688 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