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

update grid

5 Answers 108 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Minh
Top achievements
Rank 1
Minh asked on 22 Aug 2011, 06:54 PM
Hi,

I'm using the update feature of the grid. during the update, I use the

OnItemDataBoundHander(object sender, GridItemEventArgs e)

to prepopulate the RadComboBox as instructed within the examples. the following contains the C# code 

 

if (e.Item.IsInEditMode)  

GridEditableItem item = (GridEditableItem)e.Item;  

if (!(e.Item is IGridInsertItem))  

//this area is for update  

 

using (dl = new WellVentDataLayer())  

//For the Area section  

 

RadComboBox combo = (RadComboBox)item.FindControl("RadComboBoxArea");  

RadComboBoxItem selectedItem = new RadComboBoxItem(); 

selectedItem.Text = ((DataRowView)e.Item.DataItem)["AreaName"].ToString();  

combo.Items.Add(selectedItem);

selectedItem.DataBind();

}

}

}

and the following aspx code 

 

<telerik:GridTemplateColumn UniqueName="AreaName" HeaderText="Area" 

SortExpression="AreaName" ItemStyle-Width="150px" DataField="_AreaName"  

AndCurrentFilterFunction="Contains">  

 

<FooterTemplate

Template footer 

</FooterTemplate>  

 

<FooterStyle VerticalAlign="Middle" HorizontalAlign="Center" />  

 

<ItemTemplate

<%

#DataBinder.Eval(Container.DataItem, "_AreaName")%>  

</ItemTemplate>  

 

<EditItemTemplate>  

 

<telerik:RadComboBox runat="server" ID="RadComboBoxArea" EnableLoadOnDemand="True" DataTextField="AreaName" OnItemsRequested="RadComboBoxArea_ItemsRequested" DataValueField="ID" AutoPostBack="true" HighlightTemplatedItems="true" Height="140px" Width="100px" DropDownWidth="200px"  

 

OnSelectedIndexChanged="ComboBox1_OnSelectedIndexChangedHandler"> 

<ItemTemplate> 

<%# DataBinder.Eval(Container, "Text")%>  

</ItemTemplate>  

 

</telerik:RadComboBox>  

 

</EditItemTemplate>  

 

<HeaderStyle Width="100px" />  

 

<ItemStyle Width="100px"></ItemStyle>  

 

</telerik:GridTemplateColumn>

  

 

 

the code gave the following error on the row

Unable to cast object of type 'GHG_MCA.WellVentEvents' to type 'System.Data.DataRowView'.
 

selectedItem.Text = ((DataRowView)e.Item.DataItem)["AreaName"].ToString();  

 

thanks,
Minh Bui

5 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 23 Aug 2011, 05:38 AM
Hello,

please check below code snippet for get the text from column.

GridDataItem item = e.Item as GridDataItem; 
            TableCell cell = item["ColumnName"]; 
  
            string strTemp = cell.Text;


Thanks,
Jayesh Goyani
0
Minh
Top achievements
Rank 1
answered on 23 Aug 2011, 05:43 AM
Hi,

this is a combobox column.....also, it is in edit/update mode...I'm looking to populate the dropdown w/ the predefined value.

thanks,
Minh Bui
0
Jayesh Goyani
Top achievements
Rank 2
answered on 23 Aug 2011, 06:17 AM
Hello,

Sorry but can you elaborate your scenario.
so we can find perfect solution for this.

Thanks,
Jayesh Goyani
0
Minh
Top achievements
Rank 1
answered on 23 Aug 2011, 08:20 PM
Hi,

I have 3 Radcombobox in 3 different GridTemplateColumn (view picture1.jpg). I'm using the c# code to perform the update command as follows:

 

 

protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)

 

{
//call a stored procedure to perform the update
}

upon clicking 'edit' to start the update process (see 2.jpg), the 3 combo box fails to retain the original value. So I'm trying to use the ItemDataBound event of the grid to capture and reload the original value...I followed the example named ComboInGrid with code as follow

 

 

 

protected void OnItemDataBoundHandler(object sender, GridItemEventArgs e)

{

if (e.Item.IsInEditMode)

{

GridEditableItem item = (GridEditableItem)e.Item;

if (!(e.Item is IGridInsertItem))

{

RadComboBox combo = (RadComboBox)item.FindControl("RadComboBox1");

RadComboBoxItem selectedItem = new RadComboBoxItem();

selectedItem.Text = ((DataRowView)e.Item.DataItem)["CompanyName"].ToString();

selectedItem.Value = ((DataRowView)e.Item.DataItem)["SupplierID"].ToString();

selectedItem.Attributes.Add("ContactName", ((DataRowView)e.Item.DataItem)["ContactName"].ToString());

combo.Items.Add(selectedItem);

selectedItem.DataBind();

Session["SupplierID"] = selectedItem.Value; }}}

on my code, it fail on the DataRowView casting....any help on repopulating the combobox during update is appreciated.

thanks,
Minh Bui

0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 24 Aug 2011, 06:03 AM
Hello,

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditFormItem && e.Item.IsInEditMode && !e.Item.OwnerTableView.IsItemInserted)
        //if the item is about to edit
        {
            GridEditFormItem editItem = (GridEditFormItem)e.Item;
            RadComboBox RadComboBoxArea = (RadComboBox)editItem.FindControl("RadComboBoxArea");
            // bind your Combo here
 
            //method 1
            HiddenField hfAreaName = (HiddenField)editItem.FindControl("hfAreaName");
            if (!string.IsNullOrEmpty(hfAreaName.Value))
            {
                if (RadComboBoxArea.FindItemByText(hfAreaName.Value) != null) //FindItemByValue
                {
                    RadComboBoxArea.FindItemByText(hfAreaName.Value).Selected = true;
                }
            }
            //method 2
            string _AreaName = editItem.GetDataKeyValue("_AreaName").ToString();
            if (RadComboBoxArea.FindItemByText(_AreaName) != null)
            {
                RadComboBoxArea.FindItemByText(_AreaName).Selected = true;
            }
        }
        if (e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted)
        //if the item is about to insert
        {
            GridEditFormInsertItem insertItem = (GridEditFormInsertItem)e.Item;
        }
    }
<MasterTableView DataKeyNames="_AreaName">
.............
............
 <telerik:GridTemplateColumn UniqueName="AreaName" HeaderText="Area" SortExpression="AreaName"
                        ItemStyle-Width="150px" DataField="_AreaName" AndCurrentFilterFunction="Contains">
                        <FooterTemplate>
                            Template footer
                        </FooterTemplate>
                        <FooterStyle VerticalAlign="Middle" HorizontalAlign="Center" />
                        <ItemTemplate>
                            <% Eval("_AreaName")%>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:HiddenField ID="hfAreaName" runat="server" Value=' <% Eval("_AreaName")%>' />
                            <telerik:RadComboBox runat="server" ID="RadComboBoxArea" EnableLoadOnDemand="True"
                                DataTextField="AreaName" OnItemsRequested="RadComboBoxArea_ItemsRequested" DataValueField="ID"
                                AutoPostBack="true" HighlightTemplatedItems="true" Height="140px" Width="100px"
                                DropDownWidth="200px">
                                <ItemTemplate>
                                    <%# DataBinder.Eval(Container, "Text")%>
                                </ItemTemplate>
                            </telerik:RadComboBox>
                        </EditItemTemplate>
                    </telerik:GridTemplateColumn>


Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Minh
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Minh
Top achievements
Rank 1
Share this question
or