Reputation: 43
I want to show the Model data passed from one form to another form.
In first form I save the data to the database and also at the same time i get back the data to a model. Now I want to send this model to a new form. After sending it to a new form I want to display it in the Datagridview. I did the rest of thing correct but now I cannot display it in the datagridview. To be honest I dont know how to display a model in the data grid view.
company = GlobalConfig.Connections.CreateCompany(company);
// MessageBox.Show(Convert.ToString(company.id));
AddInformationForm form = new AddInformationForm(company);
form.ShowDialog();
company is a model.
public class CompanyModel
{
public int id { get; set; }
public int Mohre { get; set; }
public string CompanyNameEn { get; set; }
public string CompanyNameAr { get; set; }
public int CompanyLicense { get; set; }
public DateTime CompanyLicenseExpiry { get; set; }
public int MOI { get; set; }
public DateTime MOIExpiry { get; set; }
public ContactModel Contacts { get; set; }
public SectorModel Sector { get; set; }
public CompanyCategoryModel Category { get; set; }
public CompanyModel()
{
}
}
how to display it in datagridview.
And Also when i just want to assign the values to the labels it gives me null exception reference
public partial class AddInformationForm : Form
{
private CompanyModel company = new CompanyModel();
public AddInformationForm()
{
InitializeComponent();
}
public AddInformationForm(CompanyModel cmp)
{
company = cmp;
}
private void AddInformationForm_Load(object sender, EventArgs e)
{
CompanyNameLabel.Text = company.CompanyNameAr;
SectorLabel.Text = company.Sector.Name;
CategoryLabel.Text = company.Category.CategoryName;
LicenseLabel.Text = Convert.ToString(company.CompanyLicense);
MohreLabel.Text = Convert.ToString(company.Mohre);
MOILabel.Text = Convert.ToString(company.MOI);
}
}
the values are passed perfectly fine and also present it but still gives null exception. what could be the issue here
Upvotes: 1
Views: 1693
Reputation: 56
Try like below:
dataGridView1.DataSource = new List<CompanyModel> { company };
or
var listCompanies = new List<CompanyModel> { company };
dataGridView1.DataSource = listCompanies.Select(c =>
new
{
License = c.CompanyLicense,
Name = c.CompanyNameAr,
SectorName = c.Sector.Name//Todo:set your property
}).ToList();
Upvotes: 1