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

RadGrid remove record when press delete key in editMode into a radTextBox

1 Answer 50 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Allin
Top achievements
Rank 1
Allin asked on 11 Aug 2014, 01:24 PM
Hi,

I hit Del key into a gridBoundColumn or a gridNumeriColumn in EDIT mode and radgrid fired Delete command...

How to avoid this command? 

If i hit del Key into a description field i don't want delete record.

AllowAutomaticDeletes=False same result.

I don't want to disabled keyboardShortcuts.

If i use a templateColumn with radTextBox to replace gridBoundColumn no delete command fired.

It's a bug?

Thank you.





1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 13 Aug 2014, 05:59 AM
Hi Allin,

I was not able to replicate such an issue at my end. Please try the following sample code. If this doesn't help, provide you full code snippet and more details about your version and steps to replicate this issue.

ASPX:
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="RadGrid1">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1">
                </telerik:AjaxUpdatedControl>
                <telerik:AjaxUpdatedControl ControlID="Label1" LoadingPanelID="RadAjaxLoadingPanel1" />
                <telerik:AjaxUpdatedControl ControlID="RadWindowManager1" LoadingPanelID="RadAjaxLoadingPanel1" />
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" BackColor="AliceBlue"
    Direction="LeftToRight">
    Please Wait
</telerik:RadAjaxLoadingPanel>
<telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource" AllowPaging="true" OnUpdateCommand="RadGrid1_UpdateCommand" OnInsertCommand="RadGrid1_InsertCommand"    OnDeleteCommand="RadGrid1_DeleteCommand">
    <MasterTableView AutoGenerateColumns="False" DataKeyNames="OrderID" CommandItemDisplay="Top" InsertItemDisplay="Top" InsertItemPageIndexAction="ShowItemOnCurrentPage">
        <Columns>
            <telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="DeleteColumn" ConfirmText="Delete" ConfirmDialogType="RadWindow" />
            <telerik:GridBoundColumn HeaderText="OrderID" DataField="OrderID" ReadOnly="true" UniqueName="OrderID" />
            <telerik:GridBoundColumn HeaderText="EmployeeID" DataField="EmployeeID" UniqueName="EmployeeID" />
            <telerik:GridBoundColumn HeaderText="OrderDate" DataField="OrderDate" UniqueName="OrderDate" />
            <telerik:GridBoundColumn HeaderText="ShipName" DataField="ShipName" UniqueName="ShipName" />
            <telerik:GridEditCommandColumn UniqueName="EditCommandColumn" />
        </Columns>
    </MasterTableView>
    <ClientSettings Selecting-AllowRowSelect="true" AllowKeyboardNavigation="true" KeyboardNavigationSettings-AllowSubmitOnEnter="true"  KeyboardNavigationSettings-EnableKeyboardShortcuts="true">
    </ClientSettings>
</telerik:RadGrid>
<asp:Label ID="lblDisplay" runat="server"></asp:Label>

C#:
public static string connection = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(connection);
DataTable dtGrid = new DataTable();
 
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    string selectQuery1 = "SELECT [OrderID], [EmployeeID], [OrderDate], [ShipName] FROM [Orders]";
    SqlDataAdapter adapter1 = new SqlDataAdapter(selectQuery1, conn);
    conn.Open();
    adapter1.Fill(dtGrid);
    conn.Close();
    RadGrid1.DataSource = dtGrid;
}
 
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
{
    GridEditableItem edit = (GridEditableItem)e.Item;
    string OrderID = edit.GetDataKeyValue("OrderID").ToString();
    try
    {
        TextBox txt = (TextBox)edit["EmployeeID"].Controls[0];
        TextBox txt1 = (TextBox)edit["OrderDate"].Controls[0];
        TextBox txt2 = (TextBox)edit["ShipName"].Controls[0];
        conn.Open();
        string query = "UPDATE Orders SET EmployeeID ='" + txt.Text + "', OrderDate='" + txt1.Text + "',ShipName='" + txt2.Text + "' WHERE OrderID = '" + OrderID + "'";
        SqlCommand cmd = new SqlCommand(query, conn);
        cmd.ExecuteNonQuery();
        conn.Close();
        lblDisplay.Text = "Product with ID " + OrderID + " is Updated!";
    }
    catch (Exception ex)
    {
        lblDisplay.Text = "Product with ID " + OrderID + " cannot be Updated!";
    }
}
protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
{
    try
    {
        GridEditableItem edit = (GridEditableItem)e.Item;
        TextBox txt = (TextBox)edit["EmployeeID"].Controls[0];
        TextBox txt1 = (TextBox)edit["OrderDate"].Controls[0];
        TextBox txt2 = (TextBox)edit["ShipName"].Controls[0];
        conn.Open();
        string query = "INSERT into Orders (EmployeeID,OrderDate,ShipName ) VALUES ('" + txt.Text + "','" + txt1.Text + "','" + txt2.Text + "')";
        SqlCommand cmd = new SqlCommand(query, conn);
        cmd.ExecuteNonQuery();
        conn.Close();
        lblDisplay.Text = "Product is Inserted";
    }
    catch (Exception ex)
    {
        lblDisplay.Text = "Product cannot be Inserted!";
    }
}
protected void RadGrid1_DeleteCommand(object sender, GridCommandEventArgs e)
{
    GridDataItem edit = (GridDataItem)e.Item;
    string OrderID = edit.GetDataKeyValue("OrderID").ToString();
    try
    {
        conn.Open();
        string query = "DELETE from Orders Where OrderID = '" + OrderID + "'";
        SqlCommand cmd = new SqlCommand(query, conn);
        cmd.ExecuteNonQuery();
        conn.Close();
        lblDisplay.Text = "Product with ID " + OrderID + " is Deleted!";
    }
    catch (Exception ex)
    {
        lblDisplay.Text = "Product with ID " + OrderID + " cannot be Deleted!";
    }
}

Thanks,
Princy
Tags
Grid
Asked by
Allin
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or