This is a migrated thread and some comments may be shown as answers.

Populate GridView through another GridView

11 Answers 89 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Flávio
Top achievements
Rank 1
Flávio asked on 12 Sep 2011, 09:31 PM
Hi,

   I'm creating a window with a GridView 01 where I need open other window with a GridView 02 for select some values and set in GridView 01 the selected values. I'd like know how to do set the selected values of GridView 02 to GridView 01?
Image with a sample attached.

Thankful for attention!
  

11 Answers, 1 is accepted

Sort by
0
tuonglam
Top achievements
Rank 1
answered on 13 Sep 2011, 12:22 PM
this's good question :x i expect the answer.thank you
0
Emanuel Varga
Top achievements
Rank 1
answered on 14 Sep 2011, 06:57 AM
Hello,

Do they have the same type as the datasource? Which type of data source are you using? IList, DataTable ... ?

Please answer these questions first and then i can help you, but in my point of view you could use the code posted in this code library article to achieve this. (+ you can give drag and drop functionality to the users :P)

Best Regards,
Emanuel Varga
0
Flávio
Top achievements
Rank 1
answered on 14 Sep 2011, 01:41 PM
Hi Emanuel,

   I saw your sample and I implemented in other window, it's very nice! In this situation I can't use DragAndDrop.
Yeah, they have the same datasource and using IList!

In the GridView02 I am using the following code:

private void AddSelectedStage(object sender, EventArgs e)
     {
         IList<Dominio.Entidades.Estagio> listSelectedStages = new List<Dominio.Entidades.Estagio>();
         Dominio.Entidades.Estagio obj;
         var checkedRows = this.GrdEstagios.Rows.Where(r => r.Cells["estSeleciona"].Value != null && r.Cells["estSeleciona"].Value is bool && (bool)r.Cells["estSeleciona"].Value);           
         foreach (var i in checkedRows)
         {
             obj = new Dominio.Entidades.Estagio();
             obj.estId = int.Parse(i.Cells["estId"].Value.ToString());
             obj.estDescricao = i.Cells["estDescricao"].Value.ToString();
             listSelectedStages.Add(obj);        
         }
         FrmTemplate frmTemplate = new FrmTemplate();
         frmTemplate.SetSelectedStages(listSelectedStages);
         this.Close();
     }

The method SetSelectedStages(listSelectedStages) received the selected values of the GridView 02 and pass the values to GridView 01, but GridView 01 couldn't load the values.

In the GridView 01 I using the following code:

private void SetSelectedStages(IList<Dominio.Entidades.Estagio> _listSelectedStages)
    {
        this.radGridView1.BeginUpdate();
        foreach (var i in _listSelectedStages)
        {
            this.radGridView1.Rows.Add(i.estId, i.estDescricao);
        }
        this.radGridView1.EndUpdate();
    }

Do you have any tip?

Thankful for your attention!
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 14 Sep 2011, 02:14 PM
Hello Flavio,

It will go something like this, i will write a rough estimation, hope it will be ok

Create a property SelectedStages in the 2nd form, and then change your code like so:
private void AddSelectedStage(object sender, EventArgs e)
{
    SelectedStages = this.GrdEstagios.Rows.Where(r => r.Cells["estSeleciona"].Value != null && r.Cells["estSeleciona"].Value is bool && (bool)r.Cells["estSeleciona"].Value)
        .Select(r => r.DataBoundItem)
        .Cast<Dominio.Entidades.Estagio>()
        .ToList();
    this.Close();
}

and in your main form, just take the datasource of your grid and add these to it, not with rows...
private void SetSelectedStages(IList<Dominio.Entidades.Estagio> _listSelectedStages)
{
    this.radGridView1.BeginUpdate();
    var dataSource = GrdEstagios.DataSource as IList<Dominio.Entidades.Estagio>;
    if (dataSource != null)
        _listSelectedStages.ToList().ForEach(dataSource.Add);
    this.radGridView1.EndUpdate();
}

And it should be ok.

Best Regards,
Emanuel Varga
0
Flávio
Top achievements
Rank 1
answered on 14 Sep 2011, 07:23 PM
Hi Emanuel.

   I tried implement your code, but isn't work. When I call to the method SetSelectedStages, var datasource is receiving null.
