Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkbox column in Infragistics win ultragrid

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.

like image 420
Ed. Avatar asked Mar 28 '11 03:03

Ed.


People also ask

How do I add a column in UltraGrid?

It is possible to add groups and columns to an UltraGrid dynamically using ABL. But the columns to be added must already be defined in the proBindingSource object. In the ABL after creating the grid band, use the 'Add' method of the columns and groups collection to add the groups and columns.

How do I hide columns in infragistics?

To hide a column at design time:Switch to the Groups and Columns tab. Select the Column or Group you want to hide from the tree list. Click the Show/Hide button. Set the Hidden property.


2 Answers

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));
    }
}
like image 140
PaulF Avatar answered Sep 21 '22 09:09

PaulF


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

like image 45
Bek Raupov Avatar answered Sep 21 '22 09:09

Bek Raupov