双人余公
2021-09-01 20:36
采纳率: 71.4%
浏览 27

C# asp.net GridView中BoundField改为TemplateField后,部分列后台获取不到

原本是用GridView的BoundField属性展示列,现在因为需要增加删除和编辑GridView的行,改为TemplateField展示。但是原来部分更改单元格颜色的代码失效了,找不到原因。
原来的前端代码:

 <asp:GridView ID="grid_01" runat="server"  OnRowDataBound="grid_01_RowDataBound" AutoGenerateColumns="False" Width="100%"  Font-Names="Arial" AllowSorting="True">
                      <Columns>
                       <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:CheckBox ID="CheckBox1" runat="server" />
                                </ItemTemplate>
                            </asp:TemplateField> 
                      
                        <asp:BoundField DataField="col1" ShowHeader="true" HeaderText="col1" ReadOnly="true"
                            ItemStyle-HorizontalAlign="center" HeaderStyle-BackColor="SkyBlue" />
                        <asp:BoundField DataField="col2" ShowHeader="true" HeaderText="col2" ReadOnly="true"
                            ItemStyle-HorizontalAlign="center" HeaderStyle-BackColor="SkyBlue" />
                        <asp:BoundField DataField="col3" ShowHeader="true" HeaderText="col3" ReadOnly="true"
                            ItemStyle-HorizontalAlign="center" HeaderStyle-BackColor="SkyBlue" />
                        <asp:BoundField DataField="col4" ShowHeader="true" HeaderText="col4" ReadOnly="true"
                            ItemStyle-HorizontalAlign="center" HeaderStyle-BackColor="SkyBlue" />
                    </Columns>
                </asp:GridView>

更改后前端代码:

<asp:GridView ID="grid_01" runat="server"  OnRowDataBound="grid_01_RowDataBound" AutoGenerateColumns="False" Width="100%"   Font-Names="Arial" AllowSorting="True">
                      <Columns>
                       <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:CheckBox ID="CheckBox1" runat="server" />
                                </ItemTemplate>
                            </asp:TemplateField> 

                        <asp:TemplateField ShowHeader="true" HeaderText="col1" HeaderStyle-BackColor="SkyBlue"
                            ItemStyle-HorizontalAlign="Center">
                            <ItemTemplate>
                                <asp:Label ID="lbl_col1" runat="server" Text='<%# Eval("col1") %>'></asp:Label>
                                <asp:TextBox ID="gv_tb_col1" runat="server" Text='<%# Eval("col1") %>' Visible="false" />
                            </ItemTemplate>
                        </asp:TemplateField>

                        <asp:TemplateField ShowHeader="true" HeaderText="col2" HeaderStyle-BackColor="Silver">
                            <ItemTemplate>
                                <asp:Label ID="lbl_col2" runat="server" Text='<%# Eval("col2") %>'></asp:Label>
                                <asp:TextBox ID="gv_tb_col2" runat="server" Text='<%# Eval("col2") %>' Visible="false" />
                            </ItemTemplate>
                        </asp:TemplateField>

                        <asp:TemplateField ShowHeader="true" HeaderText="col3" HeaderStyle-BackColor="Silver">
                            <ItemTemplate>
                                <asp:Label ID="lbl_col3" runat="server" Text='<%# Eval("col3") %>'></asp:Label>
                                <asp:TextBox ID="gv_tb_col3" runat="server" Text='<%# Eval("col3") %>' Visible="false" />
                            </ItemTemplate>
                        </asp:TemplateField>
                     
                         <asp:TemplateField ShowHeader="true" HeaderText="col4" HeaderStyle-BackColor="Silver">
                            <ItemTemplate>
                                <asp:Label ID="lbl_col4" runat="server" Text='<%# Eval("col4") %>'></asp:Label>
                                <asp:TextBox ID="gv_tb_col4" runat="server" Text='<%# Eval("col4") %>' Visible="false" />
                            </ItemTemplate>
                        </asp:TemplateField>
                               
                    </Columns>
                </asp:GridView>


后台更改颜色代码:

protected void grid_01_RowDataBound(object sender, GridViewRowEventArgs e) {
            DataRowView drv = (DataRowView)e.Row.DataItem;
            if (e.Row.RowType == DataControlRowType.DataRow)    //不包括第一行表头
            {
                if (e.Row.Cells[1].Text == "Y")
                {
                    e.Row.Cells[1].Attributes.Add("style", "background-color:DodgerBlue  ");
                }
                if (!string.IsNullOrEmpty(drv["number"].ToString()))   //单元格不为空
                {
                    if (int.Parse(e.Row.Cells[2].Text) < 0)
                    {
                        e.Row.Cells[2].Attributes.Add("style", "color:red  ");
                    }
                }
              Response.Write(e.Row.Cells[4].Text);
                if (!string.IsNullOrEmpty(drv["x"].ToString()) && !string.IsNullOrEmpty(drv["y"].ToString()))   //单元格不为空
                {
                    if (DateTime.Compare(Convert.ToDateTime(e.Row.Cells[4].Text), Convert.ToDateTime(e.Row.Cells[3].Text)) > 0)     //y大于x
                    {
                        e.Row.Cells[4].Attributes.Add("style", "color:red  ");
                    }
                }
            }
        }

