Telerik >
Windows Forms >
Delete Row RadGridView
Example: Add GridViewCommandColumn buton to RadGridView to delete current row
GridViewCommandColumn displays a button element.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.UI;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(
object sender,
EventArgs e)
{
// define text column
GridViewTextBoxColumn colText =
new GridViewTextBoxColumn();
// define
command colum
GridViewCommandColumn colBtnDelete = new GridViewCommandColumn();
colBtnDelete.DefaultText =
"Delete";
colBtnDelete.HeaderText =
"Delete";
colBtnDelete.Name =
"Delete";
colBtnDelete.TextAlignment = System.Drawing.
ContentAlignment.MiddleCenter;
RadGridView1.MasterTemplate.Columns.AddRange(
new Telerik.WinControls.UI.
GridViewDataColumn[] {colText,colBtnDelete});
// add rows to grid
GridViewDataRowInfo rowInfo =
new GridViewDataRowInfo(RadGridView1.MasterView);
rowInfo.Cells[0].Value =
"1";
rowInfo.Cells[1].Value =
"X";
// text for delete button
RadGridView1.Rows.Add(rowInfo);
rowInfo = new
GridViewDataRowInfo(RadGridView1.MasterView);
rowInfo.Cells[0].Value =
"2";
rowInfo.Cells[1].Value =
"X";
RadGridView1.Rows.Add(rowInfo);
rowInfo = new
GridViewDataRowInfo(RadGridView1.MasterView);
rowInfo.Cells[0].Value =
"3";
rowInfo.Cells[1].Value =
"X";
RadGridView1.Rows.Add(rowInfo);
}
private void RadGridView1_CommandCellClick(
object sender,
EventArgs e)
{
// handle CommandCellClick event
GridCommandCellElement _cmd =(
GridCommandCellElement)(sender);
// if text for delete button = X
if ( _cmd.Value.ToString() ==
"X")
RadGridView1.Rows.Remove(RadGridView1.CurrentRow); // remove current row
}
}
}