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

Gridview ComboBox Column

3 Answers 448 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Edson
Top achievements
Rank 1
Edson asked on 29 Oct 2015, 10:26 PM

Hi,

I have a gridview with one combobox column that its populated depending of the value entered in column 1. The combobox column is populated as expected, but, if I save the values and open the record and click inside the combobox column the selected value in that row its not the value that comes from the db, it always select the first value of the combobox. And I need to get the saved selected value when I enter into edit mode.

 

My code

 

private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
        {
            RadDropDownListEditor listEditor = this.radGridView1.ActiveEditor as RadDropDownListEditor;

            if (listEditor == null)
            {
                return;
            }

            RadDropDownListEditorElement editorElement = listEditor.EditorElement as RadDropDownListEditorElement;
            editorElement.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            editorElement.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList;


            string item = radGridView1.CurrentRow.Cells[0].Value == null ? "" : radGridView1.CurrentRow.Cells[0].Value.ToString();

            var result = SqlClass.GetUOMByItem(item);
            editorElement.DataSource = null;
            editorElement.Items.Clear();
            editorElement.DataSource = result;
            editorElement.DisplayMember = "UOFM";
            editorElement.ValueMember = "UOFM";


            if (editorElement.Items.Count > 0)
            {
                editorElement.SelectedValue = null;
                editorElement.SelectedValue = this.radGridView1.CurrentCell.Value;

            }

        }

 

thanks for your help!!

3 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 02 Nov 2015, 07:16 AM
Hello Edson,

Thank you for writing.

Since you are rebinding the column every time you open the editor, it is expected that the selection in the popup will be lost. Your solution with saving the selected value and setting it after rebinding the grid is valid. I extended the code snippet you sent me to create an example: 
public partial class Form1 : Form
{
    private BindingSource bs;
 
    public Form1()
    {
        InitializeComponent();
 
        this.radGridView1.DataSource = this.GetData();
 
        GridViewComboBoxColumn combolCol = new GridViewComboBoxColumn();
        combolCol.Name = "Phone";
        combolCol.Width = 200;
        this.radGridView1.Columns.Add(combolCol);
 
        this.radGridView1.CellEditorInitialized += radGridView1_CellEditorInitialized;
    }
 
    private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
    {
        RadDropDownListEditor listEditor = this.radGridView1.ActiveEditor as RadDropDownListEditor;
        if (listEditor == null)
        {
            return;
        }
 
        RadDropDownListEditorElement editorElement = listEditor.EditorElement as RadDropDownListEditorElement;
        editorElement.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
        editorElement.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList;
 
        bool isEven = (int)this.radGridView1.CurrentRow.Cells[1].Value % 2 == 0;
        IList<string> res = this.GetComboData(isEven);
        editorElement.DataSource = res;
 
        if (editorElement.Items.Count > 0)
        {
            editorElement.SelectedValue = null;
            editorElement.SelectedValue = this.radGridView1.CurrentCell.Value;
 
        }
    }
 
    private DataTable GetData()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Age", typeof(int));
        dt.Columns.Add("Date", typeof(DateTime));
        dt.Columns.Add("Bool", typeof(bool));
        for (int i = 0; i < 100; i++)
        {
            dt.Rows.Add("Name " + i, i, DateTime.Now.AddMinutes(i), i % 2 == 0 ? true : false);
        }
 
        return dt;
    }
 
    private IList<string> GetComboData(bool flag)
    {
        IList<string> data = new List<string>();
        if (flag)
        {
            data.Add("Mobile1");
            data.Add("Mobile2");
            data.Add("Mobile3");
            data.Add("Mobile4");   
        }
        else
        {
            data.Add("Fax1");
            data.Add("Fax2");
            data.Add("Fax3");
            data.Add("Fax4");
        }
         
        return data;
    }
}

I am also sending you a gif file showing the result on my end.

I hope this helps. Should you have further questions please do not hesitate to write back.

Regards,
Hristo Merdjanov
Telerik
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 Feedback Portal and vote to affect the priority of the items
0
keerthi
Top achievements
Rank 1
answered on 27 Sep 2018, 11:52 AM

Hi histro, 

i am using same sample code as given by you,when  I am selecting one value  from the combo column in gridview , getting issue as object of type system.string cannot be  convert totype system.collections.generic.list(system.string).please help to resolve the issue.  

Thanks for your help!!

 

 

0
Hristo
Telerik team
answered on 27 Sep 2018, 01:53 PM
Hi Keerthi,

I am not sure how to reproduce the experienced on your end behavior. Can you please open up a support ticket and send us your project so that we can investigate it on our end?

Regards,
Hristo
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
GridView
Asked by
Edson
Top achievements
Rank 1
Answers by
Hristo
Telerik team
keerthi
Top achievements
Rank 1
Share this question
or