简体   繁体   中英

How to read from DataGrid Column Cells?

I'm messing with this problem for about 2 days now and searched on many boards for a solution to solve the problem :(

I wrote via linq XML Attributes in my DataGrids Column named "Betrag" .

Now I want to get all of those Entries and then sum them up to one number ( all entries of the column are numbers!).

I hope somebody can help me with this problem.

Best Regards, Fabian

Now some code :

        data = new List<Daten>();
        data = (from datensatz in doc1.Descendants("datensatz")

                            select new Daten

                            {
                                //datum = "27.6.2012",
                                datum =datensatz.Attribute("datum").Value,
                                //zweck = "Eröffnung",
                                zweck =datensatz.Attribute("zweck").Value,
                                //empfang = benutzer,
                                empfang =datensatz.Attribute("empfang").Value,
                                //betrag = "0€"
                                betrag =datensatz.Attribute("betrag").Value + "€"

                            }).ToList();


                this.Daten.ItemsSource = data;
                //THIS CODE ADDS THE ATTRIBUTES TO MY GRID

then I tried this :

kontostand += Convert.ToInt32(Daten.Columns[3].GetCellContent(1).ToString());

Why not just do something like this...

var sum = data.Sum(item=>item.betrag);//you might have to parse as number.

you could put that value in a property on the page and then put a databinding expression wherever you want to display the value.

I think you should avoid trying to sum the values in the cells.

Also, I think you should make the betrag property an integer, if possible. You could always add the symbol by using String.Format on the code in front.

This :

kontostand += Convert.ToInt32(Daten.Columns[3].GetCellContent(1).ToString());

Should be like this if its an asp grid:

kontostand += Convert.ToInt32(Daten.Rows.Cells[3].innerText);

If not then you need to loop the rows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM