Search code examples
c#datatabledatatablesprimary-key

Issue with setting primary key for DataTable merge, over combobox WPF in C#


Combobox gets populated as such:

foreach (DataColumn item in Dt_Masterlist.Columns)
{
    Combo_Master_Primary.Items.Add(item.ColumnName);
}

Now im trying to get the selected column from the combobox to set it as primary key to later merge 2 datatables with each other.

DataColumn primarymaster;

//getting the DataColumn[] from Columns with the choosen Columnname by Combobox

foreach (DataColumn item in Dt_Masterlist.Columns)
{
    if (item.ColumnName == Combo_Master_Primary.SelectedItem.ToString())
    {
        primarymaster = item;
        Dt_Masterlist.PrimaryKey = item;
    }
}

Dt_Masterlist.Merge(Dt_NewList);  

I get the following error: "Type System.Data.DataColumn cant be converted implicit to System.Data.DataColumn[]"

How could i fix this issue?

Greetings SaltTM


Solution

  • Came up with a solution, got confused by the "new" tag.

    int count = Dt_Masterlist.Columns.Count;
    for (int i = 0; i < count; i++)
    {
         if (Dt_Masterlist.Columns[i].ColumnName == Combo_Master_Primary.SelectedItem.ToString())
         {
              Dt_Masterlist.PrimaryKey = new DataColumn[] {Dt_Masterlist.Columns[Dt_Masterlist.Columns[i].ColumnName]};
         }
    
    }