문제

I'm attempting to create a small desktop application (using Visual Studio) that reads xml files from a games resource files. These files are stored in XML. When I try to read the file into a datagridview I receive an error:

System.ArgumentException: Child list for field SpaceUnitsCapital cannot be created.
at System.Windows.Forms.BindingContext.EnsureListManager(Object dataSource, String dataMember)
at System.Windows.Forms.DataGridView.DataGridViewDataConnection.SetDataConnection(Object dataSource, String dataMember)
at System.Windows.Forms.DataGridView.set_DataMember(String value)
at SWFOC.Form1.clk(Object sender, EventArgs e) in C:\Users\Number6\documents\visual studio 2012\Projects\SWFOC\SWFOC\Form1.vb:line 37

The code im using to attempt this is:

Dim URL As String = "C:\Program Files (x86)\LucasArts\Star Wars Empire at War Forces of Corruption\Data\XML\Spaceunitscapital.xml"
    Dim ds As DataSet = New DataSet()
    Dim fsReadXml As System.IO.FileStream = New System.IO.FileStream(URL, System.IO.FileMode.Open)

    Try
        ds.ReadXml(fsReadXml)
        DataGridView1.DataSource = ds
        DataGridView1.DataMember = "SpaceUnitsCapital"
    Catch Err As Exception
        RichTextBox1.Text = Err.ToString
    Finally
        fsReadXml.Close()
    End Try

Here is a sample of what the xml files look like:

<?xml version="1.0"?>
<SpaceUnitsCapital>

<SpaceUnit Name="Calamari_Cruiser">
    <Text_ID>TEXT_UNIT_CALAMARI_CRUISER</Text_ID>
    <Encyclopedia_Good_Against> Victory_Destroyer Acclamator_Assault_Ship Interceptor4_Frigate </Encyclopedia_Good_Against>
    <Encyclopedia_Vulnerable_To> TIE_Bomber Skipray_Blastboat </Encyclopedia_Vulnerable_To>
    <GUI_Row> 1 </GUI_Row>
    <Space_Model_Name>RV_MONCALCRUISER.ALO</Space_Model_Name>
    <Select_Box_Scale>600</Select_Box_Scale>
    <Select_Box_Z_Adjust>-50</Select_Box_Z_Adjust>
    <Mass>0.995</Mass>
    <Scale_Factor>1.1</Scale_Factor>
    <Damage>60</Damage>
    <Dense_FOW_Reveal_Range_Multiplier>0.24</Dense_FOW_Reveal_Range_Multiplier>
    <Visible_On_Radar_When_Fogged>true</Visible_On_Radar_When_Fogged>
    <Multisample_FOW_Check>Yes</Multisample_FOW_Check>
    <Ranking_In_Category>5</Ranking_In_Category>
    <Max_Speed>1.5</Max_Speed>
    <Max_Rate_Of_Turn>0.4</Max_Rate_Of_Turn>
    <MovementClass> Space </MovementClass>
    <Space_Layer> Capital </Space_Layer>
    <Layer_Z_Adjust>-290.0</Layer_Z_Adjust>
    <OverrideAcceleration> .02 </OverrideAcceleration>
    <OverrideDeceleration> .02 </OverrideDeceleration>

    <Armor_Type> Armor_Calamari_Cruiser </Armor_Type>
    <Shield_Armor_Type>Shield_Capital</Shield_Armor_Type>
    <Max_Rate_Of_Roll>0.2</Max_Rate_Of_Roll>
    <Bank_Turn_Angle>25</Bank_Turn_Angle>   
    <Max_Thrust>0.2</Max_Thrust>
    <Hyperspace>Yes</Hyperspace>
    <Hyperspace_Speed>1</Hyperspace_Speed>
    <Maintenance_Cost>0.3</Maintenance_Cost>
</SpaceUnit>
</SpaceUnitsCapital>

If I attempt to run the code without the line that fills the datamember it does not throw an error but it also doesn't do anything else. It never populates the DGV.

Does anyone know how I can avoid this error? The end result is to somehow populate the fields into the app in some form so that they can then be edited and saved so I am open to other methods if anyone has any ideas.

Thanks in advance, Chris

도움이 되었습니까?

해결책

Based on your comments and since my comment worked I repeat my solution here:

DataGridView1.DataSource = ds.Tables(0)

Your question why my code worked is actually a question regarding the difference between a dataset and a datatable.
You can consider that a dataset is a set of datatables, or in other words imagine dataset something like a database and the datatable a table in that database.
So you should set as a datasource to your grid a datatable and not a set of tables

다른 팁

I got this message because I specify the data source to use in the control property with a DataSet and then (because I forgot about that), I tried to assign a DataSource in the code.

Then I discovered that the DataSet specified in the property was not correct. I then removed the binding in the control properties and then it worked.

If you got this message, check your DataSet binded to the control you are having the problem.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top