单击按钮后有条件删除(radgridview)

发布于 2025-02-07 12:06:44 字数 434 浏览 0 评论 0原文

假设我在GridView中有2列:A列和B列B。
如果列B没有值,我想删除整个行。
此代码如果没有任何值,请检查B列B。接下来,如何根据从代码获得的值输入删除命令?

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");
            }
        }
    }

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");
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏佲洛 2025-02-14 12:06:44

希望我可以提供一些用于使用radgridview或实际上所有网格视图的技巧。首先,使用beginInvoke避免阻止单击消息,该消息允许UI线程从单击处理程序返回(鼠标返回到UP位置,绘制了任何新按钮状态)。

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

接下来,制作需要删除的一系列记录(或行)。如果您的视图绑定到DataSource使用system.linq特别容易。然后只需从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);
    }
}

特定代码使用此记录类定义一行:

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

如果您使用的是winforms datagridview它可以这样初始化:

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;
}

您的帖子是telerik.wincontrols.ui.radgridview,但初始化非常相似:

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;
}

“

希望这有助于您将自己想要的位置带到您想要的位置。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文