Do you have any tip?

Thankful for your attention!
0
Emanuel Varga
Top achievements
Rank 1
answered on 14 Sep 2011, 07:25 PM
Hello again,

What is the datasource on the main grid?
0
Flávio
Top achievements
Rank 1
answered on 14 Sep 2011, 07:47 PM
Hi,

  Follow the code of my main GridView and secondary GridView:

Main GridView:

private void PrepareGrid(RadGridView grid)
       {
           GridViewTextBoxColumn estIdColumn = new GridViewTextBoxColumn("estId""estId");
           estIdColumn.Width = 70;
           estIdColumn.HeaderText = "Estágio Id";
           estIdColumn.IsVisible = false;
           GridViewTextBoxColumn estDescricaoColumn = new GridViewTextBoxColumn("estDescricao""estDescricao");
           estDescricaoColumn.Width = 200;
           estDescricaoColumn.HeaderText = "Descrição";
           estDescricaoColumn.ReadOnly = true;
           GridViewCommandColumn commandColumn = new GridViewCommandColumn("estAdicionaRecurso""estAdicionaRecurso");
           commandColumn.Width = 15;
           commandColumn.HeaderText = "[+] Recurso";
 
           grid.Columns.Add(estIdColumn);
           grid.Columns.Add(estDescricaoColumn);
           grid.Columns.Add(commandColumn);
           //grid.TableElement.RowHeight = 50;
           grid.MasterTemplate.AllowRowReorder = true;
           grid.ReadOnly = false;
           grid.MultiSelect = true;
           grid.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
 
           grid.GridBehavior = new CustomGridBehavior();
       }

Secondary GridView:

private void PrepareGrid(RadGridView grid)
       {
           GridViewTextBoxColumn estIdColumn = new GridViewTextBoxColumn("estId", "estId");
           estIdColumn.Width = 70;
           estIdColumn.HeaderText = "Estágio Id";
           estIdColumn.IsVisible = false;
           GridViewTextBoxColumn estDescricaoColumn = new GridViewTextBoxColumn("estDescricao", "estDescricao");
           estDescricaoColumn.Width = 200;
           estDescricaoColumn.HeaderText = "Descrição";
           estDescricaoColumn.ReadOnly = true;
           GridViewCheckBoxColumn checkColumn = new GridViewCheckBoxColumn("estSeleciona", "estSeleciona");
           checkColumn.Width = 15;
           checkColumn.HeaderText = "";
 
           grid.Columns.Add(estIdColumn);
           grid.Columns.Add(estDescricaoColumn);
           grid.Columns.Add(checkColumn); 
           //grid.TableElement.RowHeight = 50;
           grid.MasterTemplate.AllowRowReorder = true;
           grid.ReadOnly = false;
           grid.MultiSelect = true;
           grid.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
 
           grid.GridBehavior = new CustomGridBehavior();
       }
 
       private void GrdEstagio_Load(object sender, EventArgs e)
       {
           this.PrepareGrid(this.GrdEstagios);
 
           IList<Dominio.Entidades.Estagio> listaEstagio = this._estagioServico.RetornaTodos();            
           foreach (var i in listaEstagio)
           {
               this.GrdEstagios.Rows.Add(i.estId, i.estDescricao);
           }
       }


Thankful for your attention!
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 14 Sep 2011, 07:50 PM
Hello,

Instead of doing this:
foreach (var i in listaEstagio)
{
    this.GrdEstagios.Rows.Add(i.estId, i.estDescricao);
}
do this:
GrdEstagios.DataSource = listaEstagio;

and let me know if you still have problems

Best Regards,
Emanuel Varga
0
Flávio
Top achievements
Rank 1
answered on 15 Sep 2011, 07:17 PM
Hi,

  I could get values of GridView 02 of secondary window to main window, but GridView 01 isn't showing the values yet.
Follow my code:

Main Window:

This code is responsible for showing the secondary window where it contains GridView 02.

private void AbrirTelaEstagios(object sender, EventArgs e)
        {
            GrdEstagio obj = new GrdEstagio();
            obj.ShowDialog();
        }

GridView 02:

This code get selected values and call the method PopularGrid, where this is responsible for passing all selected values to GridView 01 in Main window.

