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

delete raw from rad grid on delete key pressed

5 Answers 65 Views
Grid
This is a migrated thread and some comments may be shown as answers.
ras
Top achievements
Rank 1
ras asked on 09 Jul 2013, 11:34 AM


Hi     All,
             Can anyone tell me how to delete a raw from rad grid in server side on delete key  pressed. what is the defualt navigation key for deleting raw from radgrid

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 09 Jul 2013, 12:06 PM
Hi Ras,

I'm not sure about your requirement ,can u please try the following code snippet to delete a row.If any concern please elaborate on your requirements.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" 
AllowPaging="true" OnDeleteCommand="RadGrid1_DeleteCommand">
<ClientSettings>
    <Scrolling AllowScroll="true" />
</ClientSettings>
<MasterTableView AutoGenerateColumns="False" DataKeyNames="OrderID" CommandItemDisplay="Top">
    <Columns>
        <telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="DeleteColumn" />
        <telerik:GridBoundColumn HeaderText="OrderID" DataField="OrderID" ReadOnly="False"
            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" />       
    </Columns>
</MasterTableView>
</telerik:RadGrid>

C#:
protected void RadGrid1_DeleteCommand(object sender, GridCommandEventArgs e)
   {      
           GridDataItem data= (GridDataItem)e.Item;
           string OrderID = data.GetDataKeyValue("OrderID").ToString();
           conn.Open();
           string query = "DELETE from Orders Where OrderID = '" + OrderID + "'";
           SqlCommand cmd = new SqlCommand(query, conn);
           cmd.ExecuteNonQuery();
           conn.Close();
       }
   }

Thanks,
Princy
0
ras
Top achievements
Rank 1
answered on 11 Jul 2013, 04:26 AM
Hi princi thanks. but my requirement is to delete the selected raw from rad grid on delete key is pressed from keyboard. i found it we can delete the raw from client side but i also need to delete the raw  from the database too.. any help?
0
Accepted
Princy
Top achievements
Rank 1
answered on 11 Jul 2013, 06:57 AM
Hi ras,

Im not sure how you are deleting the row.
In case if you are deleting the row using AllowAutomaticDeletes="true",then on the OnKeyPress event you can write the code to delete the row.
1)
ASPX:
<ClientSettings  AllowKeyboardNavigation="true" Selecting-AllowRowSelect="true" ClientEvents-OnKeyPress="keyPress">
</ClientSettings>

JS:
<script type="text/javascript">
    function keyPress(sender, eventArgs) {
        if (eventArgs.get_keyCode() == 46) {
            $find("<%= RadGrid1.ClientID %>")._deletedItems = [];
      }
 }
</script>

OR
Else another option is to firecommand on Delete to ItemCommand and delete the row.Set AllowAutomaticDeletes="false"
2)

JS:
<script type="text/javascript">  
    function keyPress(sender, eventArgs) {  
        if (eventArgs.get_keyCode() == 46) {
          var masterTable = sender.get_masterTableView();
          masterTable.fireCommand("Delete", "");
        }
    }
</script>

C#:
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.DeleteCommandName)
    {
        //you code to delete from database
    }      
}

Thanks,
Princy
0
ras
Top achievements
Rank 1
answered on 11 Jul 2013, 09:58 AM
Thanks Princy.. The Second one was all i looking for . Its working.. thanks alot, Good Job
0
ras
Top achievements
Rank 1
answered on 11 Jul 2013, 10:06 AM
Hi Princy Thanks.... The Second one was all i looking for. Its working .. thanks alot . Nice job
Tags
Grid
Asked by
ras
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
ras
Top achievements
Rank 1
Share this question
or