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

Data Error Handling

4 Answers 156 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Daniel
Top achievements
Rank 1
Daniel asked on 13 Jul 2010, 07:06 PM
I  have two problems occcurring in RadGriedView that has GriedViewComboboxCollumn as columns. Here there are:

1 . Creating Data: I set AutoGenerateColumns Property as false and I add two columns by a method (AddColumns) at runtime, each one column is a GridViewComboBoxColumn, defining their ValueMember = ID and DisplayMember = Name, then set datasource of first column, the second column datasource is set based on the select value of the first column. The second column datasource shows the items correctly, but when I select one item of combobox the grid display the ValueMember not the DisplayMember. Why ?


2. After creating Data (even not displaying the correct value), I save the datasource of grid in a IList and clean the gridview. Then I load this IList again in gridview, when I try to edit the value of the second column, the VS shows the message in the picture errortelerik.jpg. Thus, I cannot edit the row or add new row in the gridview.
How Can I handle this error? I already add the radGridView1_DataError event, but not solve the problem, I still not be able to edit the grid.



The code is here. What am I doing wrong?
namespace Example
{
    public partial class Form1 : Form
    {
        IList machineList = new ArrayList();
        IList SlotList1 = new ArrayList();
        IList SlotList2 = new ArrayList();
 
        IList datasource1 = null;
 
        public Form1()
        {
            InitializeComponent();
             
            this.radGridView1.MasterGridViewTemplate.AllowCellContextMenu = false;
            this.radGridView1.MasterGridViewTemplate.AllowColumnChooser = false;
            this.radGridView1.MasterGridViewTemplate.AllowColumnReorder = false;
            this.radGridView1.MasterGridViewTemplate.AllowDragToGroup = false;
            this.radGridView1.MasterGridViewTemplate.AutoGenerateColumns = false;
            this.radGridView1.MasterGridViewTemplate.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
            this.radGridView1.MasterGridViewTemplate.EnableGrouping = false;
            this.radGridView1.MasterGridViewTemplate.EnableSorting = false;
            this.radGridView1.MasterGridViewTemplate.ShowFilteringRow = false;
            this.radGridView1.ShowGroupPanel = false;
            this.radGridView1.ShowNoDataText = false;
             
            VO.Machine m1 = new VO.Machine();
            m1.IdMachine = 1;
            m1.MachineName = "Machine1";
 
            VO.Machine m2 = new VO.Machine();
            m2.IdMachine = 2;
            m2.MachineName = "Machine2";
 
            machineList.Add(m1);
            machineList.Add(m2);
 
            VO.Slot s1 = new VO.Slot();
            s1.IdMachineSlot = 1;
            s1.SlotName = "Slot1";
 
            VO.Slot s2 = new VO.Slot();
            s2.IdMachineSlot = 3;
            s2.SlotName = "Slot3";
 
            SlotList1.Add(s1);
            SlotList1.Add(s2);
 
            VO.Slot s3 = new VO.Slot();
            s3.IdMachineSlot = 2;
            s3.SlotName = "Slot2";
 
            VO.Slot s4 = new VO.Slot();
            s4.IdMachineSlot = 4;
            s4.SlotName = "Slot4";
 
            SlotList2.Add(s3);
            SlotList2.Add(s4);
        }
 
        private void clearForm()
        {         
            radGridView1.DataSource = null;
            radGridView1.Columns.Clear();
        }
 
        protected void AddColumns()
        {
            GridViewComboBoxColumn maquinaColumn = new GridViewComboBoxColumn("colMaquina");
            maquinaColumn.HeaderText = "Machine";
            maquinaColumn.ValueMember = "IdMachine";           
            maquinaColumn.DisplayMember = "MachineName";
            maquinaColumn.FieldName = "IdMachine";
             
            GridViewComboBoxColumn posicaoColumn = new GridViewComboBoxColumn("colPosicao");
            posicaoColumn.HeaderText = "Slot";
            posicaoColumn.FieldName = "SlotName";
            posicaoColumn.ValueMember = "IdMachineSlot";
            posicaoColumn.DisplayMember = "SlotName";
 
            radGridView1.Columns.Add(maquinaColumn);
            radGridView1.Columns.Add(posicaoColumn);
        }
 
        protected void FillColumns()
        {
            AddColumns();
 
            ((GridViewComboBoxColumn)this.radGridView1.Columns[0]).DataSource = machineList;
        }
 
        protected void saveDataSource()
        {
            datasource1= new ArrayList();
 
            for (int i = 0; i < radGridView1.RowCount; i++)
            {
                VO.MachineSlot ms = new VO.MachineSlot();               
                ms.IdMachine = int.Parse(radGridView1.Rows[i].Cells[0].Value.ToString());
                ms.MachineName = "Machine" + ms.IdMachine.ToString();               
                ms.IdMachineSlot = int.Parse(radGridView1.Rows[i].Cells[1].Value.ToString());
                ms.SlotName = "Slot" + ms.IdMachineSlot.ToString();
                datasource1.Add(ms);
            }
            MessageBox.Show("DataSource1 Saved");
        }
 
        private void doAction(int action)
        {
            switch (action)
            {
                case 0: // Build a new datasource
                    clearForm();                                           
                    datasource1 = null;
                    FillColumns();
                    break;
                case 1:
                    saveDataSource(); // Save datasource of radgridview
                    clearForm();
                    break;
                case 2:
                    clearForm();
                    radGridView1.DataSource = datasource1;
                    FillColumns();
                    break;
            }
        }
 
