RadGrid Edit Row

1 posts, 0 answers
  1. osman
    osman avatar
    1 posts
    Member since:
    Aug 2019

    Posted 15 Sep 2019 Link to this post

    Hi, i have problem whne click save in edit mode,  my problem when i click edit and fill text box after save click i got no change , no save to database and save don go back to edit mode in

    my code is

    <telerik:RadGrid ID="RadGrid1" OnNeedDataSource="RadGrid1_NeedDataSource" ClientSettings-EnablePostBackOnRowClick="true" OnUpdateCommand="RadGrid1_UpdateCommand" runat="server" Skin="Bootstrap" AutoGenerateColumns="False" AllowSorting="True" CellSpacing="-1" GridLines="Both" >

                        <GroupingSettings CollapseAllTooltip="Collapse all groups"></GroupingSettings>
                        <ClientSettings>
                            <Selecting AllowRowSelect="True" />
                        </ClientSettings>
                        <MasterTableView EditMode="InPlace" CommandItemDisplay="Bottom" CommandItemSettings-ShowAddNewRecordButton="false" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" AlternatingItemStyle-HorizontalAlign="Center" DataKeyNames="ID"  >
                            <Columns>
                                <telerik:GridBoundColumn ReadOnly="true" DataField="da" DataType="System.DateTime" FilterControlAltText="Filter da column" HeaderText="date"  SortExpression="da" UniqueName="da">
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn ReadOnly="true" DataField="GivenName" FilterControlAltText="Filter GivenName column" HeaderText="GivenName" SortExpression="GivenName" UniqueName="GivenName">
                                </telerik:GridBoundColumn>

                                <telerik:GridTemplateColumn  DataField="GateComment" HeaderText="GateComment" ItemStyle-Width="240px" UniqueName="GateComment" ShowFilterIcon="false" AutoPostBackOnFilter="true" SortExpression="GateComment">
                                    <ItemTemplate>
                                        <%#DataBinder.Eval(Container.DataItem, "GComment")%>
                                    </ItemTemplate>
                                    <EditItemTemplate>

                                        <asp:TextBox ID="GateComment" runat="server" Text='<%# Bind("GComment") %>'>
                                        </asp:TextBox>
                                    </EditItemTemplate>
                                </telerik:GridTemplateColumn>

     

     

     

    and my code behind

     


            protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
            {
                String ConnString = ConfigurationManager.ConnectionStrings["TR"].ConnectionString;
                SqlConnection conn = new SqlConnection(ConnString);
                SqlDataAdapter adapter = new SqlDataAdapter();
                adapter.SelectCommand = new SqlCommand();

                adapter.SelectCommand.CommandText = "SELECT GComment, GivenName, da, ScID as ID from tblSC";


                adapter.SelectCommand.Connection = conn;
                var myDataTable = new DataTable();

                conn.Open();
                try
                {
                    adapter.Fill(myDataTable);
                }
                finally
                {
                    conn.Close();
                }

                RadGrid1.DataSource = myDataTable;
            }

     

     

     protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
            {
                //Get the GridEditableItem of the RadGrid    
                GridEditableItem editedItem = e.Item as GridEditableItem;
                //Get the primary key value using the DataKeyValue.    
                string ID = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["ID"].ToString();

            
                TextBox GateComment = (TextBox)editedItem.FindControl("GComment");
                string connectionString = ConfigurationManager.ConnectionStrings["TR"].ConnectionString;
                SqlConnection SqlConnection = new SqlConnection(connectionString);



                try
                {

                    using (SqlCommand cmb = new SqlCommand())
                    {
                        if ((e.Item is GridEditableItem) && e.Item.IsInEditMode)
                        {
                            //edit operation triggered   
                            cmb.CommandText = "update tblSC set GComment = '" + GComment.Text + "'  where ID = " + ID + "";
                        }






                        cmb.Connection = SqlConnection;

                        SqlConnection.Open();
                        cmb.ExecuteNonQuery();
                    }
                }



                catch (Exception ex)
                {
                    RadGrid1.Controls.Add(new LiteralControl("Unable to update the field. Reason: " + ex.Message));
                    e.Canceled = true;
                }
            }

     

     

Back to Top