locked
About RowDataBound RRS feed

  • Question

  • User544906106 posted

    Hi,

    I have two columns save date of my contract date, and I want to make the expiring contract red, for example:

    001 Peter  Contract2017   2017/01/01  2017/12/31   , and today's date is 2017/12/12-----> For this  Column, should be red . like this    001 Peter  Contract2017   2017/01/01  2017/12/31   

    This is my code :

    Protected Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

            If e.Row.RowType = DataControlRowType.DataRow Then

                Dim ContractEndD As Label = CType(e.Row.FindControl("Label5"), Label)

                Dim dd As String = CStr(DateTime.Today)

                If CBool(ContractEndD.Text < dd And ContractEndD.Text <> "") = True Then

                    e.Row.BackColor = Drawing.Color.LightPink

                End If

            End If

    It doesn't work after i add <EditItemTemplate>,Why ?

    <asp:TemplateField HeaderText="合約起始日" SortExpression="ContractStartD">

                                            <EditItemTemplate>

                                                <asp:TextBox ID="tbContractStartD" runat="server" Text='<%#Bind("ContractStartD","{0:yyyy/MM/dd}") %>' TextMode="Date"></asp:TextBox>

                                            </EditItemTemplate>

                                            <ItemTemplate>

                                                <asp:Label ID="Label4" runat="server" Text='<%# Eval("ContractStartD","{0:yyyy/MM/dd}") %>'></asp:Label>

                                            </ItemTemplate>

                                            <ItemStyle Width="10%" />

                                        </asp:TemplateField>

                                        <asp:TemplateField HeaderText="合約到期日" SortExpression="ContractEndD">

                                            <EditItemTemplate>

                                            <asp:TextBox ID="tbContractEndD" runat="server" Text='<%#Bind("ContractEndD","{0:yyyy/MM/dd}") %>' TextMode="Date" />

                                            </EditItemTemplate>

                                            <ItemTemplate>

                                                <asp:Label ID="Label5" runat="server" Text='<%# Eval("ContractEndD","{0:yyyy/MM/dd}") %>'></asp:Label>

                                            </ItemTemplate>

                                            <ItemStyle Width="10%" />

                                        </asp:TemplateField>

    My code was without <EditItemTemplate>, but i want to make my gridview can edit, so i add the <EditItemTemplate>,

    before i add this ,  My RowDataBound can work , but when i add it doesn't work .... and the error message is  System.NullReferenceException....

    how can i solve it ?

    Thanks 

    Regards

     

    Tuesday, December 12, 2017 8:23 AM

Answers

  • User-1838255255 posted

    Hi PPetee,

    According to your description and needs, I make a sample based on your code, it works, please check:

    Sample Code:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
                    <Columns>
                        <asp:TemplateField HeaderText="合約起始日" SortExpression="ContractStartD">
                            <EditItemTemplate>
                                <asp:TextBox ID="tbContractStartD" runat="server" Text='<%#Bind("ContractStartD","{0:yyyy/MM/dd}") %>' TextMode="Date"></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label4" runat="server" Text='<%# Eval("ContractStartD","{0:yyyy/MM/dd}") %>'></asp:Label>
                            </ItemTemplate>
                            <ItemStyle Width="10%" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="合約到期日" SortExpression="ContractEndD">
                            <EditItemTemplate>
                                <asp:TextBox ID="tbContractEndD" runat="server" Text='<%#Bind("ContractEndD","{0:yyyy/MM/dd}") %>' TextMode="Date" />
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label5" runat="server" Text='<%# Eval("ContractEndD","{0:yyyy/MM/dd}") %>'></asp:Label>
                            </ItemTemplate>
                            <ItemStyle Width="10%" />
                        </asp:TemplateField>
                    </Columns>
    </asp:GridView>
    
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                Dim dt As DataTable = New DataTable()
                dt.Columns.Add("ContractStartD", GetType(DateTime))
                dt.Columns.Add("ContractEndD", GetType(DateTime))
                dt.Rows.Add("2017/01/01", "2017/12/31")
                dt.Rows.Add("2017/01/01", "2017/12/31")
                dt.Rows.Add("2017/01/01", "2017/12/31")
                GridView1.DataSource = dt
                GridView1.DataBind()
            End If
    
        End Sub
    
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim ContractEndD As Label = CType(e.Row.FindControl("Label5"), Label)
                Dim dd As String = DateTime.Today.ToString()
                If DateTime.Parse(ContractEndD.Text) > DateTime.Parse(dd) AndAlso ContractEndD.Text <> "" Then
                    e.Row.BackColor = System.Drawing.Color.LightPink
                End If
            End If
        End Sub

     About that exception, I think it is not related to the edittempleate, so you need check which line code caused this error and debug it step by step to find issue. 

    Best Regards,

    Eric Du 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 13, 2017 3:20 AM