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

editable grid GridDropDownColumn distinct value

5 Answers 156 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ruben Vera
Top achievements
Rank 1
Ruben Vera asked on 12 Jan 2011, 05:59 PM
Hi I'm tring to do a disctinct combobox in edit mode in a radgrid but whit no result.
My code is based on the example EditOnDblClick.
Any help would be apreciated.
Thanks in advance

<

 

 

telerik:RadGrid ID="RadGrid1" DataSourceID="SqlDataSource1" Width="97%" ShowStatusBar="True" AllowSorting="True" PageSize="14" GridLines="None" AllowPaging="True" runat="server" AllowAutomaticUpdates="True" AutoGenerateColumns="False" OnItemDataBound="RadGrid1_ItemDataBound">

 

 

<MasterTableView TableLayout="Fixed" DataKeyNames="nusuario" EditMode="InPlace">
<Columns>
<telerik:GridBoundColumn UniqueName="nusuario" DataField="nusuario" HeaderText="Nusuario" ReadOnly="True" />  

 

 

<telerik:GridBoundColumn UniqueName="nombre" DataField="nombre" HeaderText="Nombre" />

 

<telerik:GridDropDownColumn UniqueName="tipo" HeaderText="Tipo" DataField="tipo" ListTextField="tipo" ListValueField="tipo" DataSourceID="SqlDataSource1" DropDownControlType="RadComboBox" />

 

</Columns>

 

</MasterTableView>

 

<ClientSettings>

 

<ClientEvents OnRowClick="RowClick" OnRowDblClick="RowDblClick" OnGridCreated="GridCreated" OnCommand="GridCommand" />

 

</ClientSettings>

 

</telerik:RadGrid 

<br />

 

<asp:Label ID="Label1" runat="server" EnableViewState="false" />

 

<br />

 

<asp:SqlDataSource ID="SqlDataSource1" runat="server" />

 

 

 

Code

 

 

protected void Page_Load(object sender, EventArgs e)

{

 

 

 

 

SqlDataSource1.ConnectionString = Dynamic.Common.

 

DConfig.ConnectionString;

 

 

 SqlDataSource1.SelectCommand =

 

"SELECT * from usuarios order by nusuario";

 

 

/*

if (DConfig.SelCodNac) AddColumn("codnac");

if (DConfig.SelCodArea) AddColumn("codarea");

if (DConfig.SelCodDel) AddColumn("coddel");

if (DConfig.SelCodMonitor) AddColumn("codmonitor");

*/

 

//Update

 

string sql = string.Empty;

 

foreach (GridColumn column in RadGrid1.MasterTableView.Columns)

{

 

 

if (sql != string.Empty) sql += ",";

sql += column.UniqueName +

 

"=@" + column.UniqueName;

 

 

SqlDataSource1.UpdateParameters.Add(column.UniqueName,

 

string.Empty);

 

 

}

SqlDataSource1.UpdateCommand =

 

"UPDATE usuarios SET " +sql +" WHERE nusuario=@nusuario";

 

 

}

 

 

 

 

protected void RadGrid1_ItemUpdated(object source, Telerik.Web.UI.GridUpdatedEventArgs e)

{

 

 

if (e.Exception != null){

e.KeepInEditMode =

 

true;

 

 

e.ExceptionHandled =

 

true;

 

 

 

SetMessage(Server.HtmlEncode(

 

"Error al actualizar: " + e.Exception.Message).Replace("'", "&#39;").Replace("\r\n", "<br />"));  

 

 

}

 

 

 

else{

 

GridDataItem dataItem = (GridDataItem)e.Item;

SetMessage(

 

" Nusuario " + dataItem.GetDataKeyValue("nusuario") + " updated");

 

 

}

}

 

 

 

private void DisplayMessage(string text)

{

Label1.Text =

 

string.Format("<span>{0}</span>", text);

 

 

}

 

 

 

private void SetMessage(string message)

{

gridMessage = message;

}

 

 

 

 

protected void RadGrid1_DataBound(object sender, EventArgs e)

 

{

 

 

 

if (!string.IsNullOrEmpty(gridMessage)) DisplayMessage(gridMessage);

 

}

 

 

 

protected void AddColumn(string campo)

 

