开发者

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

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

dataset dsAgent=table; DataTable dataTable = dsAgent.Tables[0];

            DataGridTableStyle tableStyle = new DataGridTableStyle();

            tableStyle.MappingName = dataTable.TableName;

            GridColumnStylesCollection columnStyles = tableStyle.GridColumnStyles;

            DataGridTextBoxColumn columnStyle = new DataGridTextBoxColumn();
            columnStyle.MappingName = "FirstName";
            columnStyle.HeaderText = "Name";
            columnStyle.Width = 80;
            columnStyles.Add(columnStyle);

            //columnStyle = new DataGridTextBoxColumn();
            //columnStyle.MappingName = "EmailAddress";
            //columnStyle.HeaderText = "EmailID";
            //columnStyle.Width = 150;
            //columnStyles.Add(columnStyle);

            columnStyle = new DataGridTextBoxColumn();
            columnStyle.MappingName = "WorkPhone";
            columnStyle.HeaderText = "PhoneNo";
            columnStyle.Width = 150;
            columnStyles.Add(columnStyle);


            GridTableStylesCollection tableStyles = DataGrid.TableStyles;
            tableStyles.Add(tabl开发者_JS百科eStyle);


            DataGrid.PreferredRowHeight = 16;
            DataGrid.RowHeadersVisible = false;
            DataGrid.DataSource = dataTable;


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);
                }
            }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