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

RadGirdView DataBinding.

1 Answer 112 Views
GridView
This is a migrated thread and some comments may be shown as answers.
L. M. Pathan
Top achievements
Rank 1
L. M. Pathan asked on 24 Mar 2011, 02:14 PM
Hello,

I am working with MDI Windows Application and I have two different RadForms both are having RadGrid each. I have a button on first RadForm that opens second RadForm. Now user should be able to selects multiple rows from the grid on second RadForm using either mouse click or up-down arrow key to navigate through rows and press space key to select particular row. Again selected row should be highlighted. There is a Ok button on the second RadForm on click of which second RadForm should close and Rad Grid View on first RadForm should get filled with all selected rows from the grid on second RadForm.

I have set Modifier to Public for the grid on first RadForm and tried this functionality on cell click event for the grid on second RadForm all with no luck. Any help is appreciated. I would be great full if someone help. This is my first windows application.

Regards
Pathan L. 

1 Answer, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 25 Mar 2011, 09:49 AM
Hello Pathan,

You cannot do this with the default grid selection, but you could add another boolean flag to your data bound object and use that one to select a specific row or not.

Please take a look at the following example:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
public partial class Form1 : Form
{
    private RadGridView radGridView1;
    private List<Person> persons;
    public Form1()
    {
        InitializeComponent();
        this.Controls.Add(radGridView1 = new RadGridView());
        radGridView1.Dock = DockStyle.Fill;
        radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        radGridView1.GridBehavior = new CustomGridBehavior();
        radGridView1.ReadOnly = true;
        radGridView1.RowFormatting += new RowFormattingEventHandler(radGridView1_RowFormatting);
        radGridView1.DataBindingComplete += new GridViewBindingCompleteEventHandler(radGridView1_DataBindingComplete);
        persons = new List<Person>();
 
        for (int i = 0; i < 20; i++)
        {
            persons.Add(new Person
            {
                Id = i,
                Name = "Person "+i,
            });
        }
 
        radGridView1.DataSource = persons;
 
        var button = new RadButton();
        this.Controls.Add(button);
        button.Dock = DockStyle.Bottom;
        button.Text = "Show selected";
        button.Click += new EventHandler(button_Click);
    }
 
    void button_Click(object sender, EventArgs e)
    {
        var selectedPersons = persons.Where(p => p.Selected).ToList();
        // set selected persons here?
 
        var stringBuilder = new StringBuilder();
        selectedPersons.ForEach(p => stringBuilder.AppendLine(p.Name));
        MessageBox.Show(stringBuilder.ToString());
    }
 
    void radGridView1_DataBindingComplete(object sender, GridViewBindingCompleteEventArgs e)
    {
        foreach (var column in radGridView1.Columns)
        {
            if (column.FieldName == "Selected")
            {
                column.IsVisible = false;
                column.VisibleInColumnChooser = false;
            }
        }
    }
 
    void radGridView1_RowFormatting(object sender, RowFormattingEventArgs e)
    {
        if (e.RowElement.RowInfo.DataBoundItem != null)
        {
            var data = e.RowElement.RowInfo.DataBoundItem as Person;
            if (data != null && data.Selected)
            {
                e.RowElement.DrawFill = true;
                e.RowElement.NumberOfColors = 1;
                e.RowElement.SetValue(LightVisualElement.BackColorProperty, Color.LightSkyBlue);
            }
            else
            {
                e.RowElement.ResetValue(LightVisualElement.NumberOfColorsProperty, Telerik.WinControls.ValueResetFlags.Local);
                e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, Telerik.WinControls.ValueResetFlags.Local);
                e.RowElement.ResetValue(LightVisualElement.BackColorProperty, Telerik.WinControls.ValueResetFlags.Local);
            }
        }
    }
 
    private class CustomGridBehavior : BaseGridBehavior
    {
        public override bool ProcessKeyPress(KeyPressEventArgs keys)
        {
            if (keys.KeyChar == (char)Keys.Space)
            {
                if (this.GridControl.CurrentRow != null && this.GridControl.CurrentRow.DataBoundItem != null)
                {
                    var data = this.GridControl.CurrentRow.DataBoundItem as Person;
                    if (data != null)
                    {
                        data.Selected = !data.Selected;
                        this.GridControl.CurrentRow.InvalidateRow();
                    }
                }
            }
 
            return base.ProcessKeyPress(keys);
        }
    }
}
 
public class Person
{
    public int Id
    {
        get;
        set;
    }
 
    public string Name
    {
        get;
        set;
    }
 
    public bool Selected
    {
        get;
        set;
    }
}

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
Telerik WinForms MVP
Tags
GridView
Asked by
L. M. Pathan
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Share this question
or