Infragistics 中的复选框列赢得 ultragrid

发布于 2024-10-28 03:14:47 字数 168 浏览 4 评论 0原文

我是基础设施的新手。 在我的 winforms 应用程序上,我使用 Ultrawingrid 来显示数据库中的数据。

如何将复选框列显示为网格中的第一列? 另外,我需要捕获选中/取消选中事件,然后读取应用程序中相应的网格行/单元格。

你能帮我解决这个问题吗?

感谢您的阅读。

Am a newbie to Infragistics.
On my winforms app, am using Ultrawingrid to display data from database.

How do I show a checkbox column as the first column in the grid?
Also, I need to capture check/uncheck event and then read the corresponding grid row/cells in the application.

Could you please help me on this?

Thanks for reading.

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

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

发布评论

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

评论(2

从此见与不见 2024-11-04 03:14:47

您需要获取要呈现为复选框的列的 UltraGridColumn 实例。类似于:

UltraGridColumn ugc = myGrid.DisplayLayout.Bands[0].Columns[@"myColumnKey"];

然后将列的显示样式更改为复选框并确保它允许编辑:

ugc.Style = ColumnStyle.CheckBox;
ugc.CellActivation = Activation.AllowEdit;

在我看来,将此网格初始化代码放在表单的 Load 事件或网格的 InitializeLayout 事件的处理程序中是合适的。

处理网格的 CellChange 事件以查看用户何时更改复选框值:

private void mygrid_CellChange(object sender, CellEventArgs e)
{
    if (StringComparer.OrdinalIgnoreCase.Equals(e.Cell.Column.Key, @"myColumnKey"))
    {
         // do something special when the checkbox value is changed
    }
}

根据要求,这里是示例代码,演示了添加未绑定列、将其移动到最左边的位置、处理单元格更改事件以及从网格中检索附加值。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=tempdb;Trusted_Connection=true"))
        {
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter("select * from sysobjects", conn);
            conn.Open();
            da.Fill(ds); 
            ultraGrid1.DataSource = ds;
        }
    }

    private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
    {
        UltraGridColumn checkColumn = e.Layout.Bands[0].Columns.Add(@"checkColumnKey", @"caption");
        checkColumn.Style = Infragistics.Win.UltraWinGrid.ColumnStyle.CheckBox;
        checkColumn.CellActivation = Activation.AllowEdit;
        checkColumn.Header.VisiblePosition = 0;
    }

    private void ultraGrid1_CellChange(object sender, CellEventArgs e)
    {
        if (!StringComparer.Ordinal.Equals(e.Cell.Column.Key, @"checkColumnKey"))
        {
            return;
        }

        bool checkedState = bool.Parse(e.Cell.Text);

        DataRowView row = e.Cell.Row.ListObject as DataRowView;
        string name = row.Row[@"name"] as string;

        MessageBox.Show(string.Format("Checked={0}, name={1}", checkedState, e.Cell.Row.ListObject));
    }
}

You need to get a hold of the UltraGridColumn instance for the column you want rendered as a checkbox. Something like:

UltraGridColumn ugc = myGrid.DisplayLayout.Bands[0].Columns[@"myColumnKey"];

Then change the column's display style to checkbox and make sure it allows edits:

ugc.Style = ColumnStyle.CheckBox;
ugc.CellActivation = Activation.AllowEdit;

In my opinion, it's appropriate to have this grid initialization code in a handler for the form's Load event or the grid's InitializeLayout event.

Handle the grid's CellChange event to see when the user changes the checkbox value:

private void mygrid_CellChange(object sender, CellEventArgs e)
{
    if (StringComparer.OrdinalIgnoreCase.Equals(e.Cell.Column.Key, @"myColumnKey"))
    {
         // do something special when the checkbox value is changed
    }
}

As requested, here is sample code that demonstrates adding an unbound column, moving it to the leftmost position, handling the cell change event, and retrieving an additional value from the grid.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=tempdb;Trusted_Connection=true"))
        {
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter("select * from sysobjects", conn);
            conn.Open();
            da.Fill(ds); 
            ultraGrid1.DataSource = ds;
        }
    }

    private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
    {
        UltraGridColumn checkColumn = e.Layout.Bands[0].Columns.Add(@"checkColumnKey", @"caption");
        checkColumn.Style = Infragistics.Win.UltraWinGrid.ColumnStyle.CheckBox;
        checkColumn.CellActivation = Activation.AllowEdit;
        checkColumn.Header.VisiblePosition = 0;
    }

    private void ultraGrid1_CellChange(object sender, CellEventArgs e)
    {
        if (!StringComparer.Ordinal.Equals(e.Cell.Column.Key, @"checkColumnKey"))
        {
            return;
        }

        bool checkedState = bool.Parse(e.Cell.Text);

        DataRowView row = e.Cell.Row.ListObject as DataRowView;
        string name = row.Row[@"name"] as string;

        MessageBox.Show(string.Format("Checked={0}, name={1}", checkedState, e.Cell.Row.ListObject));
    }
}
风为裳 2024-11-04 03:14:47

为什么不确保你的数据层返回 Bool,Infragistics 网格会自动(自动生成)它的复选框

why not make sure your data layer returns Bool, Infragistics grid will automatically (auto generate) Check Box for it

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