Binding a RadCombobox inside a radgrid - c#

Please can someone point me in the right direction, i have a radcombobox inside the radgrid, as soon as i edit the row it loses it's value
<telerik:GridTemplateColumn DataField="SupplierRegion" UniqueName="SupplierRegion" HeaderText="Region">
<ItemTemplate>
<asp:HyperLink runat="server" ID="SupplierRegionHyperlink" Text='<%# Eval("SupplierRegion")%>'></asp:HyperLink>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadComboBox runat="server" ID="SupplierRegionRadComboBox" EnableLoadOnDemand="true" AutoPostBack="true" >
<WebServiceSettings Method="GetRegions" Path="~/WebServices/SuppliersWS.asmx"></WebServiceSettings>
</telerik:RadComboBox>
</EditItemTemplate>
</telerik:GridTemplateColumn>
and the below is my C# code
if (e.CommandName == "Edit")
{
GridEditCommandColumn editColumn = (GridEditCommandColumn)SupplierSearchGrid.MasterTableView.GetColumn("EditCommandColumn");
if (!editColumn.Visible)
editColumn.Visible = true;
GridEditableItem item = (GridEditableItem)e.Item;
RadComboBox SupplierRegionValue = (RadComboBox)item.FindControl("SupplierRegionRadComboBox");
SupplierRegionValue.SelectedValue = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["RegionID"].ToString();
SupplierRegionValue.Text = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Description"].ToString();
}
in my edit it can't seem to find the Combobox as it returns null

Take a look at topic How to bind data to radcombobox inside grid EditItemTemplate
XAML
<telerik:GridTemplateColumn DataField="SupplierRegion" UniqueName="SupplierRegion" HeaderText="Region">
<ItemTemplate>
<asp:HyperLink runat="server" ID="SupplierRegionHyperlink" Text='<%# Eval("SupplierRegion")%>'></asp:HyperLink>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadComboBox runat="server" ID="SupplierRegionRadComboBox" EnableLoadOnDemand="true" AutoPostBack="true" >
<WebServiceSettings Method="GetRegions" Path="~/WebServices/SuppliersWS.asmx"></WebServiceSettings>
</telerik:RadComboBox>
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridEditCommandColumn FooterText="EditCommand footer" UniqueName="EditCommandColumn"
HeaderText="Edit" HeaderStyle-Width="100px" UpdateText="Update">
</telerik:GridEditCommandColumn>
C#
protected void gvSupplierRegion_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem myGridItem = (GridDataItem)e.Item;
if (myGridItem.IsInEditMode)
{
RadComboBox combo = (RadComboBox)myGridItem["SupplierRegion"].FindControl("SupplierRegionRadComboBox");
combo.DataSource = GetUploadStatus();
combo.DataTextField = "Value";
combo.DataValueField = "Key";
combo.DataBind();
combo.SelectedValue = DataBinder.Eval(myGridItem.DataItem, "UploadStatus").ToString();
}
}

so for these with the same issue i found the solution
in the aspx page add the the webservice's id and description colum id in the datakeynames, then in your radcombobox in the edititemtemplate
<telerik:GridTemplateColumn DataField="SupplierRegion" UniqueName="SupplierRegion" HeaderText="Region">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"Description")%>'
</ItemTemplate>
<EditItemTemplate>
<telerik:RadComboBox runat="server" ID="SupplierRegionRadComboBox" AllowCustomText="false" EnableLoadOnDemand="true" AutoPostBack="true" Text='<%#Bind("Description") %>' >
<WebServiceSettings Method="GetRegions" Path="~/WebServices/SuppliersWS.asmx"></WebServiceSettings>
</telerik:RadComboBox>
</EditItemTemplate>
</telerik:GridTemplateColumn>
and in the code behind
protected void SuppliersSearchRadGrid2_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item.IsInEditMode)
{
GridEditableItem editItem = (GridEditableItem)e.Item;
RadComboBox combo = (RadComboBox)editItem.FindControl("SupplierRegionRadComboBox");
combo.SelectedValue = DataBinder.Eval(editItem.DataItem, "RegionID").ToString();
}
}

Related

RadButton - Single Click approach, not working inside RadGrid EditItemTemplate

