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

[Solved] "InPlace" Editing in Grid

2 Answers 152 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Katte
Top achievements
Rank 1
Katte asked on 30 May 2009, 10:00 AM
I am new to rad grid. I have tried the following sample code for <EditItemTemplate>. But when I clicked the edit button, nothing was in the grid. When I saw the demo, everything was done using SQLDataSource. So I have confusion on what I have missed. Can anyone suggest me what I have missed in the following code.

<telerik:RadGrid ID="RadGrid1" runat="server" Skin="Black" OnItemDeleted="RadGrid1_ItemDeleted" OnItemUpdated="RadGrid1_ItemUpdated">
                <MasterTableView AutoGenerateColumns="False" DataKeyNames="ProductID" CommandItemDisplay="Top"
                    EditMode="InPlace">
                    <Columns>
                        <telerik:GridTemplateColumn DataField="ProductID" HeaderText="Product ID" UniqueName="ProductID">
                            <ItemTemplate>
                                <asp:Label runat="server" ID="lblProductID" Text='<%# Eval("ProductID") %>'></asp:Label>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridTemplateColumn DataField="ProductName" HeaderText="Product Name" UniqueName="ProductName">
                            <ItemTemplate>
                                <asp:Label runat="server" ID="lblProductName" Text='<%# Eval("ProductName") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox runat="server" ID="txtProductName" Text='<%# Bind("ProductName") %>'></asp:TextBox>
                                <span style="color: Red">*</span>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txtProductName"
                                    ErrorMessage="Required" runat="server">
                                </asp:RequiredFieldValidator>
                            </EditItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridTemplateColumn HeaderText="UnitPrice" SortExpression="UnitPrice" UniqueName="TemplateColumn"
                            EditFormColumnIndex="1">
                            <ItemTemplate>
                                <asp:Label runat="server" ID="lblUnitPrice" Text='<%# Eval("UnitPrice", "{0:C}") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox runat="server" ID="txtUnitPrice" Text='<%# Bind("UnitPrice") %>'></asp:TextBox>
                                <span style="color: Red">*</span>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txtUnitPrice"
                                    ErrorMessage="Required" runat="server">
                                </asp:RequiredFieldValidator>
                            </EditItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridEditCommandColumn ButtonType="ImageButton">
                            <ItemStyle CssClass="MyImageButton" />
                        </telerik:GridEditCommandColumn>
                        <telerik:GridButtonColumn ConfirmText="Delete this product?" ConfirmDialogType="RadWindow"
                            ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" Text="Delete"
                            UniqueName="DeleteColumn">
                            <ItemStyle HorizontalAlign="Center" CssClass="MyImageButton" />
                        </telerik:GridButtonColumn>
                    </Columns>
                </MasterTableView>
            </telerik:RadGrid>

Code Behind: 

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindProducts();
        }
    }

private void BindProducts()
{
        try 
   {
            conn = new SqlConnection("Data Source=server1;Initial Catalog=Northwind;User ID=sa;Password=sa");
            conn.Open();
            SqlCommand cmd = new SqlCommand("select * from Products", conn);
            SqlDataAdapter ad = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            ad.Fill(ds);
            RadGrid1.DataSource = ds;
            RadGrid1.DataBind();
   }
   finally
   {
            if (conn != null)
            {
                conn.Close();
            }
   }
}

    protected void RadGrid1_ItemDeleted(object source, GridDeletedEventArgs e)
    {
        GridDataItem dataItem = (GridDataItem)e.Item;
        String id = dataItem.GetDataKeyValue("ProductID").ToString();
....
        // code to handle deletion process
....
    }

    protected void RadGrid1_ItemUpdated(object source, Telerik.Web.UI.GridUpdatedEventArgs e)
    {
            GridEditableItem item = (GridEditableItem)e.Item;
            String id = item.GetDataKeyValue("ProductID").ToString();
     ....
             // code to handle Updation process
     ....           
     }
Thanks in advance,
Katte

2 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 30 May 2009, 10:20 AM
Hello Katte,

Please use a datasource control or advanced data binding to avoid this problem. I recommend you examine the following links:
Advanced Data-binding
Declarative DataSource
Advanced Data Binding demo

Best regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Katte
Top achievements
Rank 1
answered on 01 Jun 2009, 05:50 AM
hi Daniel,

Thanks. Now its working fine.

Regards,
Katte.
Tags
Grid
Asked by
Katte
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Katte
Top achievements
Rank 1
Share this question
or