How to get all the rows from DataGrid in WPF

时间:2017-12-18 06:06:06

标签: c# wpf datagrid

I have a DataGrid in my WPF application, I want to get all the row values in a list in a button click. I tried some ways but I get the last row values only...

private async void Save_Btn_Click(object sender, RoutedEventArgs e)
{
    pojo rowdata = new pojo();
    int rowcount = calendarmstrDG.Items.Count;
    List<pojo> pojolist = new List<pojo>();
    var rows = (calendarmstrDG).SelectedItems;
    for (int i = 1; i < rowcount - 1; i++)
    {
        pojo sda = (pojo)calendarmstrDG.SelectedItems;
        pojolist.Add(sda);
    }
}

Here calendarmstrDG is my datagrid name ... pojo is my model class name ...

public class pojo
{
    public string Prefix { get; set; }
    public int Year { get; set; }
    public int Quarter { get; set; }
    public int SerialNo { get; set; }
    public string From { get; set; }
    public string To { get; set; }
    public string PeriodName { get; set; }
}

2 个答案:

答案 0 :(得分:1)

iterate over all items using [HttpPost] public ActionResult EditDescription(int id, string Description) { var desc = db.StockBuys.Find(id); desc.Description = Description; db.SaveChanges(); return Content(Description); } loop:

foreach

DataGrid.Items collection contains elements of type foreach(pojo p in calendarmstrDG.Items) { // do something with "p", e.g. access properties: p.SerialNo } , so in object loop we have to specify exact type foreach to be able to access properties

if you need to get a new list of pojo, it can be done with Linq:

pojo

答案 1 :(得分:1)

You can use <h3>Your BorkCoins: <span id="bork-coins"></span></h3> <img src="https://i.pinimg.com/736x/ef/6a/cf/ef6acfc481b76637b71d4a71db7de82a--dog-birthday-animal-memes.jpg" height="80" width="80" id="bork-coins" onclick="earnBork();"> <p>Click on Gabe the Doggo to earn a BorkCoin!</p> <h3>Shop</h3> <p>Dank Miner <i>(2 BorkCoins/sec)</i> <br>Your Dank Miner(s): <span id="dankMiner"></span></p> <button id="dank-miner" onclick="buydankMiner1();">Buy 1 (20 BorkCoins)</button> to bind the data to DataGrid and you can retrieve the data using it without any effort. Something like this :

ObservableCollection