There is a RadGrid inside which there is a RadComboBox and asp Button in
EditItemTemplate.
Below is the current code:
<telerik:GridTemplateColumn UniqueName="AccountCode" HeaderText="Account Code">
<ItemTemplate>
<asp:Label ID="lblAcCode" runat="server" Text='<%# Eval("AccountCode")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadComboBox ID="ddlAccountCode" runat="server" Height="200" Width="240" DropDownWidth="310"
EnableLoadOnDemand="True" OnItemsRequested="ddlAccountCode_ItemsRequested" EnableItemCaching="true"
ShowMoreResultsBox="True" EnableVirtualScrolling="true" AllowCustomText="true" MarkFirstMatch="true"
Filter="Contains" HighlightTemplatedItems="true" CausesValidation="true" AppendDataBoundItems="true"
DataTextField="AccountDescription" DataValueField="AccountCodeID"
ShowDropDownOnTextboxClick="false"
OnClientDropDownOpening="OnClientDropDownOpening" OnClientItemsRequested="OnClientItemsRequested"
OnClientTextChange="LoadECnEntityKeys" />
<asp:Button ID="btnSearch" runat="server" Text="Search" OnClient="btnSearch_Click" />
<asp:Label ID="lblMsg" runat="server" Visible="false"></asp:Label>
</EditItemTemplate>
</telerik:GridTemplateColumn>
protected void btnSearch_Click(object sender, EventArgs e)
{
Response.Write("Default.aspx");
//other code
}
When I type/key-in something inside RadComboBox and click on asp Button,
then only the searching related to key-in text starts and display after execution of OnClick event of asp Button.
Now, the new requirement came to place RadButton(with - Single Click
approach) in place of asp Button, to avoid double click.
Problem is: when I implement RadButton inside EditItemTemplate of RadGrid, RadButton never postback i.e., when I click on it nothing happens.
But same RadButton when I use outside of RadGrid, is working fine.
Below is the code using RadButton(with - Single Click
approach):
<telerik:GridTemplateColumn UniqueName="AccountCode" HeaderText="Account Code">
<ItemTemplate>
<asp:Label ID="lblAcCode" runat="server" Text='<%# Eval("AccountCode")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadComboBox ID="ddlAccountCode" runat="server" Height="200" Width="240" DropDownWidth="310"
EnableLoadOnDemand="True" OnItemsRequested="ddlAccountCode_ItemsRequested" EnableItemCaching="true"
ShowMoreResultsBox="True" EnableVirtualScrolling="true" AllowCustomText="true" MarkFirstMatch="true"
Filter="Contains" HighlightTemplatedItems="true" CausesValidation="true" AppendDataBoundItems="true"
DataTextField="AccountDescription" DataValueField="AccountCodeID"
ShowDropDownOnTextboxClick="false"
OnClientDropDownOpening="OnClientDropDownOpening" OnClientItemsRequested="OnClientItemsRequested"
OnClientTextChange="LoadECnEntityKeys" />
<telerik:RadButton runat="server" ID="btnSearch" Text="Search" SingleClick="true"
SingleClickText="Submitting..." OnClick="btnSearch_Click" />
<asp:Label ID="lblMsg" runat="server" Visible="false"></asp:Label>
</EditItemTemplate>
</telerik:GridTemplateColumn>
protected void btnSearch_Click(object sender, EventArgs e)
{
Response.Write("Default.aspx");
//other code
}
Please let me know why is this hapenning?
Please do reply
Thanks in advance
I would recommend you to use CommandName as button event. Anyway here is my code... I tried use OnClick and CommandName it work perfectly fine. I suspect your error will be some sort of javascript...
.aspx
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" Width="100%"
OnNeedDataSource="RadGrid1_NeedDataSource" OnItemCommand="RadGrid1_ItemCommand"
OnItemDataBound="RadGrid1_ItemDataBound">
<MasterTableView EditMode="InPlace">
<Columns>
<telerik:GridTemplateColumn>
<ItemTemplate>
<asp:Label ID="lblAcCode" runat="server" Text='<%# Eval("T")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadComboBox ID="rcb" runat="server" AllowCustomText="true">
</telerik:RadComboBox>
<telerik:RadButton runat="server" ID="btnSearch" Text="Search"
SingleClick="true" SingleClickText="Submitting..." CommandName="Search" />
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn>
<ItemTemplate>
<telerik:RadButton ID="btnEdit" runat="server"
Text="Edit" CommandName="Edit"></telerik:RadButton>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadButton ID="btnCancel" runat="server" Text="Cancel"
CommandName="Cancel"></telerik:RadButton>
</EditItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
.cs
protected void Page_Load(object sender, EventArgs e)
{
// Check
if (!IsPostBack)
{
// Variable
DataTable dt = new DataTable();
dt.Columns.Add("T");
// Loop & Add
for (int i = 0; i < 10; i++)
dt.Rows.Add(i + "");
// Check & Bind
if (dt != null)
{
ViewState["Grid"] = dt;
RadGrid1.DataSource = dt;
RadGrid1.DataBind();
// Dispose
dt.Dispose();
}
}
}
protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
RadGrid1.DataSource = ViewState["Grid"] as DataTable;
}
protected void btnSearch_Click(object sender, EventArgs e)
{
Response.Write("GG");
}
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
// Check
if (e.CommandName == "Search")
{
// Variable
GridEditableItem item = e.Item as GridEditableItem;
string something = "";
// Find Control
RadComboBox rcb = item.FindControl("rcb") as RadComboBox;
// Check
if (rcb != null)
{
// Set
something = rcb.Text;
// Do Something
Response.Write(something);
}
}
}
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
// Check
if (e.Item is GridEditableItem)
{
// FindControl
RadComboBox rcb = e.Item.FindControl("rcb") as RadComboBox;
// Check
if (rcb != null)
{
rcb.DataSource = ViewState["Grid"] as DataTable;
rcb.DataTextField = "T";
rcb.DataValueField = "T";
rcb.DataBind();
}
}
}

