How to fill a datagridview with getProcess of c #?

0

How could you guide me on how to fill out a datagridview that I have on asp.net?

<asp:GridView ID="GridView1" runat="server" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" ShowHeaderWhenEmpty="true" Width="646px">
        <Columns>
            <asp:BoundField HeaderText="columna1" />
            <asp:BoundField HeaderText="columna2" />
            <asp:BoundField HeaderText="columna3" />
            <asp:BoundField HeaderText="columna4" />
        </Columns>
    </asp:GridView>

with a method of the GetProcesses class in c # like this:

Process[] processlist = Process.GetProcesses();

        foreach(Process theprocess in processlist){
        Console.WriteLine("Process: {0} ID: {1}", theprocess.ProcessName, theprocess.Id); 
        }
        Console.ReadLine();

so that what it shows in console is filled in the datagridview, thanks.

    
asked by matteo 27.04.2016 в 21:12
source

1 answer

1

To show in a DataGridview the data of a list you can use the property DataSource

In your case you could directly assign the result of Process.GetProcesses() although it is preferable to create an intermediate class with the data to be displayed

The code would be something like this ::

// Clase con la información que se necesita de un proceso
public class ProcessInfo
{
   public int Id { get; set; }     
   public string Name { get; set; }
}

// Evento de carga de la página
void Page_Load(Object sender, EventArgs e)
{
   if(!IsPostBack)
   {
       // Obtener información de los procesos seleccionando los datos a mostrar
       // es necesario añadir un using System.Linq
       var processList = Process.GetProcesses().Select(p => new ProcessInfo
       {
          Id = p.Id,
          Name = p.ProcessName
       });

       // Asignar la lista de procesos a la Grid
       GridView1.DataSource = processList;
       GridView1.DataBind();
   }     
}

This would be the HTML with which you would show the information in GridView

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowHeaderWhenEmpty="true" Width="646px">
    <Columns>
        <asp:BoundField datafield="Id" HeaderText="Identificador" />
        <asp:BoundField datafield="Name" HeaderText="Nombre" />
    </Columns>
</asp:GridView>
    
answered by 27.04.2016 / 21:52
source