        private void radButton1_Click(object sender, EventArgs e)
        {
            doAction(0);
        }
 
        private void radButton3_Click(object sender, EventArgs e)
        {
            doAction(1);
        }
 
        private void radButton2_Click(object sender, EventArgs e)
        {
            doAction(2);
        }
 
        private void onCellEditorInitialized(object sender, GridViewCellEventArgs e)
        {
            if (e.Column.HeaderText == "Slot")
            {
                if (this.radGridView1.CurrentRow.Cells["IdMachine"].Value != DBNull.Value
                    && this.radGridView1.CurrentRow.Cells["IdMachine"].Value != null)
                {
                    RadComboBoxEditor editor = (RadComboBoxEditor)this.radGridView1.ActiveEditor;
                    RadComboBoxEditorElement editorElement = (RadComboBoxEditorElement)editor.EditorElement;
 
                    if (this.radGridView1.CurrentRow.Cells["IdMachine"].Value.ToString().Equals("1"))
                    {
                        editorElement.DataSource = SlotList1;
                    }
                    else
                        editorElement.DataSource = SlotList2;
 
                    editorElement.SelectedValue = null;
                    editorElement.SelectedValue =this.radGridView1.CurrentCell.Value;
                }
            }
        }
    }
}
 
namespace VO
{
    public class Machine
    {
        private int _id_maquina;
        private string _cd_maquina;
 
        public Machine()
        {
        }
 
        public int IdMachine
        {
            get { return _id_maquina; }
            set { _id_maquina = value; }
        }
        public string MachineName
        {
            get { return _cd_maquina; }
            set { _cd_maquina = value; }
        }
    }
 
    public class Slot
    {
        private int _idMachineSlot;
        private string _cdSlot;
 
        public Slot()
        {
        }
 
        public int IdMachineSlot
        {
            get { return _idMachineSlot; }
            set { _idMachineSlot = value; }
        }
        public string SlotName
        {
            get { return _cdSlot; }
            set { _cdSlot = value; }
        }
    }
 
    public class MachineSlot
    {
        private int _id_maquina;
        private string _cd_maquina;
        private int _idMachineSlot;
        private string _cdSlot;
 
        public MachineSlot()
        {
        }
        public int IdMachine
        {
            get { return _id_maquina; }
            set { _id_maquina = value; }
        }
        public string MachineName
        {
            get { return _cd_maquina; }
            set { _cd_maquina = value; }
        }
        public int IdMachineSlot
        {
            get { return _idMachineSlot; }
            set { _idMachineSlot = value; }
        }
        public string SlotName
        {
            get { return _cdSlot; }
            set { _cdSlot = value; }
        }
    }
}

Thanks.

4 Answers, 1 is accepted

Sort by
0
Accepted
Alexander
Telerik team
answered on 16 Jul 2010, 03:43 PM
Hello Daniel,

Thank you for your questions.

The second GridViewComboBoxColumn (posicaoColumn) in your code snippet has no DataSource assigned. This is the reason why it cannot define its DisplayMember. The DataSource should contain all possible values that can be assigned to the column's cells. In your example you can add:
IList comboDataSource = new ArrayList();
 
public Form2()
{
    // ...
 
    // add all possible slots
    comboDataSource.Add(s1);
    comboDataSource.Add(s2);
    comboDataSource.Add(s3);
    comboDataSource.Add(s4);
}
 
protected void FillColumns()
{
    AddColumns();
 
    ((GridViewComboBoxColumn)this.radGridView1.Columns[0]).DataSource = machineList;
    ((GridViewComboBoxColumn)this.radGridView1.Columns[1]).DataSource = comboDataSource;
}

My second point is that the FieldName of the posicaoColumn should match a RadGridView column which provides values within the range of column's ValueMember values. In your example:
posicaoColumn.FieldName = "IdMachineSlot";

I hope it helps. For more information concerning GridViewComboBoxColumn you can review this help article.

Regards,
Alexander
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Daniel
Top achievements
Rank 1
answered on 16 Jul 2010, 09:51 PM
Thanks so much for the answer.

It helps me to resolve the first problem.

But  the second problem continues. When I try to add a new row in the grid after it is loaded by the IList saved, the VS launch a popup as errortelerik.JPG. How can I handle this error?

have a nice week.
0
Accepted
Alexander
Telerik team
answered on 20 Jul 2010, 06:43 AM
Hello Daniel,

Thank you for writing back.

It is preferred to use BindingList as the DataSource collection of RadGridView because its changes are automatically reflected on the data-bound control. I think it should solve the problem with adding new rows in your solution. Please try using the following collections:

BindingList<Machine> machineList = new BindingList<Machine>();
BindingList<Slot> SlotList1 = new BindingList<Slot>();
BindingList<Slot> SlotList2 = new BindingList<Slot>();
 
BindingList<MachineSlot> datasource1 = null;
BindingList<Slot> comboDataSource = new BindingList<Slot>();

I hope it will resolve the issues in your solution.

Best regards,
Alexander
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Daniel
Top achievements
Rank 1
answered on 20 Jul 2010, 02:29 PM
It solved my problems.

Thank you so much.



Tags
GridView
Asked by
Daniel
Top achievements
Rank 1
Answers by
Alexander
Telerik team
Daniel
Top achievements
Rank 1
Share this question
or