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/unche开发者_开发技巧ck event and then read the corresponding grid row/cells in the application.
Could you please help me on this?
Thanks for reading.
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));
}
}
why not make sure your data layer returns Bool, Infragistics grid will automatically (auto generate) Check Box for it
精彩评论