how to put checkboxes in datagrid in windows mobile 6 using c#?

╄→尐↘猪︶ㄣ 提交于 2019-11-27 14:12:56

Here's some code from and old blog by Eric Hartwell (pulled into SO using the wayback machine):

    private void SetupTableStyles()
    {
       Color alternatingColor = SystemColors.ControlDark;
       DataTable vehicle = dataSource.Tables[1];

       // ID Column 
       DataGridCustomTextBoxColumn dataGridCustomColumn0 = new DataGridCustomTextBoxColumn();
       dataGridCustomColumn0.Owner = this.dataGrid1;
       dataGridCustomColumn0.Format = "0##";
       dataGridCustomColumn0.FormatInfo = null;
       dataGridCustomColumn0.HeaderText = vehicle.Columns[0].ColumnName;
       dataGridCustomColumn0.MappingName = vehicle.Columns[0].ColumnName;
       dataGridCustomColumn0.Width = dataGrid1.Width * 10 / 100;    // 10% of grid size
       dataGridCustomColumn0.AlternatingBackColor = alternatingColor;
       dataGridCustomColumn0.ReadOnly = true;
       dataGridTableStyle1.GridColumnStyles.Add(dataGridCustomColumn0);

       // Make column
       DataGridCustomTextBoxColumn dataGridCustomColumn1 = new DataGridCustomTextBoxColumn();
       dataGridCustomColumn1.Owner = this.dataGrid1;
       dataGridCustomColumn1.HeaderText = vehicle.Columns[1].ColumnName;
       dataGridCustomColumn1.MappingName = vehicle.Columns[1].ColumnName;
       dataGridCustomColumn1.NullText = "-Probably Ford-";
       dataGridCustomColumn1.Width = dataGrid1.Width * 40 / 100;     // 40% of grid size
       dataGridCustomColumn1.Alignment = HorizontalAlignment.Right;
       dataGridCustomColumn1.AlternatingBackColor = alternatingColor;
       dataGridTableStyle1.GridColumnStyles.Add(dataGridCustomColumn1);

       // Mileage column
       DataGridCustomUpDownColumn dataGridCustomColumn2 = new DataGridCustomUpDownColumn();
       dataGridCustomColumn2.Owner = this.dataGrid1;
       dataGridCustomColumn2.HeaderText = vehicle.Columns[2].ColumnName;
       dataGridCustomColumn2.MappingName = vehicle.Columns[2].ColumnName;
       dataGridCustomColumn2.NullText = "-Unknown-";
       dataGridCustomColumn2.Width = dataGrid1.Width * 20 / 100;     // 20% of grid size
       dataGridCustomColumn2.Alignment = HorizontalAlignment.Left;
       dataGridCustomColumn2.AlternatingBackColor = alternatingColor;
       dataGridTableStyle1.GridColumnStyles.Add(dataGridCustomColumn2);

       // Availability column 
       DataGridCustomCheckBoxColumn dataGridCustomColumn3 = new DataGridCustomCheckBoxColumn();
       dataGridCustomColumn3.Owner = this.dataGrid1;
       dataGridCustomColumn3.HeaderText = vehicle.Columns[3].ColumnName;
       dataGridCustomColumn3.MappingName = vehicle.Columns[3].ColumnName;
       dataGridCustomColumn3.NullText = "-Unknown-";
       dataGridCustomColumn3.Width = dataGrid1.Width * 10 / 100;     // 10% of grid size
       dataGridCustomColumn3.Alignment = HorizontalAlignment.Left;
       dataGridCustomColumn3.AlternatingBackColor = alternatingColor;
       dataGridTableStyle1.GridColumnStyles.Add(dataGridCustomColumn3);

       // Fuel Level column
       DataGridCustomComboBoxColumn dataGridCustomColumn4 = new DataGridCustomComboBoxColumn();
       dataGridCustomColumn4.Owner = this.dataGrid1;
       dataGridCustomColumn4.HeaderText = vehicle.Columns[4].ColumnName;
       dataGridCustomColumn4.MappingName = vehicle.Columns[4].ColumnName;
       dataGridCustomColumn4.NullText = "-Unknown-";
       dataGridCustomColumn4.Width = dataGrid1.Width * 30 / 100;     // 30% of grid size
       dataGridCustomColumn4.Alignment = HorizontalAlignment.Left;
       dataGridCustomColumn4.AlternatingBackColor = alternatingColor;
       dataGridTableStyle1.GridColumnStyles.Add(dataGridCustomColumn4);

       // Last Used column
       DataGridCustomDateTimePickerColumn dataGridCustomColumn5 = new DataGridCustomDateTimePickerColumn();
       dataGridCustomColumn5.Owner = this.dataGrid1;
       dataGridCustomColumn5.HeaderText = vehicle.Columns[5].ColumnName;
       dataGridCustomColumn5.MappingName = vehicle.Columns[5].ColumnName;
       dataGridCustomColumn5.NullText = "-Unknown-";
       dataGridCustomColumn5.Width = dataGrid1.Width * 30 / 100;     // 30% of grid size
       dataGridCustomColumn5.Alignment = HorizontalAlignment.Left;
       dataGridCustomColumn5.AlternatingBackColor = alternatingColor;
       dataGridTableStyle1.GridColumnStyles.Add(dataGridCustomColumn5);

       // Grid, mapping
       dataGridTableStyle1.MappingName = vehicle.TableName;                       // Setup table mapping name
       dataGrid1.DataSource = vehicle;                                  

       // Setup grid's data source
       ComboBox cb = (ComboBox)dataGridCustomColumn4.HostedControl;
       DataTable fuel = dataSource.Tables[0];                                                    // Set up data source
       cb.DataSource = fuel;                                            

       // For combo box column
       cb.DisplayMember = fuel.Columns[0].ColumnName;
       cb.ValueMember = fuel.Columns[0].ColumnName;

       dataGrid1.CurrentRowIndex = 50;                                                            // Move to the middle of the table
    }

For better checkbox UI look and feel, rather than big cross

private void DrawCheckBox(Graphics g, Rectangle bounds, CheckState state)
        {
            int size;
            int boxTop;

            size = bounds.Size.Height < bounds.Size.Width ? bounds.Size.Height : bounds.Size.Width;
            size = size > ((int)g.DpiX / 7) ? ((int)g.DpiX / 7) : size;

            boxTop = bounds.Y + (bounds.Height - size) / 2;
            size = 12; // 13, so I made it 12
            boxTop = boxTop - 1;
            using (Pen p = new Pen(this.Owner.ForeColor))
            {
                g.DrawRectangle(p, bounds.X, boxTop, size, size);
            }

            if (state != CheckState.Unchecked)
            {
                using (Pen p = new Pen(state == CheckState.Indeterminate ? SystemColors.GrayText : SystemColors.ControlText))
                {
                    p.Width = 2;
                    int offset = 2;
                    int edgeOffset = 2;
                    g.DrawLine(p, bounds.X + offset, boxTop + offset + 2, bounds.X + (size / 2) - edgeOffset, boxTop + (size / 2) + edgeOffset);
                    g.DrawLine(p, bounds.X + (size / 2) - edgeOffset, boxTop + (size / 2) + edgeOffset, bounds.X + size - offset, boxTop + offset);
                }
            }
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!