Datarow.setvalue maybe simple but

M

Michael

Hello,

I am trying to add some kind of counter in a datatable, i use SetValue but
it don't semm to work.

Here is the code

System.Data.DataSet dsClubs = new System.Data.DataSet();

monDA.Fill(dsClubs, "Clubs");

//Ajout de la colonne Nombre de combattants par club

System.Data.DataColumn cNbrComp = new System.Data.DataColumn();

cNbrComp.ColumnName = "NbCompetiteur";

cNbrComp.DefaultValue = 0;

cNbrComp.DataType = System.Type.GetType("System.Int32");

dsClubs.Tables["Clubs"].Columns.Add(cNbrComp);



// Définition de la clé primaire

System.Data.DataColumn[] cles = new System.Data.DataColumn[1];

cles[0] = dsClubs.Tables["Clubs"].Columns[0];

dsClubs.Tables["Clubs"].PrimaryKey = cles;




// Récupération du nombre de combattants par clubs

for (int i=0;i<monDS.Tables["Competiteur"].Rows.Count;i++)

{

System.Data.DataRow tempLigne =
dsClubs.Tables["Clubs"].Rows.Find(monDS.Tables["Competiteur"].Rows.ItemArray[5])
;

// I Search for a spécific Row in Clubs, it works fine, but the next line
don't seem to work, as the value stay to 0

tempLigne.ItemArray.SetValue( (int) (tempLigne.ItemArray.GetValue(2)) + 1,
2);

System.Windows.Forms.MessageBox.Show(tempLigne.ItemArray[2].ToString());

}


The Column is not in Readonly, so i should be able to modify it no ?

Thanks for your answers.

Michael
 
C

Cor Ligthert

Michael,

When I see your code than I get the idea that you are searching for
something that is easy to find using the dataview.find. Can you explain what
you try to achieve?

I assume that you don't set that value for nothing, there must be a reason
for that..

Cor
 
M

Michael

I have a list of persons (Tables["Competiteur"]) who belongs to some judo
clubs, in this list i will have maybe 5 people from 1 club, 3 from an other,
and 2 from again an other one.

I have also a Table (Tables["Clubs"]) with the list of all Clubs possible.

And i want for the purpose of another algorithm, to know for each club how
many people i have in the previous list.

So i get the Dataset corresponding to the Clubs Table, i had a column which
will be my counter "NbCompetiteur", i set the default value to 0.

I look now at the list of people, and for each club i want to increment the
counter, that's why i use the Find method, and the the SetValue method.

Whith This i have the correct value : 1 (0 +1)
(int) (tempLigne.ItemArray.GetValue(2)) + 1

But i cannot Put this with the SetValue method, it don't seem to work.

I hope it is a little bit clearer.
 
C

Cor Ligthert

Michael,

You can use the datarelation for this or the dataview. Where I would take
the dataview
Roughly typed

\\\
DataView dv = new DataView(....Tables["Persons"]);
dv.RowFilter = "ClubName = '" + (....Tables["Clubs"].Rows[x]["ClubName"] +
"'"
int persons = dv.count;
///

I hope I understood you well

There are more methods by instance the datatable select which gives you a
collection of affected datarows and which you can use almost the same as
above.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Hide Column in Datagrid 2

Top