Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
875
Key already exists error when setting DataSource
posted

I have a set of data that I am pulling from a database and putting into a datatable.  When I assign the datatable to the DataSource of my grid, I get the error: "Key already exists Parameter name: Key"

I have found that two columns in the .Net DataTable have the same name, but different cases.  Here is a short example to reproduce this:

1.  Add a grid to the form and just click "Finshed" when the wizard comes up.

2.  Add the following code:

        Dim dt As New DataTable("Test")

        Dim col1 As DataColumn = New DataColumn("Col 1", GetType(Double))
        col1.Caption = "Col 1"
        dt.Columns.Add(col1)

        Dim col2 As DataColumn = New DataColumn("COL 1", GetType(Double))
        col2.Caption = "COL 1"
        dt.Columns.Add(col2)

        UltraGrid1.DataSource = dt  '  <---  Exception will occur here

The names of the DataColumns only differ in case.  Aside from changing the names (since in the real world I will not have control of this and the names differing in case only is a potential in the data I have to use), is there a setting I am missing to keep this from happening.