Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default value for DataGridViewCheckBoxComboBoxColumn #1

Open
ccko opened this issue Feb 8, 2018 · 0 comments
Open

Default value for DataGridViewCheckBoxComboBoxColumn #1

ccko opened this issue Feb 8, 2018 · 0 comments

Comments

@ccko
Copy link

ccko commented Feb 8, 2018

Hi ,

I want set default cell value in grid.
but part of cell's value is empty, please see below code
thanks.

` private ListSelectionWrapper wrappedList;
public Form1()
{
InitializeComponent();

        var dt = new DataTable();
        dt.Columns.Add("Text", typeof(string));


        var dataSource = new List<Data>();
        dataSource.Add(new Data() { Text = "Sunday, Monday" });
        dataSource.Add(new Data() { Text = "Tuesday" });
        dataSource.Add(new Data() { Text = "Thursday" });


        foreach (var obj in dataSource)
        {
            var row = dt.NewRow();

            row.ItemArray = new object[] { obj.Text };

            dt.Rows.Add(row);
        }

        //var bindingList = new BindingList<Data>(dataSource);
        //var source = new BindingSource(bindingList, null);

        dataGridView1.AutoGenerateColumns = true;
        var txtCol = new DataGridViewTextBoxColumn();
        txtCol.Name = "Text";
        txtCol.HeaderText = "Text";
        txtCol.ReadOnly = true;
        txtCol.DataPropertyName = "Text";
        dataGridView1.Columns.Add(txtCol);

        List<Status> statuses = new List<Status>();
        statuses.Add(new Status() { Name = "Sunday" });
        statuses.Add(new Status() { Name = "Monday" });
        statuses.Add(new Status() { Name = "Tuesday" });
        statuses.Add(new Status() { Name = "Wednesday" });
        statuses.Add(new Status() { Name = "Thursday" });
        statuses.Add(new Status() { Name = "Friday" });
        statuses.Add(new Status() { Name = "Saturday" });
        var comboboxColumn = new DataGridViewCheckBoxComboBoxColumn();

        wrappedList = new ListSelectionWrapper<Object>(statuses);
        wrappedList.TextSeparator = comboboxColumn.TextSeparator;

        comboboxColumn.DataSource = wrappedList;
        comboboxColumn.ValueMember = "Selected";
        comboboxColumn.DisplayMemberSingleItem = "Name";
        comboboxColumn.DisplayMember = "NameConcatenated";
        comboboxColumn.Name = "Status";
        dataGridView1.Columns.Add(comboboxColumn);

        
        dataGridView1.DataSource = dt;

    }

    private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
    {
        var grid = (DataGridView)sender;
        try
        {
            var cell = grid.Rows[e.RowIndex].Cells["Text"];
            var cellValue = Convert.ToString(cell.Value);
            //var cellValue = Convert.ToString((dataGridView1.DataSource as DataTable).Rows[e.RowIndex]["Text"]);

            if (!string.IsNullOrEmpty(cellValue))
            {
                var rss = (from name in cellValue.Split(',')
                           select new KeyValuePair<string, object>(
                               name,
                               new ObjectSelectionWrapper<Object>(new Status() { Name = name.Trim() }, this.wrappedList))
                               ).ToDictionary(t => t.Key, t => t.Value);

                var testCell = dataGridView1.Rows[e.RowIndex].Cells["Status"] as
                     PresentationControls.DataGridViewCheckBoxComboBoxColumn.DataGridViewCheckBoxComboBoxCell;

                testCell.Value = rss;
            }
        }
        catch
        {

        }
    }`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant