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

jcrcarmo

Active member
Joined
Dec 14, 2005
Messages
32
Hello everyone,

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! Heres the code:

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 Im looking forward to your reply.

Best regards,

JC Carmo.
 
Do you have the values stored someplace else that you can use? Otherwise Im not sure how you could NOT hard code it?
Heres an alternate way to load some data:

C#:
           DataRow row = sementesDT.NewRow();
           row["CodSemente"] = 0;
           row["CodTipo"] = 0;
           row["Semente"] = "SubCat0-Cat0";
           sementesDT.Rows.Add(row);

Now, picture that in a loop where you can change each value. You can, of course, use your code to do that in a loop.

If you have the SubCat-Cat data in another format (a file, database, enum, etc.) then let us know.

-ners
 
Back
Top