C# .Net 2005 - Question about .Row.Add(new object[]{...,...}) Please help!

csharp Brazil
  • 15 years ago

    Hello everyone,

    Greetings from Brazil!  As shown in the code below, is it possible for me to add the new objects for tipoDT and sementesDT without having to do it one-by-one?  Like, for example, getting the values automatically from the tables?....  How would I do that?  The sementesDT table is quite large and would take me forever to add the new objects one-by-one!  Here's the code:

           public frmBA()
           {
               tipoDT = new DataTable("tabTipoSemente");
               tipoDT.Columns.Add("CodTipo", typeof(int));
               tipoDT.Columns.Add("Tipo", typeof(string));



               tipoDT.Rows.Add(new object[] { 0, "Nocivas Probidas" });
               tipoDT.Rows.Add(new object[] { 1, "Nocivas Toleradas" });
               tipoDT.Rows.Add(new object[] { 2, "Sementes Silvestres" });

               sementesDT = new DataTable("tabSementes");
               sementesDT.Columns.Add("CodSemente", typeof(int));
               sementesDT.Columns.Add("CodTipo", typeof(int));
               sementesDT.Columns.Add("Semente", typeof(string));


               sementesDT.Rows.Add(new object[] { 0, 0, "SubCat0-Cat0" });
               sementesDT.Rows.Add(new object[] { 1, 0, "SubCat1-Cat0" });
               sementesDT.Rows.Add(new object[] { 2, 0, "SubCat2-Cat0" });
               sementesDT.Rows.Add(new object[] { 3, 1, "SubCat3-Cat1" });
               sementesDT.Rows.Add(new object[] { 4, 1, "SubCat4-Cat1" });
               sementesDT.Rows.Add(new object[] { 5, 1, "SubCat5-Cat1" });
               sementesDT.Rows.Add(new object[] { 6, 2, "SubCat6-Cat2" });
               sementesDT.Rows.Add(new object[] { 7, 2, "SubCat7-Cat2" });
               sementesDT.Rows.Add(new object[] { 8, 2, "SubCat8-Cat2" });







               InitializeComponent();

               tipoBS = new BindingSource();
               tipoBS.DataSource = tipoDT;
               TipoComboBoxColumn.DataSource = tipoBS;
               TipoComboBoxColumn.DisplayMember = "Tipo";
               TipoComboBoxColumn.ValueMember = "CodTipo";



               unfilteredSementesBS = new BindingSource();
               DataView undv = new DataView(sementesDT);
               unfilteredSementesBS.DataSource = undv;
               EspecieComboBoxColumn.DataSource = unfilteredSementesBS;
               EspecieComboBoxColumn.DisplayMember = "Semente";
               EspecieComboBoxColumn.ValueMember = "CodTipo";




               filteredSementesBS = new BindingSource();
               DataView dv = new DataView(sementesDT);
               filteredSementesBS.DataSource = dv;
           }



    Thank you very much for your attention, time and help and I'm looking forward to your reply.

    Best regards,

    JC Carmo  :)

  • 15 years ago

    Hi,

    I assume when you mention using a database you mean that the list of items to add would be from a database?

    If this is the case then the most obvious choice would be to create a datatable using a query directly on the database and then binding the combobox to this data table directly as you have already done?

    Maybe you can't create the datatable in this way? If you can't then the only solution is can think of is to use a for loop or a foreach loop to iterate though your items you wish to be added and then add then to the datatable in the following way:

    foreach (object[] Item in ItemGroup)

    {

    DataRow dr = tipoDT.NewRow();

    dr[0] = Item[0]; //e.g. 0

    dr[1] = Item[0]; //e.. Nocivas Toleradas

    tipoDT.Rows.Add(dr);

    }

    This foreach loop will add one row each time. So should cover what you need.

  • 15 years ago

    Hi djohns,

    Thank you very much for your reply.  Yes, you assumed correctly! I tried a different approach and now it works perfectly using a DataSet and applying a filter to it, but thanks a lot for your answer.  I'll sure keep it in my code vault for future reference.

    Best regards,

    JC :)

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The generation of random numbers is too important to be left to chance.” - Robert R. Coveyou