代码太长,这里做了简化处理,更改前端代码后,col1和col2的颜色能更改,col1值是Y或N,col2值是整数。
但是日期型就不行了,col3,col4都是日期,前端数据正常展现,Response就获取不到值了,没有东西打印出来,更改颜色更不行,报格式错误。而用原来的前端代码是可以取出日期数据的,实在是奇怪。

  • 好问题 提建议
  • 收藏

2条回答 默认 最新

  • Yijing Sun 2021-09-02 10:30
    已采纳

    您好,
    当你使用TemplateField时, 不能使用e.Row.Cell[] 去得到值. 你可以使用findcontrol. 请参考以下代码:

    <asp:GridView ID="grid_01" runat="server" OnRowDataBound="grid_01_RowDataBound" AutoGenerateColumns="False" Width="100%" Font-Names="Arial" AllowSorting="True" AutoGenerateEditButton="true" OnRowEditing="grid_01_RowEditing" OnRowCancelingEdit="grid_01_RowCancelingEdit">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField ShowHeader="true" HeaderText="col1" HeaderStyle-BackColor="SkyBlue"
                        ItemStyle-HorizontalAlign="Center">
                        <ItemTemplate>
                            <asp:Label ID="lbl_col1" runat="server" Text='<%# Eval("col1") %>'></asp:Label>
                            <asp:TextBox ID="gv_tb_col1" runat="server" Text='<%# Eval("col1") %>' Visible="false" />
                        </ItemTemplate>
                    </asp:TemplateField>
    
                    <asp:TemplateField ShowHeader="true" HeaderText="col2" HeaderStyle-BackColor="Silver">
                        <ItemTemplate>
                            <asp:Label ID="lbl_col2" runat="server" Text='<%# Eval("col2") %>'></asp:Label>
                            <asp:TextBox ID="gv_tb_col2" runat="server" Text='<%# Eval("col2") %>' Visible="false" />
                        </ItemTemplate>
                    </asp:TemplateField>
    
                    <asp:TemplateField ShowHeader="true" HeaderText="col3" HeaderStyle-BackColor="Silver">
                        <ItemTemplate>
                            <asp:Label ID="lbl_col3" runat="server" Text='<%# Eval("col3") %>'></asp:Label>
                            <asp:TextBox ID="gv_tb_col3" runat="server" Text='<%# Eval("col3") %>' Visible="false" />
                        </ItemTemplate>
                    </asp:TemplateField>
    
                    <asp:TemplateField ShowHeader="true" HeaderText="col4" HeaderStyle-BackColor="Silver">
                        <ItemTemplate>
                            <asp:Label ID="lbl_col4" runat="server" Text='<%# Eval("col4") %>'></asp:Label>
                            <asp:TextBox ID="gv_tb_col4" runat="server" Text='<%# Eval("col4") %>' Visible="false" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
    
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[4] { new DataColumn("col1", typeof(string)),
                        new DataColumn("col2", typeof(int)),
                        new DataColumn("col3", typeof(DateTime)),
                        new DataColumn("col4", typeof(DateTime))
                    });
                    dt.Rows.Add("Y", 1, "2020/5/2", "2021/8/5");
                    dt.Rows.Add("N", 2, "2022/4/4", "2023/9/8");
                    grid_01.DataSource = dt;
                    grid_01.DataBind();
                }
            }
    
            protected void grid_01_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                DataRowView drv = (DataRowView)e.Row.DataItem;
                if (e.Row.RowType == DataControlRowType.DataRow)    //不包括第一行表头
                {
                    Label lab1 = (Label)e.Row.FindControl("lbl_col1");
                    Label lab2 = (Label)e.Row.FindControl("lbl_col2");
                    Label lab3 = (Label)e.Row.FindControl("lbl_col3");
                    Label lab4 = (Label)e.Row.FindControl("lbl_col4");
                    if (lab1.Text == "Y")
                    {
                        lab1.Attributes.Add("style", "background-color:DodgerBlue");
                    }
                    if (!string.IsNullOrEmpty(drv["col2"].ToString()))   //单元格不为空
                    {
                        if (Convert.ToInt32(lab2.Text) < 0)
                        {
                            lab2.Attributes.Add("style", "color:red");
                        }
                    }
                    Response.Write(lab3.Text);
                    if (!string.IsNullOrEmpty(drv["col3"].ToString()) && !string.IsNullOrEmpty(drv["col4"].ToString()))   //单元格不为空
                    {
                        if (DateTime.Compare(Convert.ToDateTime(lab4.Text), Convert.ToDateTime(lab3.Text)) > 0)     //y大于x
                        {
                            lab3.Attributes.Add("style", "color:red");
                        }
                    }
    
                }
            }
    
            protected void grid_01_RowEditing(object sender, GridViewEditEventArgs e)
            {
    
            }
    
            protected void grid_01_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
            {
    
            }
    
    

    结果:

    img

    已采纳该答案
    评论
    解决 1 无用
    打赏 举报
  • 双人余公 2021-09-01 20:40

    补充:后面还有col5,6,7等等,我发现只有数值和col1的单个字母能正常获取,其他的日期,或者字符串就无法获取。

    评论
    解决 无用
    打赏 举报