8

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.

Ed.
  • 1,654
  • 7
  • 20
  • 33

2 Answers2

17

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));
    }
}
PaulF
  • 1,133
  • 8
  • 14
  • Thanks a ton PaulF, but I need to add this checkbox column as an unbound one.And I need to show it as the first column in the grid ahead of the all the db columns.Now it is showing up as the last column.Also, when I click on the checkbox, it is not getting checked even after setting CellActivation to AllowEdit.Please help. – Jimmy Mar 29 '11 at 05:25
  • Hi Jimmy . Thanks for the amazing answer . I had a question as an extension. For sake of convenience , I am adding my columns with data types in a DataTable. And am adding the dataTable to ultragrid. Anyway I can configure the CellChange event for the table ? – Ace McCloud Apr 22 '15 at 20:50
  • @PrasaanthNeelakandan I think this should be possible with Infragistics. I don't typically use DataTables, but I would try using the designer to add a CellChange event to your grid. Then set a breakpoint in the handler and have a look at CellEventArgs, in particular e.Cell.Column and e.Cell.Column.Key. This might get you started. – PaulF Apr 23 '15 at 10:22
2

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

Bek Raupov
  • 3,782
  • 3
  • 24
  • 42
  • I am having the same issue. Adding it to your data layer might be wrong because some fields are a UI concern only and you should always be cautious not to mix your UI and Data layer code too much. – Guillaume Schuermans Jun 10 '13 at 14:00