locked
My gridview cannot read the contents of my object / property: Company.Name RRS feed

  • Question

  • User-317007944 posted

    The code below doesn't show the full message: "You want to delete Company: XYZ?". It shows only: "You want to delete Company: ?"

    <asp:GridView ID="gvTEST" runat="server" AutoGenerateColumns="false" Width="50%" DataKeyNames="IdCompany" AllowPaging="True" PageSize="15" AllowSorting="true" OnRowDataBound="gvTEST_RowDataBound" OnRowCommand="gvTEST_RowCommand" OnRowDeleting="gvTEST_RowDeleting">
    	<Columns>
    		<asp:TemplateField HeaderStyle-Width="3%" ItemStyle-HorizontalAlign="Center">
    			<ItemTemplate>
    				<asp:ImageButton ID="imbDelete" runat="server" ImageUrl="../../Images/Operations/delete.gif" ToolTip="Delete" CausesValidation="false" Visible="false" />
    			</ItemTemplate>
    		</asp:TemplateField>
    
    		<asp:TemplateField HeaderStyle-Width="3%" ItemStyle-HorizontalAlign="Center">
    			<ItemTemplate>
    				<asp:ImageButton ID="imbUpdate" runat="server" ImageUrl="../../Images/Operations/update.png" ToolTip="Update" CausesValidation="false" Visible="false" />
    			</ItemTemplate>
    		</asp:TemplateField>
    
    		<asp:TemplateField HeaderText="Name" SortExpression="Company.Name" HeaderStyle-Width="44%" HeaderStyle-Height="15px" ItemStyle-HorizontalAlign="Left">
    			<ItemTemplate>
    				<%# DataBinder.Eval(Container.DataItem, "Company.Name")%>
    			</ItemTemplate>
    		</asp:TemplateField>
    	</Columns>
    </asp:GridView>
    
    .
    .
    .
    
    protected void gvTEST_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) {
    	if (e.Row.RowType == DataControlRowType.DataRow) {
    		var imbDelete = (ImageButton)e.Row.FindControl("imbDelete");
    		imbDelete.CommandName = "DeleteTEST";
    		imbDelete.CommandArgument = gvTEST.DataKeys[e.Row.RowIndex].Values["IdCompany"].ToString();
    		imbDelete.OnClientClick = "return window.confirm('You want to delete TEST: " + Server.HtmlDecode(e.Row.Cells[2].Text) + "?');";
    
    		ScriptManager.GetCurrent(this).RegisterPostBackControl(imbDelete);
    	}
    }

    But this code below show the full message:

    <asp:GridView ID="gvTEST" runat="server" AutoGenerateColumns="false" Width="50%" DataKeyNames="IdTEST" AllowPaging="True" PageSize="15" AllowSorting="true" OnRowDataBound="gvTEST_RowDataBound" OnRowCommand="gvTEST_RowCommand" OnRowDeleting="gvTEST_RowDeleting">
    	<Columns>
    		<asp:TemplateField HeaderStyle-Width="3%" ItemStyle-HorizontalAlign="Center">
    			<ItemTemplate>
    				<asp:ImageButton ID="imbDelete" runat="server" ImageUrl="../../Images/Operations/delete.gif" ToolTip="Delete" CausesValidation="false" Visible="false" />
    			</ItemTemplate>
    		</asp:TemplateField>
    
    		<asp:TemplateField HeaderStyle-Width="3%" ItemStyle-HorizontalAlign="Center">
    			<ItemTemplate>
    				<asp:ImageButton ID="imbUpdate" runat="server" ImageUrl="../../Images/Operations/update.png" ToolTip="Update" CausesValidation="false" Visible="false" />
    			</ItemTemplate>
    		</asp:TemplateField>
    
    		<asp:TemplateField HeaderText="Name" SortExpression="Name" HeaderStyle-Width="44%" HeaderStyle-Height="15px" ItemStyle-HorizontalAlign="Left">
    			<ItemTemplate>
    				<%# DataBinder.Eval(Container.DataItem, "Name")%>
    			</ItemTemplate>
    		</asp:TemplateField>
    	</Columns>
    </asp:GridView>

    Any tips why this is happening?

    Saturday, January 18, 2020 12:41 AM

