Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C# .Net 2005 - Question about .Row.Add(new object[]{})
23-02-2006, 09:22 AM
Post: #1
C# .Net 2005 - Question about .Row.Add(new object[]{})
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:

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 Smile
Send this user an email Find all posts by this user
Quote this message in a reply
23-02-2006, 09:34 AM
Post: #2
Re: C# .Net 2005 - Question about .Row.Add(new object[]{})
jcrcarmo Wrote: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?

What tables? If you mean that all these things are stored in a database, then yes, that's easy to use as your datasource.

Hopefully, you have something along the lines of SQL Server. If you have Access or a spreadsheet or something horrible like that, then I would suggest downloading SQL 2005 Express, which is free from Microsoft and importing into a fresh database, as it will frankly save you much time and hassle.

The class library of choice is, IMHO, SqlHelper. It's very good for one-liners that read the results of a SQL stored procedure into a datatable object. From there you can do with the tables as you like, really. The following article seems ok for an intro:
http://aspnet.4guysfromrolla.com/articles/070203-1.aspx

Hope that helps!

*edit*

Alternatively your SementesDT data seems to have a very common pattern:

Code:
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" });

Why not set up a loop that adds objects three at a time, and builds the final string as it goes? E.g...

Code:
int groupCount = 0;

for (int i = 0; i < someNumber; i += 3)
{
           sementesDT.Rows.Add(new object[] { i, groupCount, "SubCat" + i + "-Cat" + groupCount });
           sementesDT.Rows.Add(new object[] { i+1, groupCount, "SubCat" + i + "-Cat" + groupCount });
           sementesDT.Rows.Add(new object[] { i+2, groupCount, "SubCat" + i + "-Cat" + groupCount });

           groupCount += 1;
}

Give me liberty, or give me death - Patrick Henry 1775.
Think for yourselves and let others enjoy the privilege to do so too - Evelyn Beatrice Hall 1906
I spread my wings, only to find that they are paper in the rain - Neko 2006
Send this user an email Visit this user's website Find all posts by this user
Quote this message in a reply
23-02-2006, 10:03 AM
Post: #3
 
Hi Neki,

Thanks for your quick reply. The table I'm talking about is the tabSementes:

sementesDT = new DataTable("tabSementes");

as referenced in the code I posted. I will study the code that you posted, but please don't overestimate my C# habilities. I'm still a newbie! Hehehehe ... Thanks!

JC Smile
Send this user an email Find all posts by this user
Quote this message in a reply
23-02-2006, 12:00 PM
Post: #4
 
Heh no prob, everyone starts somewhere Smile

I think I'm a little lost though... is it that you want to be able to duplicate data from one table to another?

Give me liberty, or give me death - Patrick Henry 1775.
Think for yourselves and let others enjoy the privilege to do so too - Evelyn Beatrice Hall 1906
I spread my wings, only to find that they are paper in the rain - Neko 2006
Send this user an email Visit this user's website Find all posts by this user
Quote this message in a reply
23-02-2006, 03:40 PM
Post: #5
 
Hi Neko,

Let me explain of I'm trying to do: the code I posted filters the data displayed in a comboBoxColumn in a DataGridView depending on the value I select a from another comboBoxColumn in the same DataGridView. Everything is working, BUT if I have to manually enter more than 500 values for the second ComboBoxColumn, it would leave much room for mistakes, since the names are all in Latin.

My tabSementes doesn't really have a common pattern. That was just an example. Here's a more accurate representation:

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, "Acanthospermum australe" });
           sementesDT.Rows.Add(new object[] { 1, 0, "Bidens pilosa" });
           sementesDT.Rows.Add(new object[] { 2, 0, "Borreria capitata" });
           sementesDT.Rows.Add(new object[] { 3, 1, "Digitária sanguinalis" });
           sementesDT.Rows.Add(new object[] { 4, 1, "Hiptis brevipes" });
           sementesDT.Rows.Add(new object[] { 5, 1, "Pennisetum setosum" });
           sementesDT.Rows.Add(new object[] { 6, 2, "Senna occidentalis" });
           sementesDT.Rows.Add(new object[] { 7, 2, "Stachytarpheta }); polyura" });
           sementesDT.Rows.Add(new object[] { 8, 2, "Xantium spp" });
           .
           .// I would have to do the same for 500 more values... See?
           .

Do you have any suggestions? Thanks a lot!

JC
Quote this message in a reply
23-02-2006, 03:46 PM
Post: #6
 
Hi Neko,

It's me again. I forgot to ask you this: is it possible to do that loop to load the values if theuy are all different strings? What would be the code for it? Thanks a million! JC Smile
Send this user an email Find all posts by this user
Quote this message in a reply
23-02-2006, 09:15 PM
Post: #7
 
What to do now depends on what form you have your data for sementes. If it's a database, we can do that easily, see above post on SqlHelper Smile

Spreadsheets, text files, or XML can all be read through various means, but none is as friendly. It is however, better than hardcoding them all, as it would let you add rows or correct spelling errors without recompiling the code.

You can't loop for unpredictable strings - you'd still have to load the proper strings from somewhere.

Give me liberty, or give me death - Patrick Henry 1775.
Think for yourselves and let others enjoy the privilege to do so too - Evelyn Beatrice Hall 1906
I spread my wings, only to find that they are paper in the rain - Neko 2006
Send this user an email Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump: