以编程方式访问网格控件中的按钮 DevExpress

发布于 2024-09-30 20:27:16 字数 65 浏览 1 评论 0原文

我想启用/禁用网格控件每一行中的按钮。我不确定如何通过代码访问它。我认为这将是 GridView1 方法......

I wanna enable/disable a button found in every row of my grid control. I am not sure how I can access it through code. I would think it would bein the GridView1 methods.....

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

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

发布评论

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

评论(1

圈圈圆圆圈圈 2024-10-07 20:27:16

您可以通过处理 ShowingEditor 事件来禁用该按钮(或者更好的是,禁用特定单元格的整个编辑)...然后您可以检查其他列的值,然后根据您的意愿取消该单元格的编辑。下面是示例程序的一些代码,向您展示了具体的操作方法:

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 DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid;

namespace GridButtonDisable
{

public partial class Form1 : Form
{
    public class MyData
    {
        public int Number { get; set; }
        public bool Even { get { return Number % 2 == 0; } }
    }

    public Form1()
    {
        InitializeComponent();

        List<MyData> List = new List<MyData>
        {
            new MyData() { Number = 1 },
            new MyData() { Number = 2 },
            new MyData() { Number = 5 },
            new MyData() { Number = 7 },
            new MyData() { Number = 10 },
        };

        gridControl1.DataSource = List;
        gridView1.ShowingEditor += gridView1_ShowingEditor;
    }

    private void gridView1_ShowingEditor(object sender, CancelEventArgs e)
    {
        GridView view = sender as GridView;
        if (view.FocusedColumn.Name == "gridButtonCol" && 
            !(bool)view.GetRowCellValue(view.FocusedRowHandle, "Even") )
            e.Cancel = true;
    }
}

这是一个非常简单的程序。您必须假设您的按钮编辑列名为 gridButtonCol...我正在测试为此示例创建的 MyData 类的 Even 列的值,您可以执行任何您想要的操作,并在此处检查您喜欢的任何条件。

如果您设置 ShowingEditor 事件的 CancelEventArgs 的 e.Cancel 属性,则单元格将不可编辑,按钮编辑器将不会响应用户单击...

You can disable the button (or better yet, the entire editing of a specif cell) by just handling the ShowingEditor event... You can then inspect the values of other columns and then cancel the editing of that cell depending on your wishes. Here goes some code for a sample program that shows you exactly how to do that:

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 DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid;

namespace GridButtonDisable
{

public partial class Form1 : Form
{
    public class MyData
    {
        public int Number { get; set; }
        public bool Even { get { return Number % 2 == 0; } }
    }

    public Form1()
    {
        InitializeComponent();

        List<MyData> List = new List<MyData>
        {
            new MyData() { Number = 1 },
            new MyData() { Number = 2 },
            new MyData() { Number = 5 },
            new MyData() { Number = 7 },
            new MyData() { Number = 10 },
        };

        gridControl1.DataSource = List;
        gridView1.ShowingEditor += gridView1_ShowingEditor;
    }

    private void gridView1_ShowingEditor(object sender, CancelEventArgs e)
    {
        GridView view = sender as GridView;
        if (view.FocusedColumn.Name == "gridButtonCol" && 
            !(bool)view.GetRowCellValue(view.FocusedRowHandle, "Even") )
            e.Cancel = true;
    }
}

This is a pretty simple program. You must assume your button edit column is named gridButtonCol... I'm testing the value of the Even column of the MyData class I've created for this sample, you can do whatever you want and check whichever condition you like here.

If you set the e.Cancel property of the CancelEventArgs of the ShowingEditor event, the cell won't be editable and the button editor won't respond to user clicks...

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