Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hello All,
 
What I am trying to achieve is basically to display my calculation results by means of a datagridview control on a form. However, the issue is that the datagridview remains blank when its DataSource = my object. Please see below:
 
My Object :
 

 [Serializable()]
    public class PipeDetails
    {
 
        [CategoryAttribute("Id"), DescriptionAttribute("Id")]
        [XmlElementAttribute("Id")]
        public string Id { get; set; }
 
        [CategoryAttribute("Diameter"), DescriptionAttribute("Diameter")]
        [XmlElementAttribute("Diameter")]
        public double diameter { get; set; }
 
        [CategoryAttribute("thickness"), DescriptionAttribute("Coin Thickness")]
        [XmlElementAttribute("thickness")]
        public double thickness { get; set; }
 
 
        [CategoryAttribute("ShippedCountry"), DescriptionAttribute("Shipped Country")]
        [XmlElementAttribute("ShippedCountry")]
        public string Country { get; set; }
 
 
        [CategoryAttribute("pipeName"), DescriptionAttribute("Pipe Name")]
        [XmlElementAttribute("pipeName")]
        public List<string> pipeName = new List<string>();
 
 
        [CategoryAttribute("pipeCapacity"), DescriptionAttribute("pipe Capacity")]
        [XmlElementAttribute("pipeCapacity")]
        public List<double> pipeCapacity = new List<double>();
 
    }
 
My Declaration:
 
public static PipeDetails selectedPipe=new PipeDetails();
 
The above object is then populated.
 
Now the issue raised when I implement the following :
 
private void button1_Click(object sender, EventArgs e)
 
{
this.dataGridView3.DataSource= selectedPipe;
}
 
Many thanks in advance,
 
[edit]Code block added - OriginalGriff[/edit]
Posted 20-Nov-12 22:56pm
Edited 20-Nov-12 23:06pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Just read this article
 
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.datasource.aspx[^]
 
under the remarks heading you will see what types the datasource supports and a class I'm afraid isn't one of them.
 
Also from having a simple test application if you converted the SelectedPipe into a list you will not get the properties that you have set to a list.
  Permalink  
Comments
Member1978 at 21-Nov-12 6:29am
   
Many thanks for your reply. However, issue I have is that I have used two list type variables in my class. If I were to remove the aforementioned then the datagridview works with no issue raised. I am thinking maybe I should re-think about the list variables within my class.
Simon_Whale at 21-Nov-12 9:31am
   
Sadly I dont think there is a way as I know datagridview uses reflection to build up the rows from the datasource.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

update your code as below.
private void button1_Click(object sender, EventArgs e) 
{
   this.dataGridView3.DataSource= selectedPipe;
   this.dataGridView3.DataBind();
}
This may help you.
  Permalink  
v2
Comments
Member1978 at 21-Nov-12 6:26am
   
Thanks for your suggestion. I should have mentioned. I am using winForm not webForm as a result there is no DataBind() method.
Mohd. Mukhtar at 21-Nov-12 6:39am
   
try to use refresh or update of gridview control.
Member1978 at 21-Nov-12 7:32am
   
Many thanks for your help. I use them methods prior to my question. I believe my issue is that I have list variables within my class? If somehow there was a work-around?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

As declared before, the issue was raised due to multiple list<> variables within my class. In order to resolve this, I used DataTable (DataTable dt=new DataTable();). Once this TableData was populated then it was just a matter of implementing the following:
 
this.dataGridView3.DataSource = dt;
 

My sincere gratitude go to individuals with their recommendations put forward to this issue.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 355
1 Maciej Los 180
2 Richard MacCutchan 115
3 arvind mepani 104
4 Tino Fourie 92


Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 23 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid