[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

using DatagridViewColumn's ValueType in ColumnAdded Event

Posted on 2010-01-04
10
Medium Priority
?
1,582 Views
Last Modified: 2013-11-08
Hi experts

I have a databound datagridview in which I need to know a Columns underlying valuetype (datapropertyname is set) whenever I add a column.
The obvious choice to me was to use the ColumnAdded-Event, but the ValueType property of the column is always NULL.

Is there a way to get the underlying datatype as soon as I add a Column.

P.S.
it's about a WinForm-Application
Type DataType = DgvColumn.ValueType.UnderlyingSystemType;

Open in new window

0
Comment
Question by:Arikael
  • 6
  • 4
10 Comments
 
LVL 13

Expert Comment

by:kaylanreilor
ID: 26170935
What is the type of DgvColumn ? Who did set the property according to you ? Is it set by the fact of a data binding ? If yes, how did you actually data bind ?
0
 
LVL 3

Author Comment

by:Arikael
ID: 26170972
Hi

It's a textboxcolumn.
the DatagridViews DataSource is a BindingSource which points to a table inside a strongly typed dataset

//constructor
public MetaMainList(TtReportsDataSet TtReports, BindingSource BindingSource)
        {
            InitializeComponent();

            this.TtReports = TtReports;
            this._MetaBindingSource = BindingSource;
            this._MetaBindingSource.DataSource = this.TtReports;
            
            this.MainListEnhancedDataGridView.AutoGenerateColumns = false;
            this.MainListEnhancedDataGridView.NonExistantColumnBehaviour = NonExistantColumnBehaviour.Add;
        }

//load event
private void MetaMainList_Load(object sender, EventArgs e)
        {
            try
            {
                this._MetaBindingSource.DataMember = this.ModuleDataMember;
                this.MainListEnhancedDataGridView.DataSource = this.MetaBindingSource;
            }
            catch(ArgumentException)
            {
                ErrorMessage.Show("Konnte das Modul nicht laden", "Modul fehlerhaft definiert");
                this.Close();
            }

            this.ExportToExcelToolStripMenuItem.Click += new EventHandler(ExportToExcelToolStripMenuItem_Click);
            this.MainListEnhancedDataGridView.DataGridViewFilter = new DataGridViewFilter(new DataGridViewFilterDbAdapter(this.TtReports.ConnectionString));
        }

Open in new window

0
 
LVL 3

Author Comment

by:Arikael
ID: 26171008
Oh, and I should add that I'm using a custom DataGridView which inherits from DataGridView. But even when I add the columns like shown below, it doesn't work.


DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
            col.DataPropertyName = "idJob";
            col.HeaderText = "Auftrags-Nr1";
            this.MainListEnhancedDataGridView.Columns.Add(col);

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:kaylanreilor
ID: 26172018
According to the doc, it seems that you should set this property yourself : http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.valuetype.aspx when you add the column for example.
0
 
LVL 3

Author Comment

by:Arikael
ID: 26181201
hi

that is a possibility, but at somepoint the valuetype of a bound dataggridview column gets set according to the corresponding column in the dataset.
It seems that happens after the column is added.

Do you know when?
0
 
LVL 13

Expert Comment

by:kaylanreilor
ID: 26197577
No, I don't. But probably the first line of the column added event handler could be to call the directly the event handler of the base class and them examine what are the values.
But I doubt that you can do much manipulation with a data bound control : http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/6b83e893-eb5b-445f-bab5-d7191c10b5c9.
BTW, I still don't know what your BindingSource does look like.
0
 
LVL 3

Author Comment

by:Arikael
ID: 26197636
hi

thanks for your answer

Manipulation of ValueType is not necesseray, but I need to be able to get the correct ValueType when I add a new column.

how my BindingSource looks like?
Do you mean how instantiate it?

BindingSource MyBs = new BindingSource();
MyBs.DataSource = this.MyDataSet;
MyBs.DataMember = "MyDataTable";

where myDataSet is a strongly typed one.
0
 
LVL 13

Accepted Solution

by:
kaylanreilor earned 2000 total points
ID: 26200473
Well, no I meant the DataSet itself here. So you say that it is strongly type. So I guess you are confident that each column has got a type.
So we suppose here that after having added a column the type is set. On the other hand, the type of a column is the same for the whole table I guess. So probably you don't really need to know as soon as this event is raised.
But since you really want to examine this type here, did you try to call the OnColumnAdded method of the base class and them having a look to the ValueType property ?
0
 
LVL 3

Author Comment

by:Arikael
ID: 26200624
Hi

great hint with the OnColumnAdded. This works :-) thanks

Strangly, the OnColumnAdded and the ColumnAdded get called multiple times. Don't know what .NET is doing here.
0
 
LVL 3

Author Comment

by:Arikael
ID: 26291788
When I tested the OnColumnAdded-Solution I forgot that I earlier in the code manually set the valuetype, which led to the assumption the it worked.
Unfortunately this is not the case.

The only solution I found so far is the CollectionChanged event of the Column Collection.
There the ValueType is set. I still have no idea when the dgv sets the ValueType
//in constructor
this.Columns.CollectionChanged += new CollectionChangeEventHandler(Columns_CollectionChanged);
      

//ColletionChanged Event
        void Columns_CollectionChanged(object sender, CollectionChangeEventArgs e)
        {
            if(e.Element is DataGridViewColumn 
            {
                //do some stuff with ValueType (need to convert e.Element to a DataGridViewColumn)
}
        }

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month18 days, 22 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question