Philipe
Philipe

Reputation: 27

Conditional Delete after button click (radgridview)

assume I have 2 columns in gridview : column A and column B.
I want to delete the whole row if Column B has no value.
this code checks column B if it doesn't have any value. next, how to enter the delete command based on the value obtained from the code ?

private void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRowInfo row in DGV1.Rows)
        {
            if (row.Cells[1].Value == null || Convert.ToString(row.Cells[1].Value) == string.Empty)
            {
                MessageBox.Show("Null value");
            }
        }
    }

Upvotes: 0

Views: 262

Answers (1)

IV.
IV.

Reputation: 9486

Hopefully I can offer a couple of tips for working with RadGridView or really most any grid view. First, use BeginInvoke to avoid blocking the Click message which allows the UI thread to return from the click handler (mouse returns to the up position, any new button state is painted).

private void buttonRemove_Click(object sender, EventArgs e)
{
    // Do not block the click event.
    BeginInvoke((MethodInvoker)delegate 
    {
        onButtonRemove();
    });
}

Next make an array of records (or rows) that need to be removed. If your view is bound to a DataSource this is especially easy using System.Linq. Then simply remove from DataSource.

void onButtonRemove()
{
    // Perform Linq query to see what needs to be removed
    var removes = 
        DataSource
        .Where(record => string.IsNullOrWhiteSpace(record.ColumnB));

    // Cast to an array before iterating to
    // avoid "CollectionWasModified" exception.
    foreach (var record in removes.ToArray())
    {
        DataSource.Remove(record);
    }
}

This particular code defines a row with this Record class:

class Record
{
    public string ColumnA { get; set; } = "SomeValue";
    public string ColumnB { get; set; }
}

If you were using a Winforms DataGridView it could initialize like this:

BindingList<Record> DataSource = new BindingList<Record>();
private void InitializeDataGridView()
{
    dataGridView1.AllowUserToAddRows = false;
    dataGridView1.DataSource = DataSource;
    // Add one or more records to auto-create columns.
    DataSource.Add(new Record { ColumnB = "Not empty or null"});
    DataSource.Add(new Record { ColumnB = String.Empty});
    DataSource.Add(new Record { ColumnB = null});

    // Column formatting
    dataGridView1.Columns[nameof(Record.ColumnA)].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
    dataGridView1.Columns[nameof(Record.ColumnB)].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}

before and after

Your post is for Telerik.WinControls.UI.RadGridView but the initialization is very similar:

BindingList<Record> DataSource = new BindingList<Record>();
private void InitializeDataGridView()
{
    DGV1.DataSource = DataSource;
    // Add one or more records to auto-create columns.
    DataSource.Add(new Record { ColumnB = "Not empty or null"});
    DataSource.Add(new Record { ColumnB = String.Empty});
    DataSource.Add(new Record { ColumnB = null});

    // Column formatting
    DGV1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
}

RadGridView

Hope this helps get you where you want to be.

Upvotes: 1

Related Questions