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
IIS.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
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;
}
}
}
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!
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.
IIS.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
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.
Member
5 Points
8 Posts
My gridview cannot read the contents of my object / property: Company.Name
Jan 18, 2020 12:41 AM|Rorion|LINK
The code below doesn't show the full message: "You want to delete Company: XYZ?". It shows only: "You want to delete Company: ?"
But this code below show the full message:
Any tips why this is happening?
Contributor
6061 Points
2515 Posts
Re: My gridview cannot read the contents of my object / property: Company.Name
Jan 18, 2020 01:55 AM|KathyW|LINK
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.
Contributor
3370 Points
1409 Posts
Re: My gridview cannot read the contents of my object / property: Company.Name
Jan 18, 2020 02:32 AM|samwu|LINK
Hi Rorion,
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
Member
5 Points
8 Posts
Re: My gridview cannot read the contents of my object / property: Company.Name
Jan 20, 2020 11:25 PM|Rorion|LINK
Ok, I'll show you a little more of the code.
Disregard the previous examples. This is the real structure of my page.
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!
Contributor
3370 Points
1409 Posts
Re: My gridview cannot read the contents of my object / property: Company.Name
Jan 21, 2020 07:39 AM|samwu|LINK
Hi Rorion,
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.
Best regards,
Sam
Member
5 Points
8 Posts
Re: My gridview cannot read the contents of my object / property: Company.Name
Jan 21, 2020 05:31 PM|Rorion|LINK
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:
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!
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! ;-)