How do I read a Datagrid, line by line?

Asked

Viewed 1,692 times

2

I have a DataGrid (WPF) filled by an external data source. I need to rotate the entire grid, checking the values of each row, column by column.

  • How is it possible to do this?
  • What is the "heart" of grid data?
  • I can turn the data into a collection and then use a foreach to rotate and consult?
  • I’ll have access to all the columns like this?
  • Why not query the data directly? The grid is UI and should only be used by the user and not by application logic.

  • Translate at intern level (laughs) and put as a response, I’m grateful.

  • What you’re doing is the equivalent of printing the data on paper and then doing OCR to process the data. You must process the data directly from the data source. Same as you assigned to the grid.

  • Oh yes, I get it, I also think it’s unnecessary to play on the grid, it’s just one more process, but I was asked so...

  • And the reason I’m taking data from the grid is learning, I don’t do grids, I need to learn to manipulate a little...

1 answer

4


If the data has a type defined as shown in this example (Model), you can recover the items in various ways, see the example:

Note: if you do not have data type the last example is the most appropriate

Example:

Model:

public class Modelo
{
    public int Id { get; set; }
    public String Nome { get; set; }
}

Filling in:

GridDados.AutoGenerateColumns = true;
GridDados.ItemsSource = new List<Modelo>(){
    new Modelo(){Id = 1, Nome = "Fulano 1"},
    new Modelo(){Id = 2, Nome = "Fulano 2"},
};

Recovering

List<Modelo> modelos = GridDados.Items.OfType<Modelo>().ToList();

or

foreach (Modelo modelo in GridDados.Items.OfType<Modelo>().ToList())
{
    int id = modelo.Id;
    String nome = modelo.Nome;
}

or

If the data is an anonymous type:

GridDados.ItemsSource = new object[]{
            new {Id = 1, Nome = "Fulano 1"},
            new {Id = 2, Nome = "Fulano 2"},
        }.ToArray();

foreach (dynamic item in GridDados.Items)
{
    int id = item.Id;
    String nome = item.Nome;
}

Debugging to find the item:

inserir a descrição da imagem aqui

The dynamic in this specific case will assume at runtime the type that each item in the list. Like this Collection has a type of class Modelo the variable item assumed that guy. That is, "The Dynamic type is a static type that is defined by the Dynamic reserved word and the variable of this type can be ALL in principle, and at compile time the Dynamic type assumes any operation." (Marcorrati.Net, C# - The data type Dynamic, 2014. Available at: http://www.macoratti.net/11/02/c_din1.htm. Date Accessed: 26.Jun.2014)

or

With Datarowview:

foreach (DataRowView item in GridDados.Items.OfType<DataRowView>().ToArray())
{
      string CDESC_INEP = item["CDESC_INEP"].ToString();                    
}

References:

  • In my case the Grid is filled from a DBF file. I tried to use, as you said in case the data do not have type, the last option. Man item has no access to any property, I think because the type is set at runtime according to what you gave me regarding Dynamic. If I type the properties (column header?) it works?

  • So, the last one is for this let’s say that your model is anonymous type Dynamic takes the items of each position, I will edit and put this example

  • I re-edited the last item, anything on that line foreach (dynamic item in GridDados.Items) put a breakpoint and pick up the guy who’s going out

  • 1

    my items are Datarowviews... I was able to access the values by properties like this here DataRowView drv = dgdDados.Items[1] as DataRowView;&#xA;txtCaminhoDbf.Text = drv["CDESC_INEP"].ToString();

  • @Caiquec. just put it like this in place of Model put Datarowview: like this: foreach (DataRowView item in GridDados.Items.OfType<DataRowView>().ToList()). Logic is the same as in the example only the type you must specify!

  • 1

    Um, okay, thank you very much, I’ll test it here.

  • I’ll edit it for you...

  • 1

    It worked perfectly! Thank you, it was very useful, I do not know if the problem is me, but it is difficult to find articles on data manipulation in grids... Maybe it’s easy and I’m getting beat up

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.