{

 

 

 

//GridBoundColumn

 

 

 

 

GridDropDownColumn boundColumn = new GridDropDownColumn();

 

boundColumn.UniqueName = campo;

boundColumn.HeaderText = campo.Substring(0, 1).ToUpper() + campo.Substring(1);

boundColumn.DataField = campo;

boundColumn.ListTextField=campo;

boundColumn.ListValueField=campo;

boundColumn.DataSourceID =

 

 

"SqlDataSource1";

 

RadGrid1.MasterTableView.Columns.Add(boundColumn);

 

}

 

 

 

 

 

 

 

protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)

 

{

 

 

 

if (e.Item is GridEditableItem && (e.Item as GridEditableItem).IsInEditMode)

 

{

 

 

 

/*

 

GridEditFormItem editform = (GridEditFormItem)e.Item;

DropDownList ddl = (DropDownList)editform.FindControl("ddl");

ddl.Items.Add("foo");

ddl.Items.Add("bar");

*/

 

 

}

}

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 13 Jan 2011, 12:12 PM
Hello Ruben Vera,

One option is creating SqlDataSource with select distinct statement to avoid duplication. Another approach is customizing the GridDropDownColumn on editing. For more information on this please go through the 'Customizing the options for GridDropDownColumn on editing' part of the following documentation.
Customize/Configure GridDropDownColumn

Thanks,
Princy.
0
Ruben Vera
Top achievements
Rank 1
answered on 09 Feb 2011, 11:31 AM
I have changes as you suggest and it fire up when is in edit mode but it don't take any effect, it still shows all repeated options.
Any suggestions.
Thanks in advance

<

 

 

telerik:GridDropDownColumn UniqueName="Tipo" DataSourceID="SqlDataSource1" SortExpression="Tipo" ListTextField="Tipo" EnableEmptyListItem = "true" EmptyListItemText="--Choose an option--" EmptyListItemValue="" ListValueField ="Tipo" HeaderText="Contact name" DataField="Tipo" />

 


protected

 

 

void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e){

 

 

 

if (e.Item is GridEditableItem && (e.Item as GridEditableItem).IsInEditMode){

 

 

 

GridEditableItem editedItem = e.Item as GridEditableItem;

 

 

 

GridEditManager editMan = editedItem.EditManager;

 

 

 

GridDropDownListColumnEditor editor = editMan.GetColumnEditor("Tipo") as GridDropDownListColumnEditor;

 

 

 

//Then you can modify the list control as per your custom conventions

 

 

editor.DataSource =

 

new string[] { "Delegado", "Investigador", "Promotor" };

 

editor.DataBind();

 

 

// editor.DropDownListControl.SelectedValue = "2";

 

}

0
Daniel
Telerik team
answered on 14 Feb 2011, 03:34 PM
Hello Ruben,

Please test the attached demo and let me know if you need further assistance.

Regards,
Daniel
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Ruben Vera
Top achievements
Rank 1
answered on 15 Feb 2011, 05:14 PM
It worked great many thanks !!!

Now I have another problem, I'm tring to add columns in runtime and it works but when i double click to eddit data nothing happens.
I attach my function. Thanks in advance

protected

 

 

void AddColumn(string campo)

 

 

{

 

 

 

//GridBoundColumn

 

 

 

GridDropDownColumn boundColumn = new GridDropDownColumn();

 

 

boundColumn.UniqueName = campo;

 

boundColumn.HeaderText = campo.Substring(0, 1).ToUpper() + campo.Substring(1);

 

boundColumn.DataField = campo;

 

boundColumn.ListTextField=campo;

 

boundColumn.ListValueField=campo;

 

boundColumn.DataSourceID =

 

"SqlDataSource1";

 

 

RadGrid1.MasterTableView.Columns.Add(boundColumn);

 

}

0
Daniel
Telerik team
answered on 21 Feb 2011, 08:32 AM
Hello Ruben,

Could you please post the rest of the code here?
In the meantime I recommend that you take a look at these links:
Creating the grid entirely in the code behind
Dynamically defining the structure of a statically-declared grid

Kind regards,
Daniel
the Telerik team
Tags
Grid
Asked by
Ruben Vera
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Ruben Vera
Top achievements
Rank 1
Daniel
Telerik team
Share this question
or