I`m new to Telerik, so please help!
I have a GridView with several columns of different types..Two of these column types are GridViewComboBoxColumn..
The content in the GridViewComboBoxColumn cell can be unique for each row. And the content in the GridViewComboBoxColumn cell depends on another cell.
If I change the value in one cell, I want the GridViewComboBoxColumn cell to update automaticly when the editing ends.
How can I achieve this?
Please help!
16 Answers, 1 is accepted
Thank you for writing.
Please refer to the following article, demonstrating how to achieve the desired functionality: http://www.telerik.com/support/kb/winforms/gridview/details/cascading-comboboxes-in-radgridview.
Should you have any other questions, do not hesitate to contact us.
Regards,
Stefan
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.
#Each RadGridCombobox has different datasources according to previous column
I have a radgrid.In which two combobox columns,namely 'clmproduct' and 'clmUnit'. Each product('clmProduct') has different set of Units('clmUnits').so here,each radgrid row's 'clmUnit' has different datasources according to 'clmProduct'.In DataGrid we have solution.Here,we cant get "GridViewComboBoxCell" as in DataGrid.But in RadGrid i didnt get solution for this problem.Please help me..
Thank you for writing.
The referred KB article from Stefan's reply demonstrates a sample approach how to achieve different DataSource collections considering the selection from the previous cell. There are two sample projects attached at the bottom of the article for your convenience. If you are still experiencing any further difficulties, feel free to submit a support ticket with the WinForms product and provide a sample project demonstrating the problem. Thus, we would be able to investigate the precise case and assist you further. Thank you in advance.
I hope this information helps. Should you have further questions I would be glad to help.
Regards,
Dess
Telerik by Progress
Hi Dess,
Thanks For Your Time
Actually I am new to Telerik.You said that you attached two sample projects.Is it below link?I hav'nt see anything other than the below link.
http://www.telerik.com/kendo-angular-ui/?utm_medium=ticketsignature&utm_source=supportticket&utm_campaign=dt-kendo-angular2-beta&utm_content=winforms
You mean,i have to use Kendo UI control???And I have to install that???
Please help me....
Thank you in advance
Regards
ABDUL HAFEEL
Thank you for writing back.
The C#/VB projects are available at the bottom of the referred KB article in Stefan's reply on 14-Apr-2014. I am posting the link again: http://www.telerik.com/support/kb/winforms/gridview/details/cascading-comboboxes-in-radgridview
I would like to note that this forum is related to the Telerik UI for WinForms suite. If you have any questions regarding Kendo UI or other Telerik products, feel free to post in the relevant forum: http://www.telerik.com/forums
I hope this information helps. If you have any additional questions, please let me know.
Regards,
Dess
Telerik by Progress
Hi Dess,
Thanks Again For Your Time
And Sorry for the late reply.
I saw that you attached projects.But in my case,I am binding from BackEnd(From Table).I have product and unit details in different table.Not manually creating datasources.And also it will have many datasources according to product.And it will change after changing table's value not in code.I can do it in DataGridView,but i can't in radgrid.
I hope you understand my question.
Thank you in advance
Regards
ABDUL HAFEEL
Thank you for writing back.
Although you have the different units in separate DataSources, the GridViewComboBoxColumn. property must contain all available options. Hence, it is necessary to create a merged units collection. This is the only way to store the cell values in the GridViewComboBoxColumn. An alternative approach is to use a GridViewTextBoxColumn and handle the EditorRequired event in order to replace the default editor with a dropdown and populate with the respective collection. Here is a sample code snippet:
public
RadForm1()
{
InitializeComponent();
DataTable dt =
new
DataTable();
dt.Columns.Add(
"Id"
,
typeof
(
int
));
dt.Columns.Add(
"Name"
,
typeof
(
string
));
for
(
int
i = 0; i < 5; i++)
{
dt.Rows.Add(i,
"Product"
+ i);
}
GridViewComboBoxColumn productsColumn =
new
GridViewComboBoxColumn(
"Products"
);
productsColumn.DataSource = dt;
productsColumn.ValueMember =
"Id"
;
productsColumn.DisplayMember =
"Name"
;
this
.radGridView1.Columns.Add(productsColumn);
GridViewTextBoxColumn unitsColumn =
new
GridViewTextBoxColumn(
"Units"
);
radGridView1.MasterTemplate.Columns.Add(unitsColumn);
this
.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
this
.radGridView1.EditorRequired += radGridView1_EditorRequired;
}
private
void
radGridView1_EditorRequired(
object
sender, EditorRequiredEventArgs e)
{
if
(
this
.radGridView1.CurrentColumn.Name ==
"Units"
)
{
RadDropDownListEditor ddlEditor =
new
RadDropDownListEditor();
RadDropDownListEditorElement element = ddlEditor.EditorElement
as
RadDropDownListEditorElement;
element.DataSource = GetProductUnits(
this
.radGridView1.CurrentRow.Cells[
"Products"
].Value);
element.DisplayMember =
"UnitTitle"
;
element.ValueMember =
"UnitTitle"
;
e.Editor = ddlEditor;
}
}
private
object
GetProductUnits(
object
cellValue)
{
int
productId = -1;
if
(cellValue !=
null
&&
int
.TryParse(cellValue.ToString(),
out
productId))
{
DataTable dt =
new
DataTable();
dt.Columns.Add(
"UnitId"
,
typeof
(
int
));
dt.Columns.Add(
"UnitTitle"
,
typeof
(
string
));
for
(
int
i = 0; i < 5; i++)
{
dt.Rows.Add(i,
"Unit "
+ productId +
"."
+ i);
}
return
dt;
}
return
null
;
}
I hope this information helps. If you have any additional questions, please let me know.
Regards,
Dess
Telerik by Progress
Hi Dess,
Thanks Again For Your Time
I am almost solved the problem with Stefan's reply on 14-Apr-2014.Now i can set different datasources,but i am facing an issue that when i select the 'Unit' GridViewComboBoxColumn,it displays display member.Then when i leave from the GridViewComboBoxColumn,it shows value member not display member that i set.Here is my sample code snippet:
Thank you in advance.
Regards
ABDUL HAFEEL
namespace
TelerikWinFormsApp1
{
public
partial
class
RadForm1 : Telerik.WinControls.UI.RadForm
{
public
RadForm1()
{
InitializeComponent();
}
DbTask mydbtas =
new
DbTask();
private
void
RadForm1_Load(
object
sender, EventArgs e)
{
radGridView1.Rows.AddNew();
BindProductDetails();
}
#region BindProductDetails
private
void
BindProductDetails()
{
string
Query =
"SELECT ProductID,ProductName FROM tblProduct"
;
object
[,] obj =
new
object
[1, 2]
{
{
"@quary_varc"
,Query},
};
DataSet ds = mydbtas.ExecuteQuery_SP(
"execute_simple_queries"
, obj);
BindingSource productBS =
new
BindingSource();
productBS.DataSource = ds.Tables[0];
((GridViewComboBoxColumn)radGridView1.Columns[
"clmProduct"
]).DataSource = ds.Tables[0];
((GridViewComboBoxColumn)radGridView1.Columns[
"clmProduct"
]).ValueMember =
"ProductID"
;
((GridViewComboBoxColumn)radGridView1.Columns[
"clmProduct"
]).DisplayMember =
"ProductName"
;
}
#endregion BindProductDetails
public
DataSet SelectUnitsByProduct(
int
ProductID)
{
object
[,] obj =
new
object
[1, 2]
{
{
"@product_Id"
,ProductID},
};
DataSet Units = mydbtas.ExecuteQuery_SP(
"select_units_by_product"
, obj);
return
Units;
}
private
void
radGridView1_CellEndEdit(
object
sender, GridViewCellEventArgs e)
{
int
rowIndex = Convert.ToInt32(e.RowIndex);
if
(radGridView1.Rows.Count - 1 == rowIndex)
{
if
(radGridView1.CurrentCell.Value !=
null
)
radGridView1.Rows.AddNew();
}
}
private
void
radGridView1_CellEditorInitialized(
object
sender, GridViewCellEventArgs e)
{
if
(
this
.radGridView1.CurrentColumn.Name ==
"clmUnit"
)
{
RadDropDownListEditor editor = (RadDropDownListEditor)
this
.radGridView1.ActiveEditor;
RadDropDownListEditorElement editorElement = (RadDropDownListEditorElement)editor.EditorElement;
DataSet ds = SelectUnitsByProduct(Convert.ToInt32(
this
.radGridView1.CurrentRow.Cells[
"clmProduct"
].Value));
BindingSource productBS =
new
BindingSource();
productBS.DataSource = ds.Tables[0];
editorElement.DataSource = productBS;
editorElement.ValueMember =
"UnitID"
;
editorElement.DisplayMember =
"UnitName"
;
editorElement.SelectedValue =
null
;
//editorElement.SelectedValue = this.radGridView1.CurrentCell.Value;
}
}
}
}
Thank you for writing back.
Following the provided information, I was unable to reproduce the issue you are facing. Note that it is important that the GridViewComboBoxColumn.DataSource collection must contain all available options while the collection for each row must be a subset of the whole collection. Hence, the cell value is necessary to be present in the GridViewComboBoxColumn.DataSource .
I have attached my sample project. Am I missing something? Could you please specify the exact steps how to reproduce the problem? Thus we would be able to make an adequate analysis of the precise case and assist you further. Thank you in advance.
I am looking forward to your reply.
Regards,
Dess
Telerik by Progress
Hi Dess,
Thanks Again Dess
I am attaching here my sample projects with Data and screenshots.I tried in both ways. That's why i am attaching two projects.When i select the Unit GridViewComboBoxColumn it shows display member.when i leave from the cell it shows display member.This is my problem(when i leave from the cell it shows display member).
Thanks in Advance
Ragards
ABDUL HAFEEL
Hi Dess,
Thanks Again Dess
Sorry for the previous reply.Please consider this as reply.
I can't attach zip file(my sample projects).How can i attach it?
In my sample projects,I tried both ways.Still same problem.
public
RadForm1()
{
InitializeComponent();
this
.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
}
DbTask mydbtas =
new
DbTask();
private
void
RadForm1_Load(
object
sender, EventArgs e)
{
radGridView1.Rows.AddNew();
BindProductDetails();
}
#region BindProductDetails
private
void
BindProductDetails()
{
string
Query =
"SELECT ProductID,ProductName FROM tblProduct"
;
object
[,] obj =
new
object
[1, 2]
{
{
"@quary_varc"
,Query},
};
DataSet ds = mydbtas.ExecuteQuery_SP(
"execute_simple_queries"
, obj);
BindingSource productBS =
new
BindingSource();
productBS.DataSource = ds.Tables[0];
((GridViewComboBoxColumn)radGridView1.Columns[
"clmProduct"
]).DataSource = ds.Tables[0];
((GridViewComboBoxColumn)radGridView1.Columns[
"clmProduct"
]).ValueMember =
"ProductID"
;
((GridViewComboBoxColumn)radGridView1.Columns[
"clmProduct"
]).DisplayMember =
"ProductName"
;
}
#endregion BindProductDetails
public
DataSet SelectUnitsByProduct(
int
ProductID)
{
object
[,] obj =
new
object
[1, 2]
{
{
"@product_Id"
,ProductID},
};
DataSet Units = mydbtas.ExecuteQuery_SP(
"select_units_by_product"
, obj);
return
Units;
}
private
void
radGridView1_CellEndEdit(
object
sender, GridViewCellEventArgs e)
{
int
rowIndex = Convert.ToInt32(e.RowIndex);
if
(radGridView1.Rows.Count - 1 == rowIndex)
{
if
(radGridView1.CurrentCell.Value !=
null
)
radGridView1.Rows.AddNew();
}
}
private
void
radGridView1_CellEditorInitialized(
object
sender, GridViewCellEventArgs e)
{
if
(
this
.radGridView1.CurrentColumn.Name ==
"clmUnit"
)
{
RadDropDownListEditor ddlEditor = e.ActiveEditor
as
RadDropDownListEditor;
RadDropDownListEditorElement element = ddlEditor.EditorElement
as
RadDropDownListEditorElement;
DataSet ds = SelectUnitsByProduct(Convert.ToInt32(
this
.radGridView1.CurrentRow.Cells[
"clmProduct"
].Value));
//BindingSource productBS = new BindingSource();
//productBS.DataSource = ds.Tables[0];
//object obj = ds.Tables[0];
element.DataSource = ds.Tables[0];
element.DisplayMember =
"UnitName"
;
element.ValueMember =
"UnitID"
;
}
}
When i select the Unit GridViewComboBoxColumn it shows display member.when i leave from the cell it shows value member.This is my problem(when i leave from the cell it shows value member) and i want show display member instead of value member.
Thanks in Advance
Ragards
ABDUL HAFEEL
Thank you for writing back.
If you don't see the DisplayMember value in GridViewComboBoxColumn, the most probable reason is that the assigned cell's value doesn't exist in the applied GridViewComboBoxColumn.DataSource collection. Otherwise, the cell's value is expected to be matched with the respective DisplayMember as it is demonstrated in my sample project.
The allowed extensions in the forum's attachments are: .gif, .jpg, .jpeg, .png. Feel free to submit a support ticket from your Telerik account where you can provide a sample project. This would be the fastest way to investigate the precise case and assist you further. Thank you in advance for your cooperation.
If you have any additional questions, please let me know.
Regards,
Dess
Telerik by Progress
The solution provided assumes that the values from the child dropdown column all come from the same datasource. I have the case where, depending on what value is chosen from the parent dropdown column, a specific datasource is required for the child dropdown column. If option 1 is chosen in the parent dropdown column, datasource 1 is required for the child dropdown column. If option 2, then datasource 2. Datasource 1 and 2 both have a record with ID = 1. Since I need to populate a datasource at load time, I need to chose one or the other or a union of both, which means that I may not have the values required to display the selected value in the child dropdown when populating the grid. Worse yet, if a record in Datasource 1 has the same ID as Datasource 2, then it could show the Datasource 1 description for ID = 1, when ID = 1 was from Datasource 2.
With that being said, how can I show the value in this column from the correct datasource (even if I want to override the column value and show just the correct text) at load time. I am able to get the correct child dropdown datasource in the EditorRequired event, but can't get it to use the correct list when I am not editing the row.
I hope this makes sense.
Suggestions?
The described scenario seems like a standard cascading combo boxes scenario with one very important detail: your IDs can be duplicated. Note that it is very important for the GridViewComboBoxColumn to have all available options in the DataSource collection of the column in order to behave properly and keep the values when the editor's value is committed. However, if you have IDs that are duplicated this will break the DataSource collection of the column. It will match the first value with the specified ID. This is not quite a supported scenario for the GridViewComboBoxColumn.
An alternative solution that I can suggest is to use a simple GridViewTextBoxColumn. You can still handle the EditorRequired event and replace the default editor with the RadDropDownListEditor. In the CellEditorInitialized event you can set the desired DataSource for the drop down. However, in this case, the IDs will be displayed in the cell values. But you can handle the CellFormatting event and change the Text of the cell element.
I hope this information helps. If you have any additional questions, please let me know.
Regards,
Dess
Progress Telerik
Hie Dess,
I followed you steps (alternative solution) for creating different datasource for each cell depending on value from another column, but when i click on someother cell after edit, the formatted text is changing into numeric value.
Atatched screenshot
If you try to implement the alternative solution with the GridViewTextBoxColumn and the RadDropDownListEditor, make sure that you don't set the RadDropDownListEditorElement.ValueMember property to the field that contains the numeric value but the text. Thus, after committing the editor's value, the desired text will be stored into the cell.
I hope this information helps. If you need any further assistance please don't hesitate to contact me.
Regards,
Dess
Progress Telerik