I am trying to add a new checkbox column to the ultrawingrid that is binding to a dataset, when ever I add a new column it says key not found, any ideas on how to fix it, Thank you...
Below is the code
private void grdPayVis_InitializeLayout(object sender, InitializeLayoutEventArgs e)
{
var gridBand = grdPayVis.DisplayLayout.Bands[0];
gridBand.Columns["Select"].Header.Caption = "Select";
gridBand.Columns["Select"].Header.Appearance.TextHAlign = HAlign.Center;
gridBand.Columns["Select"].Header.VisiblePosition = 0;
gridBand.Columns["Select"].Hidden = false;
gridBand.Columns["Select"].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.CheckBox;
gridBand.Columns["Select"].AutoSizeMode = ColumnAutoSizeMode.AllRowsInBand;
gridBand.Columns["Select"].CellActivation = Activation.AllowEdit;
gridBand.Columns["Select"].CellAppearance.TextHAlign = HAlign.Center;
gridBand.Columns["Select"].CellClickAction = CellClickAction.Edit;
}
Swetha
When the grid has its datasource set to a datatable or other binding source, it automatically creates the columns present in the datatable or in the properties of the datasource. If you want to have another column you need to ADD it before trying to reference it from the Band columns
private void grdPayVis_InitializeLayout(object sender, InitializeLayoutEventArgs e)
{
var gridBand = grdPayVis.DisplayLayout.Bands[0];
// Check if the column exists, if not, add it
if(!gridBand.Columns.Exists("Select"))
gridBand.Columns.Add("Select", "Select");
// Not needed, the ADD adds the Key and the Caption
// gridBand.Columns["Select"].Header.Caption = "Select";
// Now you can reference the column with the Key = "Select"
gridBand.Columns["Select"].Header.VisiblePosition = 0;
gridBand.Columns["Select"].Hidden = false;
gridBand.Columns["Select"].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.CheckBox;
gridBand.Columns["Select"].AutoSizeMode = ColumnAutoSizeMode.AllRowsInBand;
gridBand.Columns["Select"].CellClickAction = CellClickAction.Edit;
}
Steve's answer above is good, but it can be simplified and made better in a couple of ways.
First, a lot of that code isn't really necessary. For example, the default Hidden state of a column is false, so there's no need to set that. Same for AutoSizeMode and CellClickAction.
Also, if you add an unbound column, it's default DataType is string, which doesn't make a lot of sense for a CheckBox column. By setting the DataType to bool, you can avoid the need to set the Style and also clear up the problem of every cell being indeterminate by default.
private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
{
UltraGridLayout layout = e.Layout;
UltraGridBand rootBand = layout.Bands[0];
if (false == rootBand.Columns.Exists("Select"))
{
UltraGridColumn checkBoxColumn = rootBand.Columns.Add("Select");
checkBoxColumn.Header.VisiblePosition = 0;
checkBoxColumn.DataType = typeof(bool);
}
}
As for determining the value of the cell and keeping track of the 'selected' rows, you have to understand a little bit about how the grid cell editors work. To really understand it, imagine a cell that contains DateTime info. The user enters the cell and intends to type in a date, like '1/19/2015'. When the user begins typing, he starts by typing a '1'. If you check the cell's value at this point, the grid cannot possibly convert the current text in the cell ("1") into a date. So because of this, the grid doesn't attempt to update the underlying data source with the value until something else happens, like if the user leaves the cell or loses focus on the grid.
Of course, if the cell has a checkbox, then this problem doesn't exist, since the user is incapable of entering an invalid value, but the grid still works the same way and doesn't update the value until the user leaves the cell.
So... when you are dealing with any cell that is NOT in edit mode (not currently active) then you can use the Value property of the cell reliably. When the cell is in edit mode (is active) then you can't rely on the Value, which reads from the data source, you have to use the cell's Text.
Therefore, given any cell in a boolean (checkbox) column, to get an accurate reflection of the current state of the checkbox on the screen, you would do something like this:
private bool GetCheckBoxCellCurrentValue(UltraGridCell cell)
{
if (cell.IsInEditMode)
return bool.Parse(cell.Text);
else
return (bool)cell.Value;
}
Finally, the grid doesn't keep any kind of list of the 'checked' cells. But you could do this yourself without too much difficulty. What you would have to do is build the initial list right after you bind the grid. Then trap events such as CellChange, AfterRowAdded, BeforeRowDeleted, and perhaps some others to continuously keep your list up to date.
来源:https://stackoverflow.com/questions/33485087/adding-a-checkbox-column-to-the-infragistics-ultrawingrid-that-is-bind-to-a-data