Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

c# - Checkbox not working in Gridview to change color - Error Object not set to an instance of an object

I have a Gridview (ASP.NET C#) that currently changes color based on dates provided by the user. Past Due is Red, Future or Current Date is white (Which Works Nicely!). I have also provided a checkbox in the gridview cell that designates "Complete". If that checkbox is checked, i want it to override and dates and change the cell color to Black. So far the error i keep getting is "Object not set to an instance of an object chk was null"

ASP.NET markup:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
     GridView1_RowUpdating="true" DataKeyNames="ID" OnRowUpdating="Page_Load"
     BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid"
     BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black"
     OnRowDataBound="GridView1_RowDataBound">
    <Columns>
        <asp:TemplateField HeaderText="Verify Info Prod & Maturity Level" SortExpression="IPMaturity">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("IPMaturity", "{0:MM/dd/yyyy}") %>' Width="75"></asp:TextBox>
                <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" Text="Complete" Enabled="true" OnCheckedChanged="CheckBox1_CheckedChanged" 
                     Checked='<%# Bind ("CheckBox1") %>'></asp:CheckBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="lblIPMat" runat="server" Text='<%# Eval("IPMaturity", "{0:MM/dd/yyyy}") %>' Width="75"></asp:Label>
            </ItemTemplate>
            <HeaderStyle CssClass="verticaltext" Height="140px" Width="88px" HorizontalAlign="Center" VerticalAlign="Bottom" />
        </asp:TemplateField>

C# code behind:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
      CheckBox chk = (CheckBox)e.Row.FindControl("CheckBox1");
      if (chk.Checked == true)
    {
      e.Row.Cells[4].BackColor = System.Drawing.Color.Black;
    }
    else
    {
      string ipMaturity = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "IPMaturity"));
      DateTime? date1 = !string.IsNullOrEmpty(ipMaturity) ? Convert.ToDateTime(ipMaturity) : (DateTime?)null;

    if (date1.HasValue && date1 >= DateTime.Now)
      e.Row.Cells[4].BackColor = System.Drawing.Color.White;

    if (date1.HasValue && date1 < DateTime.Now)
      e.Row.Cells[4].BackColor = System.Drawing.Color.Red;
  }
question from:https://stackoverflow.com/questions/65545602/checkbox-not-working-in-gridview-to-change-color-error-object-not-set-to-an-in

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Figured it out. I needed to add the Checkbox to the item template to make sure it shows up after saving/editing. Then i needed to deal with the Null in C#

ASP.NET markup:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"   DataSourceID="SqlDataSource1"
            GridView1_RowUpdating="true" DataKeyNames="ID" OnRowUpdating="Page_Load"
            BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid"
            BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black"
            OnRowDataBound="GridView1_RowDataBound">
            <Columns>
                
               
                <asp:TemplateField HeaderText="Verify Info Prod & Maturity Level" SortExpression="IPMaturity">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("IPMaturity", "{0:MM/dd/yyyy}") %>' Width="75"></asp:TextBox>
                        <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" Text="Complete"  Enabled="true"  
                            Checked='<%# Bind ("CheckBox1") %>'></asp:CheckBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblIPMat" runat="server" Text='<%# Eval("IPMaturity", "{0:MM/dd/yyyy}") %>' Width="75"></asp:Label>
                        <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind ("CheckBox1") %>'/>
                    </ItemTemplate>
                    <HeaderStyle CssClass="verticaltext" Height="140px" Width="88px" HorizontalAlign="Center" VerticalAlign="Bottom" />
                </asp:TemplateField>

C# Code Behind

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
  
      if (((CheckBox)e.Row.FindControl("CheckBox1")) != null && ((CheckBox)e.Row.FindControl("CheckBox1")).Checked)
   
      {
        e.Row.Cells[4].BackColor = System.Drawing.Color.Black;
        e.Row.Cells[4].ForeColor = System.Drawing.Color.White;
      }
      else
     {
        string ipMaturity = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "IPMaturity"));
        DateTime? date1 = !string.IsNullOrEmpty(ipMaturity) ?  Convert.ToDateTime(ipMaturity) : (DateTime?)null;

        if (date1.HasValue && date1 >= DateTime.Now)
          e.Row.Cells[4].BackColor = System.Drawing.Color.White;

        if (date1.HasValue && date1 < DateTime.Now)
          e.Row.Cells[4].BackColor = System.Drawing.Color.Red;
      }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...