chopperfield
chopperfield

Reputation: 567

How to set column ticked from other column

How can I set a value into gridview column? The thing is the column was added manually, not from the database. What I want is when the checklist value was 1 the cek22 column got ticked and when value was 0 the cek22 column was unticked. I used devexpress.

Example of my code that I used:

public void abc()
{
    //select query in here
    gridControl1.DataSource = dt;

    //iam adding a column here
    dt.Columns.Add("cek22",typeof(bool));
}

enter image description here

Upvotes: 0

Views: 156

Answers (3)

nempoBu4
nempoBu4

Reputation: 6621

If your column is added manually then your column is working in unbound mode. So, you can just use its unbound expression. If you want to update your checklist column from cel22 then you can use CellValueChanging event.
Here is example:

var table = new DataTable();

table.Columns.AddRange(new[]
{
    new DataColumn("preferred", typeof(string)),
    new DataColumn("checklist", typeof(int))
});

table.Rows.Add("Director Fury", 1);
table.Rows.Add("Maria Hill", 0);

gridControl1.DataSource = table;
gridView1.PopulateColumns();

var column = new GridColumn();
column.FieldName = "cek22";
column.UnboundType = UnboundColumnType.Boolean;
column.UnboundExpression = "[checklist]";
column.Visible = true;

gridView1.Columns.Add(column);

gridView1.CellValueChanging += (sender, e) =>
{
    if (e.Column.FieldName == "cek22")
        gridView1.SetRowCellValue(e.RowHandle, "checklist", e.Value);
};

Here is the result: Result

Upvotes: 1

ivayle
ivayle

Reputation: 1070

Alternatively, add the column as part of the data source and populate it conditionally.

private void Form1_Load(object sender, EventArgs e)
{
    DataTable dataTable = GetDataTable(10);
    gridControl1.DataSource = dataTable;
}

private DataTable GetDataTable(int rows = 1)
{
    DataTable table = new DataTable("Table1");

    table.Columns.Add("checklist", typeof(int));
    table.Columns.Add("cek22", typeof(bool));

    for (int i = 0; i < rows; i++)
    {
        DataRow row = table.NewRow();
        row["checklist"] = i % 2 == 0 ? 0 : 1;
        row["cek22"] = ((int)row["checklist"]) == 0 ? false : true;

        table.Rows.Add(row);
    }

    return table;
}

enter image description here

Upvotes: 0

Sebi
Sebi

Reputation: 3979

The DevExpress Grid works with DataSource. So manipulate your DataSource and populate this to your Grid. The GridView will show this data. So if you want a check field i would recommend you to extend your DataSource with a bool Property. I don't now which DataSource you are using but if there is any bool value devexpress automatically add a checkbox column for you. If you want to link your custom column to the bool value in your DataSource you need to set the FieldName Property of the Column to your PropertyName.

If your checklist Property is 0 your bool Property return false and vice versa. This would be the easiest solution i guess. Assumed your are using a IList as DataSource.

Small example:

    public class MyDataSource()
    {
        public int Checklist { get; set; }

        public bool Cek22
        {
            get { return Checklist == 1; }
        }
    }

    private void ExplainADevExpressGrid()
    {
        List<MyDataSource> dataSource = new List<MyDataSource>();
        dataSource.Add(new MyDataSource());

        myGrid.DataSource = dataSource;
    }

Upvotes: 0

Related Questions