private void AdicionarEstagiosSelecionados(object sender, EventArgs e)
    {
        var SelectedStages = this.GrdEstagios.Rows.Where(r => r.Cells["estSeleciona"].Value != null && r.Cells["estSeleciona"].Value is bool && (bool)r.Cells["estSeleciona"].Value)
                              .Select(r => r.DataBoundItem)
                              .Cast<Dominio.Entidades.Estagio>()
                              .ToList();
        FrmTemplate frmTemplate = new FrmTemplate();           
        frmTemplate.PopularGrid(SelectedStages, this.GrdEstagios); //Set selected values to GridView 01
        this.Hide();
    }



GridView 01:

This code get values selected in GridView 02 and set the values selected in GridView 01.

public void PopularGrid(List<Dominio.Entidades.Estagio> _listaEstagios, RadGridView _grdEstagios)
      {
          this.radGridView1.MasterTemplate.BeginUpdate();
          var dataSource = _grdEstagios.DataSource as IList<Dominio.Entidades.Estagio>;
          if (dataSource != null)
          {
              _listaEstagios.ForEach(dataSource.Add);
              this.radGridView1.DataSource = _listaEstagios;               
          }
          this.radGridView1.MasterTemplate.EndUpdate();
      }


Thankful for your attention!
0
Flávio
Top achievements
Rank 1
answered on 19 Sep 2011, 02:02 PM
Hi,

   Any tips?

0
Flávio
Top achievements
Rank 1
answered on 19 Sep 2011, 06:42 PM
Hi Emanuel,

  I solved it! I created a static class for save the selected values in GridView 02 and set values in GridView 01 when secondary window is closed.
Follow my code:

Main Window:

This code is responsible for showing the secondary window where it contains GridView 02.

private void AbrirTelaEstagios(object sender, EventArgs e)
    {
        GrdEstagio obj = new GrdEstagio();
        obj.ShowDialog();
        ListarEstagiosSelecionados();
    }
 
    private void ListarEstagiosSelecionados()
    {
        this.GrdEstagiosSelecionados.MasterTemplate.BeginUpdate();
        this.GrdEstagiosSelecionados.DataSource = ListaGlobal.ListaEstagios;
        this.GrdEstagiosSelecionados.MasterTemplate.EndUpdate();
    }

GridView 02:

This code get selected values and call the method PopularGrid, where this is responsible for passing all selected values to GridView 01 in Main window.

private void AdicionarEstagiosSelecionados(object sender, EventArgs e)
    {
        var SelectedStages = this.GrdEstagios.Rows.Where(r => r.Cells["estSeleciona"].Value != null && r.Cells["estSeleciona"].Value is bool && (bool)r.Cells["estSeleciona"].Value)
                              .Select(r => r.DataBoundItem)
                              .Cast<Dominio.Entidades.Estagio>()
                              .ToList();
        FrmTemplate frmTemplate = new FrmTemplate();           
        frmTemplate.PopularGrid(SelectedStages, this.GrdEstagios);
        this.Hide();
    }


GridView 01:

This code get values selected in GridView 02 and set the values selected in GridView 01.

public void PopularGrid(List<Dominio.Entidades.Estagio> _listaEstagios, RadGridView _grdEstagios)
{
    var dataSource = _grdEstagios.DataSource as IList<Dominio.Entidades.Estagio>;
    if (dataSource != null)
    {
        _listaEstagios.ForEach(dataSource.Add);
        //Set all selected values of GridView 02 in static class ListaGlobal.
        ListaGlobal.ListaEstagios = _listaEstagios;
    }
}

Class ListaGlobal:

This class get the selected values of GridView 02 for set values in GridView 01.

public class ListaGlobal
 {
     private static List<Estagio> listaEstagios;
 
     public static List<Estagio> ListaEstagios
     {
         get { return listaEstagios; }
         set { listaEstagios = value; }
     }
 }


Thankful for your attention!


Tags
GridView
Asked by
Flávio
Top achievements
Rank 1
Answers by
tuonglam
Top achievements
Rank 1
Emanuel Varga
Top achievements
Rank 1
Flávio
Top achievements
Rank 1
Share this question
or