Populating a DataGridView with items from a visio.datarecordset

Started by RhesusMinus, January 27, 2011, 01:09:19 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

RhesusMinus

Hi.

This is probably something really easy that I'm missing here.

I need for the user to select something from my datalinked excel sheet, so I need to display everything (and will also create a filter to make it easier to find what one is looking for) in a DataGridView control.

I'm trying to just bind it like this:
DataGridView1.DataSource = visApplication.ActiveDocument.DataRecordsets.Item(1)

but nothing displays in the grid.
The visApplication.ActiveDocument.DataRecordsets.Item(1) is of course up and running....

Any hints and pointers for a n00b at this?

THL

aledlund

As a thought, you're attempting to combine data based on datarecordsets/datarecord into a control that is looking for datasets/datatables. Have you wrapped it in a try...catch...endtry to see if it is throwing an error?
al

RhesusMinus


Nikolay

Quote from: RhesusMinus on January 27, 2011, 01:09:19 PM
I'm trying to just bind it like this:
DataGridView1.DataSource = visApplication.ActiveDocument.DataRecordsets.Item(1)
Any hints and pointers for a n00b at this?

AFAIK, Visio does not provide .NET-compatible datasets, its shape-data functionality is actually based on the plain old (unmanaged) ADO "recordsets" (it's a different thing),
and this means that actually ActiveDocument.Recrodsets.Item(1) cannot be directly used as a data source for a .NET GridView
I think you might need to create a .NET DataSet and fill it from the Visio-provided recordset, like this:


    DataRecordset dataRecordset =
        applicationObject.ActiveDocument.DataRecordsets[1];;

    // key point - grab Visio data
    string data = dataRecordset.DataAsXML;

    // create ADO stream to initialize ADO recordset
    ADODB.Stream stream = new ADODB.StreamClass();
    stream.Open(missing, ConnectModeEnum.adModeUnknown, StreamOpenOptionsEnum.adOpenStreamUnspecified, "", "");
    stream.WriteText(data, StreamWriteEnum.adWriteChar);
    stream.Position = 0;

    // create ADO recordset and load from the ADO stream
    ADODB.Recordset adodbRecordSet = new ADODB.RecordsetClass();
    adodbRecordSet.Open(stream, missing, CursorTypeEnum.adOpenUnspecified, LockTypeEnum.adLockUnspecified, -1);

    stream.Close();
    Marshal.ReleaseComObject(stream);

    // create .NET DataSet and fill it from the ADO recordset
    OleDbDataAdapter adapter = new OleDbDataAdapter();
    DataSet dataSet = new DataSet("MyTable");
    adapter.Fill(dataSet, adodbRecordSet, "MyTable");

    Marshal.ReleaseComObject(adodbRecordSet);

    Form1 form1 = new Form1();
    form1.dataGridView1.DataSource = dataSet;
    form1.dataGridView1.DataMember = "MyTAble";
   
    form1.ShowDialog();


Note that you need to add reference to ADODB (Add Reference -> COM -> Microsoft ActiveX Data Objects 2.xxx) to the project for this code to work
Attached file is a sample project that creates .NET DataSet out of Visio DataRecordset.
Maybe this can be done easier, maybe not (sure the code should be much shorter in .NET 4.0)

RhesusMinus

Thank you so much for the great example. It worked like a charm.
I've even found out how to bind the dataset to a BindingSource, and then bind the gridviews datasource to this BindingSource. Makes it real easy to have a textbox were one can write something, and the datagridview filters away rows as I type!

But.. One little thing here...

If I bind the DataSet directly to the DataGridView, it populates the grid in milliseconds. When I bind the BindingSource to the DataGridView, it takes 4-5 seconds to populate. Do you by chance know why? (kind of irritating :))

THL

Nikolay

Quote from: RhesusMinus on January 28, 2011, 12:13:32 AM
If I bind the DataSet directly to the DataGridView, it populates the grid in milliseconds.
When I bind the BindingSource to the DataGridView, it takes 4-5 seconds to populate. Do you by chance know why? (kind of irritating :))

Nope, I've no idea. I can only say that most probably it should not be like that :)