Answers

  • User288213138 posted

    Hi Rorion,

    Then I added 2 more examples where I display the fields from e.Row.Cells (gvCompany_RowDataBound), and only Name (Company Name) is displayed. Country.Name is not displayed!

    I looked at your code and found that your Company Name and Country.Name are bound differently in aspx. 

    I guess this may be the reason why Country.Name is not displayed.

    Can your show me how do you bind data?

    And you can try to use the label to display the Country.Name and get the text by label control.

    <asp:Label ID="salLbl" runat="server" Text='<%# DataBinder.Eval(Container, "City.Name") %>'></asp:Label>
    
    protected void gvTEST_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
            {           
                if (e.Row.RowType == DataControlRowType.DataRow)
                {                    
                    string sal = ((Label)e.Row.FindControl("salLbl")).Text;        
                }
            }
    
    

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 21, 2020 7:39 AM

All replies

  • User409696431 posted

    I assume it is because Name is the name of the data column in the database you are querying to bind the gridview to.

    Otherwise, you'll have to show us a lot more than what you have shown us.

    Saturday, January 18, 2020 1:55 AM
  • User288213138 posted

    Hi Rorion,

    imbDelete.OnClientClick = "return window.confirm('You want to delete TEST: " + Server.HtmlDecode(e.Row.Cells[2].Text) + "?');";

    Since you gave only part of the code, I can't reproduce your problem.

    I suggest you set a breakpoint to see the value in e.Row.Cells [2] .Text.

    If you still can't solve the problem, please post the complete code that can reproduce the question.

    Best regards,

    Sam

    Saturday, January 18, 2020 2:32 AM
  • User-317007944 posted

    Ok, I'll show you a little more of the code.

    Disregard the previous examples. This is the real structure of my page.

        [Serializable()]
        public class Company {
    
            public int? IdCompany { get; set; }
            public string Name { get; set; }
            public int? IdCountry { get; set; }
            public int? IdCity { get; set; }
            public int? Year { get; set; }
    
            private Country country = new Country();
            public Country Country {
                get {
                    return country;
                }
                set {
                    country = value;
                }
            }
    
            private City city = new City();
            public City City {
                get {
                    return city;
                }
                set {
                    city = value;
                }
            }
    
        }
                <asp:GridView ID="gvCompany" runat="server" AutoGenerateColumns="false" Width="100%" DataKeyNames="IdCompany" AllowPaging="True" PageSize="15" AllowSorting="true" OnRowDataBound="gvCompany_RowDataBound" OnRowCommand="gvCompany_RowCommand" OnRowDeleting="gvCompany_RowDeleting">
                    <Columns>
                        <asp:TemplateField HeaderStyle-Width="3%" ItemStyle-HorizontalAlign="Center">
                            <ItemTemplate>
                                <asp:ImageButton ID="imbDelete" runat="server" ImageUrl="../../Images/Operations/delete.gif" ToolTip="Delete" CausesValidation="false" Visible="false" />
                            </ItemTemplate>
                        </asp:TemplateField>
    
                        <asp:TemplateField HeaderStyle-Width="3%" ItemStyle-HorizontalAlign="Center">
                            <ItemTemplate>
                                <asp:ImageButton ID="imbUpdate" runat="server" ImageUrl="../../Images/Operations/update.png" ToolTip="Update" CausesValidation="false" Visible="false" />
                            </ItemTemplate>
                        </asp:TemplateField>
    
                        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" HeaderStyle-Width="46%" HeaderStyle-Height="15px" ItemStyle-HorizontalAlign="Left" />
    
                        <asp:TemplateField HeaderText="Country" SortExpression="Country.Name" HeaderStyle-Width="16%" HeaderStyle-Height="15px" ItemStyle-HorizontalAlign="Left">
                            <ItemTemplate>
                                <%# DataBinder.Eval(Container.DataItem, "Country.Name")%>
                            </ItemTemplate>
                        </asp:TemplateField>
    
                        <asp:TemplateField HeaderText="City" SortExpression="City.Name" HeaderStyle-Width="16%" HeaderStyle-Height="15px" ItemStyle-HorizontalAlign="Left">
                            <ItemTemplate>
                                <%# DataBinder.Eval(Container.DataItem, "City.Name")%>
                            </ItemTemplate>
                        </asp:TemplateField>
    
                        <asp:BoundField DataField="Year" HeaderText="Year" SortExpression="Year" HeaderStyle-Width="16%" HeaderStyle-Height="15px" ItemStyle-HorizontalAlign="Center" />
                    </Columns>
    
                    <HeaderStyle CssClass="gridBlueHeaderStyle" HorizontalAlign="Center" />
                    <RowStyle CssClass="gridBlueRowStyle" />
                    <AlternatingRowStyle CssClass="gridBlueAlternatingRowStyle" />
                    <PagerStyle CssClass="gridBluePagerStyle" HorizontalAlign="Center" />
                </asp:GridView>
            protected void gvCompany_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) {
                if (e.Row.RowType == DataControlRowType.DataRow) {
                    if (gvCompany.DataKeys[e.Row.RowIndex].Values["IdCompany"] != null) {
                        SetGridDeleteImageButton(e);
                        SetGridUpdateImageButton(e);
                    }
                }
            }
            private void SetGridDeleteImageButton(System.Web.UI.WebControls.GridViewRowEventArgs e) {
                var imbDelete = (ImageButton)e.Row.FindControl("imbDelete");
                imbDelete.CommandName = "DeleteCompany";
                imbDelete.CommandArgument = gvCompany.DataKeys[e.Row.RowIndex].Values["IdCompany"].ToString();
                imbDelete.OnClientClick = "return window.confirm('You want to delete company: " + Server.HtmlDecode(e.Row.Cells[3].Text) + "?');";
                //imbDelete.OnClientClick = "return window.confirm('You want to delete company: " + Server.HtmlDecode(e.Row.Cells[WebForms.GetColumnIndexByName(e.Row, "Country.Name")].Text) + "?');";
                imbDelete.Visible = AccessControl.IsAccessControl(systemUserLogged.Controls, "Domain.Company.Delete");
    
                ScriptManager.GetCurrent(this).RegisterPostBackControl(imbDelete);
    
                //TEST
                var companies = BLL.Company.SelectBy<Model.Company>(Gl0b4l.DataProvider.MySQL, CommandType.Text, new Company { Name = txtName.Text });
                
                myDiv.InnerHtml = "<br><br>" +
                                  "Company Name (Database): " + companies[0].Name + "<br>" +
                                  "Country Name (Database): " + companies[0].Country.Name + "<br><br>" +
                                  "Company Name (GridView: e.Row.Cells): " + e.Row.Cells[2].Text + "<br>" +
                                  "Country Name (GridView: e.Row.Cells): " + e.Row.Cells[3].Text;
                //---
            }

    I added 2 examples at the end of the code where I bring the fields: Name (Company Name) and Country.Name from my database, and as you can see, they are displayed on my page.

    Then I added 2 more examples where I display the fields from e.Row.Cells (gvCompany_RowDataBound), and only Name (Company Name) is displayed. Country.Name is not displayed!

    Monday, January 20, 2020 11:25 PM
  • User288213138 posted

    Hi Rorion,

    Then I added 2 more examples where I display the fields from e.Row.Cells (gvCompany_RowDataBound), and only Name (Company Name) is displayed. Country.Name is not displayed!

    I looked at your code and found that your Company Name and Country.Name are bound differently in aspx. 

    I guess this may be the reason why Country.Name is not displayed.

    Can your show me how do you bind data?

    And you can try to use the label to display the Country.Name and get the text by label control.

    <asp:Label ID="salLbl" runat="server" Text='<%# DataBinder.Eval(Container, "City.Name") %>'></asp:Label>
    
    protected void gvTEST_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
            {           
                if (e.Row.RowType == DataControlRowType.DataRow)
                {                    
                    string sal = ((Label)e.Row.FindControl("salLbl")).Text;        
                }
            }
    
    

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 21, 2020 7:39 AM
  • User-317007944 posted

    Great!

    It is true, instead of taking the cell value using "e.Row.Cells [index]", you can also take it using a label inside the "ItemTemplate" of the gridview.

    I looked at your code and found that your Company Name and Country.Name are bound differently in aspx. 

    Yes ...

    Company Name is: 

    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" HeaderStyle-Width="46%" HeaderStyle-Height="15px" ItemStyle-HorizontalAlign="Left" />

    And Country Name is (were):  

                        <asp:TemplateField HeaderText="Country" SortExpression="Country.Name" HeaderStyle-Width="16%" HeaderStyle-Height="15px" ItemStyle-HorizontalAlign="Left">
                            <ItemTemplate>
                                <%# DataBinder.Eval(Container.DataItem, "Country.Name")%>
                            </ItemTemplate>
                        </asp:TemplateField>

    I guess this may be the reason why Country.Name is not displayed.


    I think gridview e.Row.Cells can't get an object's value inside an another object (mirroring). ps: Country object inside Company object.

    And you can try to use the label to display the Country.Name and get the text by label control.

    Yes, it works!

                <asp:GridView ID="gvCompany" runat="server" AutoGenerateColumns="false" Width="100%" DataKeyNames="IdCompany" AllowPaging="True" PageSize="15" AllowSorting="true" OnRowDataBound="gvCompany_RowDataBound" OnRowCommand="gvCompany_RowCommand" OnRowDeleting="gvCompany_RowDeleting">
                    <Columns>
                        <asp:TemplateField HeaderStyle-Width="3%" ItemStyle-HorizontalAlign="Center">
                            <ItemTemplate>
                                <asp:ImageButton ID="imbDelete" runat="server" ImageUrl="../../Images/Operations/delete.gif" ToolTip="Delete" CausesValidation="false" Visible="false" />
                            </ItemTemplate>
                        </asp:TemplateField>
    
                        <asp:TemplateField HeaderStyle-Width="3%" ItemStyle-HorizontalAlign="Center">
                            <ItemTemplate>
                                <asp:ImageButton ID="imbUpdate" runat="server" ImageUrl="../../Images/Operations/update.png" ToolTip="Update" CausesValidation="false" Visible="false" />
                            </ItemTemplate>
                        </asp:TemplateField>
    
                        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" HeaderStyle-Width="46%" HeaderStyle-Height="15px" ItemStyle-HorizontalAlign="Left" />
    
                        <asp:TemplateField HeaderText="Country" SortExpression="Country.Name" HeaderStyle-Width="16%" HeaderStyle-Height="15px" ItemStyle-HorizontalAlign="Left">
                            <ItemTemplate>
                                <asp:Label ID="lblCountryName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Country.Name")%>' />
                            </ItemTemplate>
                        </asp:TemplateField>
    
                        <asp:TemplateField HeaderText="City" SortExpression="City.Name" HeaderStyle-Width="16%" HeaderStyle-Height="15px" ItemStyle-HorizontalAlign="Left">
                            <ItemTemplate>
                                <%# DataBinder.Eval(Container.DataItem, "City.Name")%>
                            </ItemTemplate>
                        </asp:TemplateField>
    
                        <asp:BoundField DataField="Year" HeaderText="Year" SortExpression="Year" HeaderStyle-Width="16%" HeaderStyle-Height="15px" ItemStyle-HorizontalAlign="Center" />
                    </Columns>
    
                    <HeaderStyle CssClass="gridBlueHeaderStyle" HorizontalAlign="Center" />
                    <RowStyle CssClass="gridBlueRowStyle" />
                    <AlternatingRowStyle CssClass="gridBlueAlternatingRowStyle" />
                    <PagerStyle CssClass="gridBluePagerStyle" HorizontalAlign="Center" />
                </asp:GridView>
            private void SetGridDeleteImageButton(System.Web.UI.WebControls.GridViewRowEventArgs e) {
                var lblCountryName = (Label)e.Row.FindControl("lblCountryName");
    
                var imbDelete = (ImageButton)e.Row.FindControl("imbDelete");
                imbDelete.CommandName = "DeleteCompany";
                imbDelete.CommandArgument = gvCompany.DataKeys[e.Row.RowIndex].Values["IdCompany"].ToString();
                imbDelete.OnClientClick = "return window.confirm('You want to delete country: " + Server.HtmlDecode(lblCountryName.Text) + "?');";
                //imbDelete.OnClientClick = "return window.confirm('You want to delete company: " + Server.HtmlDecode(e.Row.Cells[WebForms.GetColumnIndexByName(e.Row, "Country.Name")].Text) + "?');";
                imbDelete.Visible = AccessControl.IsAccessControl(systemUserLogged.Controls, "Domain.Company.Delete");
    
                ScriptManager.GetCurrent(this).RegisterPostBackControl(imbDelete);
            }

    System Screen URL: https://ibb.co/Lvf1cMP

    ps: Actually, I'm going to use this logic on another screen of my system. I used it here (Company Cons) cause it was easier to explain

    Thanks! ;-)

    Tuesday, January 21, 2020 5:31 PM