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

Bind different datasource to single comboboxcolumn

5 Answers 162 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Subh
Top achievements
Rank 1
Subh asked on 24 Sep 2009, 12:26 PM
Hi,
    I have a comboBoxcolumn displaying the value based on other column. Say for example the possible values in other column are "Color" and "Fruits". When a cell in the comboBoxcolumn is edited then the values displayed in the list should be based on the value in the other column. If the value in the other column is "Color" then the cell being edited in the comboBoxcolumn should display the list of colors alone. Similarly when the value in the other column is "Fruits" then the cell being edited in the comboBoxcolumn should display the list of fruits alone.

How can the datasource be assigned at the ComboBoxcell level?

Thanks in advance.

5 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 26 Sep 2009, 06:06 AM
Hello Subh,

You can do this when handling CellBeginEdit event. Please consider the following sample:

DataTable colors = new DataTable(); 
colors.Columns.Add("ID"typeof(int)); 
colors.Columns.Add("Name"typeof(string)); 
colors.Rows.Add(0, "red"); 
colors.Rows.Add(1, "green"); 
colors.Rows.Add(2, "blue"); 
 
DataTable fruits = new DataTable(); 
fruits.Columns.Add("ID"typeof(int)); 
fruits.Columns.Add("Name"typeof(string)); 
fruits.Rows.Add(0, "apple"); 
fruits.Rows.Add(1, "grape"); 
fruits.Rows.Add(2, "banana"); 
 
GridViewLookUpColumn combo = new GridViewLookUpColumn("Items"""); 
combo.DataType = typeof(int); 
combo.DisplayMember = "Name"
combo.ValueMember = "ID"
combo.Width = 150; 
this.radGridView1.Columns.Add(combo); 
 
void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e) 
    if (((GridViewDataColumn)this.radGridView1.CurrentColumn).UniqueName == "Items"
    { 
        int rowID = (int)this.radGridView1.CurrentRow.Cells[0].Value; 
        if (rowID > 10) 
        { 
            ((GridViewLookUpColumn)this.radGridView1.CurrentColumn).DataSource = colors; 
        } 
        else 
        { 
            ((GridViewLookUpColumn)this.radGridView1.CurrentColumn).DataSource = fruits; 
        } 
    } 

You should also handle CellFormatting event to set the correct text in the cell. If you need further assistance, please write back.

Best wishes,
Jack
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Subh
Top achievements
Rank 1
answered on 01 Oct 2009, 06:36 AM
Hi,
  Thanks for your reply. When the grid is loaded with existing data, those existing values should be mapped with the lookup values. How can that be done?

Thanks
Subh
0
Jack
Telerik team
answered on 01 Oct 2009, 04:23 PM
Hello Subh,

You should do this manually when handling CellFormatting event:

void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e) 
    GridViewDataColumn column = e.CellElement.ColumnInfo as GridViewDataColumn; 
    if (column != null && column.UniqueName == "Items"
    { 
        GridDataCellElement dataCell = (GridDataCellElement)e.CellElement; 
        if (dataCell.Value != null && dataCell.Value != DBNull.Value) 
        { 
            int value = (int)dataCell.Value; 
            int rowID = (int)e.CellElement.RowInfo.Cells[0].Value; 
            if (rowID > 10) 
            { 
                e.CellElement.Text = colors.Rows[value]["Name"].ToString(); 
            } 
            else 
            { 
                e.CellElement.Text = fruits.Rows[value]["Name"].ToString(); 
            } 
        } 
        else  
        { 
            e.CellElement.Text = ""
        } 
    } 


Kind regards,
Jack
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Rafferty
Top achievements
Rank 1
answered on 27 Nov 2012, 06:50 AM
The answers can't solve the problem,and when I bound one cell and the other cell changed too  in current column.please tell me how to get it.
I used GridViewComboBoxColumn .
0
Jack
Telerik team
answered on 29 Nov 2012, 06:04 PM
Hi Rafferty,

I am not sure that I fully understand your issue. Please, could you elaborate a bit more and describe the issue with more detail? Please consider the following KB article which describes how to make combobox data source dependent on another cell value.

All the best,
Jack
the Telerik team
Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
Tags
GridView
Asked by
Subh
Top achievements
Rank 1
Answers by
Jack
Telerik team
Subh
Top achievements
Rank 1
Rafferty
Top achievements
Rank 1
Share this question
or