How to enable column in GridView?

I've a ASP.NET GridView with the following data:
Rows will be disable OnRowDataBound based on the value on column3.
GridView :
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
onrowdatabound="GridView1_RowDataBound1">
<Columns>
<asp:TemplateField HeaderText="Column1">
<ItemTemplate>
<asp:HyperLink ID="hyperlink" runat="server" Text='<% #Eval("Dosage") %>' NavigateUrl='<% #Eval("Dosage") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Column2">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<% #Eval("Drug") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Column3">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<% #Eval("Patient") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Column4">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<% #Eval("Date") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
RowDataBound :
protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label a = e.Row.FindControl("Label3") as Label;
if (a.Text == "Sam")
{
e.Row.Enabled = false;
e.Row.Cells[0].Enabled = true;
}
}
}
however, I want column1 always enable, hyperlink in column1 should always clickable.
I've tried get the cells and enabled it, but it is not working.
kindly advise what is the workaround for the issue above.
You can do this by enable/disable particular cell.
protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label a = e.Row.FindControl("Label3") as Label;
if (a.Text == "Sam")
{
e.Row.Cells[0].Enabled = true;
e.Row.Cells[1].Enabled = false;
e.Row.Cells[2].Enabled = false;
e.Row.Cells[3].Enabled = false;
}
}
}

telerik:radgrid-asp:checkbox inside template column:event for checkbox box changed on griditemcommand

i am using telerik grid in c#.
this is my code for grid columns.
<telerik:GridTemplateColumn HeaderText="Active" UniqueName="Active">
<ItemTemplate>
<asp:CheckBox ID="chbActive" runat="server"></asp:CheckBox>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="Order" UniqueName="ORDER_FLAG">
<ItemTemplate>
<asp:CheckBox ID="chbORDER_FLAG" runat="server"></asp:CheckBox>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="Sell" UniqueName="SELL_FLAG">
<ItemTemplate>
<asp:CheckBox ID="chbSELL_FLAG" runat="server"></asp:CheckBox>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="Auto GRN" UniqueName="Auto_GRN">
<ItemTemplate>
<asp:CheckBox ID="ChkAutoGrn" runat="server"></asp:CheckBox>
</ItemTemplate>
</telerik:GridTemplateColumn>
now in this case whenever i checked 'Auto GRN' checkbox ,the 'Order' checkbox should unchecked automatically in grid and same condition for reverse senario..
how i will achieve this..
Please check below demo.
.aspx
<telerik:RadGrid ID="RadGrid3" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid3_NeedDataSource"
OnItemDataBound="RadGrid3_ItemDataBound">
<MasterTableView>
<Columns>
<telerik:GridTemplateColumn>
<ItemTemplate>
<asp:CheckBox ID="Chk1" runat="server" />
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn>
<ItemTemplate>
<asp:CheckBox ID="Chk2" runat="server" />
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn HeaderText="ID" DataField="ID" UniqueName="ID">
</telerik:GridBoundColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
.aspx.cs
protected void RadGrid3_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
dynamic data = new[] {
new { ID = 1, Name ="Name1"},
new { ID = 2, Name = "Name2"},
new { ID = 3, Name = "Name3"}
};
RadGrid3.DataSource = data;
}
protected void RadGrid3_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = e.Item as GridDataItem;
CheckBox Chk1 = item.FindControl("Chk1") as CheckBox;
CheckBox Chk2 = item.FindControl("Chk2") as CheckBox;
// default first check box will be checked
Chk1.Checked = true;
Chk1.Attributes.Add("onclick", "checkUncheckManage(this,'" + Chk2.ClientID+ "')");
Chk2.Attributes.Add("onclick", "checkUncheckManage(this,'" + Chk1.ClientID + "')");
}
}
JS
function checkUncheckManage(chkA, chkB) {
var _chkB = document.getElementById(chkB);
_chkB.checked = !chkA.checked;
}

