Save data from a Row to an array

Asked

Viewed 51 times

3

I’m developing a new functionality for a project Winforms in C#. Soon I developed the following method that returns itself in the component GridControl (gvDados) there is a line selected or not.

public bool RetornaSelecionado(bool Selecionado)
{
  int linhas = 0;

  foreach (int i in gvDados.GetSelectedRows())
  {
    DataRow row = gvDados.GetDataRow(i);       
    linhas++;
     //TESTE MessageBox.Show(row[0].ToString());
  }
  if(linhas > 0)
  {
    MessageBox.Show("Selecionou");
    return Selecionado = true;
  }
  else
  {
    MessageBox.Show("Não selecionou");
    return Selecionado = false;
  }
}

The method worked as I expected. When I select an item in the component the message is displayed Selected and when not selected, the message Did not select is displayed. After this a question arose, how should I proceed to store the data from row in one another array so that I can use in another system validation?

  • what is gvDados?

  • 1

    is the name of my Gridcontrol

  • Debug your code and get the information from DataRow, ai just assign in the array you want!

  • 1

    i did a more amateur act kkkkk... That line commented there in the code was to know which item (ID, Name, Sector) is being "caught" by DataRow, and is the ID...

2 answers

0

Create an array of the type you are using in the datasource. To get the type just give a cast.

MeuObjeto oObjeto = (MeuObjeto)gvDados.GetRow(i);

Then fill the array with these objects.

0


Just create a lits<t> where T is the class or type you want to store

Suppose it’s an int

Lits<int> meusDados =  new List<int>();

foreach (int i in gvDados.GetSelectedRows())
{
    DataRow row = gvDados.GetDataRow(i);       
    linhas++;
    meusDados.Add(Int32.Parse(row[0].ToString()));
    //TESTE MessageBox.Show(row[0].ToString());
}
  • 1

    Marcos, I started testing your answer here and I’ve already figured out another detail... How do I call this list in an event, for example?... Honestly I need to study Lists because I know nothing :/

  • How so you want to call her inside another function ?

  • 1

    Well if the event is within the same class you can put the list in the global scope ie, create this variable Lits<int> meusDados outside the function. If you are in another you can set to Static

Browser other questions tagged

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