Gridview单元格颜色需要根据在asp.net/c#中检查的CheckBox进行更改不断收到错误 [英] Gridview Cell Color needs to change based on CheckBox checked in asp.net/c# keep getting errors

查看:40
本文介绍了Gridview单元格颜色需要根据在asp.net/c#中检查的CheckBox进行更改不断收到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Gridview(ASP.NET/C#),当前它根据用户提供的日期更改颜色.逾期为红色,未来或当前日期为白色(效果很好!).我还在网格视图单元格中提供了一个复选框,指定完成".如果该复选框被选中,我希望它覆盖和日期并将单元格颜色更改为黑色".到目前为止,我一直遇到的错误是,当我在"RowDataBound"下尝试将其设置为对象的实例时,该对象未设置为该对象的实例.或现在使用当前代码,我已经尝试将其放在"OnCheckedChanged"下现在可以阻止对复选框进行任何更改.
数据作为位存储在数据库中.更新命令将参数设置为布尔值.

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 errors i keep getting are either, object not set to an instance of an object when i have tried it under "RowDataBound" or now with the current code, i have tried putting it under "OnCheckedChanged" which now has prevented any changes to the checkbox.
The data is stored in the database as Bit. Update Command sets parameters as Boolean.

关于如何使它起作用的任何想法?

Any ideas on how to get this to work?

ASP.NET标记:

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="false" 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#代码:

protected void CheckBox1_CheckedChanged(object sender, EventArgs e) //from checkbox
{
     CheckBox chkItem = null;
     //Boolean chkb = Convert.ToBoolean(DataBinder.Eval(e.Row.DataItem, "CheckBox1"));

     foreach (GridViewRow grRow in GridView1.Rows)
     {
         if (grRow.RowType == DataControlRowType.DataRow)
         {
             chkItem = (CheckBox)grRow.Cells[4].FindControl("CheckBox1");
             bool bl = chkItem.Checked;

             if (bl == true)
             {
                 grRow.BackColor = Color.Black;
             }
         }
    }
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)  // Changes color of cell based on date
    {
        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;
    }
}

推荐答案

尽可能多地保存代码,我给出了一个最小的示例来显示复选框更改背景色.

Keeping as much of your code as possible, I've made a bare minimum example to show checkbox changing background color.

需要指出的是,上面的代码示例没有 Cells [4] (所以我刚使用0).我已将SQL调用替换为( IPMaturityData )类&硬编码数据.由于没有调用启用 EditItemTemplate 的功能,因此我继续进行操作,并添加了< asp:CommandField ShowEditButton ="True"/>

Things to point out, the code example above does not have Cells[4] (so I've just gone with 0). I've replaces SQL call with class (IPMaturityData) & hardcoded data. There is no call to enable EditItemTemplate, so I've just went ahead and added <asp:CommandField ShowEditButton="True" />

该代码不会为您提供所需的一切.但是,它应该足够玩,才能使其按需工作.

The code won't give you everything you need. However it should be enough to play around and get it working as desired.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    GridView1_RowUpdating="true" DataKeyNames="ID" OnRowUpdating="Page_Load"
    BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid"
    BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black"
    OnRowEditing="GridView1_RowEditing"
    OnRowDataBound="GridView1_RowDataBound">
    <Columns>
        <asp:TemplateField>
            <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"
                    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>
        <asp:CommandField ShowEditButton="True" />
    </Columns>
</asp:GridView>

背后的代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        this.DataBindGrid();
    }
}


protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView grid = sender as GridView;

    if (grid == null)
    {
        return;
    }

    grid.EditIndex = e.NewEditIndex;
    this.DataBindGrid();
}
  
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)  // Changes color of cell based on date
    {
        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[0].BackColor = System.Drawing.Color.White; // Theres no cell 4

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

protected void CheckBox1_CheckedChanged(object sender, EventArgs e) //from checkbox
{
    foreach (GridViewRow grRow in GridView1.Rows)
    {
        if (grRow.RowType == DataControlRowType.DataRow)
        {
            CheckBox chkItem = (CheckBox)sender;
            // chkItem = (CheckBox)grRow.Cells[4].FindControl("CheckBox1"); // This would work if cell 4 has a CheckBox

            if (chkItem.Checked)
            {
                grRow.BackColor = Color.Black;
            }
        }
    }
}

private void DataBindGrid()
{
    var x = new List<IPMaturityData>();
    x.Add(new IPMaturityData() { ID = 1, IPMaturity = DateTime.Now.AddDays(-1) });
    x.Add(new IPMaturityData() { ID = 2, IPMaturity = DateTime.Now.AddDays(1) });
    GridView1.DataSource = x;
    GridView1.DataBind();
}

public class IPMaturityData
{
    public int ID { get; set; }

    public DateTime IPMaturity { get; set; }

    public bool CheckBox1 { get; set; } // This looks wrong, but on html it has "Checked='<%# Bind ("CheckBox1") %>"
}

这篇关于Gridview单元格颜色需要根据在asp.net/c#中检查的CheckBox进行更改不断收到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