locked
How To add new column to datatable from combobox item RRS feed

  • Question

  • Hello
      i want to add new column to datatable from combobox all items
      my code is

     Dim newColumn As New Data.DataColumn("Mån", GetType(System.String))
    
            newColumn.DefaultValue = ComboBox1.Items.ToString
            dt.Columns.Add(newColumn)
    
            DataGridView3.DataSource = dt

    but is not working

    column is add but all items are System.Windows.Forms.ComboBox + ObjectCollection
    any help thanks
    Saturday, December 12, 2020 3:30 PM

All replies

  • Check this modification: 

    newColumn.DefaultValue = ComboBox1.SelectedItem


    Saturday, December 12, 2020 4:44 PM
  • Change your code to Visual basic please, but the comcept is the same

    dt.Columns.Add("Man", typeof(System.String));

    foreach(DataRow row in dt.Rows) { //need to set value to NewColumn column row["Man"] = ComboBox1.Items.ToString(); // or set it to some other value }

     DataGridView3.DataSource = dt



    A flower cannot blossom without sunshine, and man cannot live without love.

    Saturday, December 12, 2020 4:48 PM
  • Change your code to Visual basic please, but the comcept is the same

    dt.Columns.Add("Man", typeof(System.String));

    foreach(DataRow row in dt.Rows) { //need to set value to NewColumn column row["Man"] = ComboBox1.Items.ToString(); // or set it to some other value }

     DataGridView3.DataSource = dt



    A flower cannot blossom without sunshine, and man cannot live without love.

    not working

    column is add but all items are System.Windows.Forms.ComboBox + ObjectCollection

    I want the combobox item text 

    Saturday, December 12, 2020 5:10 PM
  • Hi,
    try this:

      Dim newColumn As New Data.DataColumn("Mån", GetType(System.String))
    
      newColumn.DefaultValue = ComboBox1.Text
      dt.Columns.Add(newColumn)
    
      DataGridView3.DataSource = dt


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Saturday, December 12, 2020 6:14 PM