How can I get a control inside my EditItemTemplate?

I have a DetailsView bound to an EntityDataSource and am trying to get the values from my TextBoxes in the EditItemTemplates.
Here is my code:
<asp:DetailsView ID="DetailsView1" DataKeyNames="Name" runat="server" AutoGenerateRows="False"
OnDataBound="DetailsView_DataBound" DataSourceID="eds2" BorderWidth="0"
OnModeChanging="OnModeChanging" AutoGenerateEditButton="true"
OnItemUpdated="DetailsView_OnItemUpdated" OnItemUpdating="DetailsView_OnItemUpdating"
EmptyDataText="N/A" OnDataBinding="DetailsView_OnDataBinding" CellPadding="0"
CellSpacing="7" GridLines="None" CssClass="Center">
<Fields>
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<Fields>
</asp:DetailsView>
And the code behind:
protected void OnModeChanging(object sender, DetailsViewModeEventArgs e)
{
foreach (DetailsViewRow row in DetailsView1.Rows)
{
if (row.RowType != DataControlRowType.DataRow) continue;
foreach (DataControlFieldCell cell in row.Cells)
{
var textbox = cell.FindControl("txtName");
var textbox2 = row.FindControl("txtName");
}
}
}
textbox and textbox2 are always null. What am I doing wrong? How can I get either the textbox or the value inside it?
You have these textboxes declared in your edit template. These will only show up when your mode has been set to edit. I'm guessing this hasn't happened yet when the ModeChanging event is fired.
Put your code in your ModeChanged event, and check to see that you're editing.
void DetailsView1_ModeChanged(object sender, EventArgs e)
{
if (DetailsView1.CurrentMode != DetailsViewMode.Edit)
return;
foreach (DetailsViewRow row in DetailsView1.Rows)
{
var textbox = row.FindControl("txtName");
}
}

Telerik - Image button inside a Grid row, getting value of first cell

I have a RadGrid that contains a template column in which I've put two image buttons for edit and delete actions.
<telerik:GridTemplateColumn HeaderText="Actions">
<ItemTemplate>
<asp:ImageButton ID="btnEdit" runat="server" ImageUrl="~/images/icon_edit.png" style="display: inline-block" ToolTip="Edit" /> <asp:ImageButton ID="btnDelete" runat="server" ImageUrl="~/images/icon_delete.png" style="display: inline-block" ToolTip="Delete" />
</ItemTemplate>
</telerik:GridTemplateColumn>
How would get the value of the first cell of the row (datafield = "User_ID") when I click on the "delete" button?
Step 1.
Go to the Radgrid itself and edit the field DataKeyNames="" (under MasterTableView) and add the datafield you are pulling:
<MasterTableView ... DataKeyNames="User_ID">
Step 2.
Edit the CommandName="" property of the Imagebuttons located in the grid:
<asp:ImageButton ID="btnDelete" runat="server" style="display: inline-block" ToolTip="Delete" CommandName="dosomething"/>
Create the following method for your Radgrid and add this code:
protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
if (e.CommandName == "dosomething")
{
//Use a line of code here to save that User_ID that you want from the first column
theUserId = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["User_ID"];
}
}
Make sure theUserId = the same Type (int,double,dec...) as the field it's pulling from or you will have to parse it:
theUserId = Int.Parse(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["User_ID"]);
Let me know if you need more help.
Please check Below code snippet.
<MasterTableView DataKeyNames="ID">
<Columns>
<telerik:GridBoundColumn DataField="Name" HeaderText="Name" UniqueName="Name">
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn HeaderText="Actions">
<ItemTemplate>
<asp:Button ID="btnEdit" runat="server" ToolTip="Edit" CommandName="Edit" /> <asp:Button
ID="btnDelete" runat="server" ToolTip="Delete" CommandName="Delete" />
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
.........................
protected void grdCompCliente_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
GridDataItem item = e.Item as GridDataItem;
string ID = item.GetDataKeyValue("ID").ToString();
string Name = item["Name"].Text;
}
else if (e.CommandName == "Delete")
{
GridDataItem item = e.Item as GridDataItem;
string ID = item.GetDataKeyValue("ID").ToString();
string Name = item["Name"].Text;
}
}

Categories