GridColumnStylesCollection colStyle = datagrid名.TableStyles[0].GridColumnStyles;
//在第一列加入一个复选框
DataGridTextBoxColumn dgMaterial    = (DataGridTextBoxColumn)colStyle[1]; ComboBox cb_Check;
cb_Check.Dock      = DockStyle.Right;
cb_Check.BackColor = Control.DefaultBackColor;
cb_Check.Width     = 25;
cb_Check.SelectedIndexChanged += new EventHandler(cb_Check_IndexChange);dgMaterial.TextBox.Controls.Clear();
dgMaterial.TextBox.Controls.Add(cb_Check);这样把控件加进去后,显示的不是控件而是true或false值,不知道大家遇到过同样的问题没有,请大家帮忙解决!

解决方案 »

  1.   

    下面的示例创建一个承载 DateTimePicker 控件的 DataGridColumnStyle。
    [Visual Basic] 
    Imports System
    Imports System.Data
    Imports System.Windows.Forms
    Imports System.Drawing
    Imports System.ComponentModel' This example shows how to create your own column style that
    ' hosts a control, in this case, a DateTimePicker.
    Public Class DataGridTimePickerColumn
        Inherits DataGridColumnStyle
        Private timePicker As New DateTimePicker()
        ' The isEditing field tracks whether or not the user is
        ' editing data with the hosted control.
        Private isEditing As Boolean    Public Sub New()
            timePicker.Visible = False
        End Sub    Protected Overrides Sub Abort(ByVal rowNum As Integer)
            isEditing = False
            RemoveHandler timePicker.ValueChanged, _
            AddressOf TimePickerValueChanged
            Invalidate()
        End Sub    Protected Overrides Function Commit _
        (ByVal dataSource As CurrencyManager, ByVal rowNum As Integer) _
        As Boolean
            timePicker.Bounds = Rectangle.Empty        AddHandler timePicker.ValueChanged, _
            AddressOf TimePickerValueChanged        If Not isEditing Then
                Return True
            End If
            isEditing = False        Try
                Dim value As DateTime = timePicker.Value
                SetColumnValueAtRow(dataSource, rowNum, value)
            Catch
            End Try        Invalidate()
            Return True
        End Function    Protected Overloads Overrides Sub Edit( _
        ByVal [source] As CurrencyManager, _
        ByVal rowNum As Integer, _
        ByVal bounds As Rectangle, _
        ByVal [readOnly] As Boolean, _
        ByVal instantText As String, _
        ByVal cellIsVisible As Boolean)
            Dim value As DateTime = _
            CType(GetColumnValueAtRow([source], rowNum), DateTime)
            If cellIsVisible Then
                timePicker.Bounds = New Rectangle _
                (bounds.X + 2, bounds.Y + 2, bounds.Width - 4, _
                bounds.Height - 4)            timePicker.Value = value
                timePicker.Visible = True
                AddHandler timePicker.ValueChanged, _
                AddressOf TimePickerValueChanged
            Else
                timePicker.Value = value
                timePicker.Visible = False
            End If        If timePicker.Visible Then
                DataGridTableStyle.DataGrid.Invalidate(bounds)
            End If
        End Sub    Protected Overrides Function GetPreferredSize( _
        ByVal g As Graphics, _
        ByVal value As Object) As Size
            Return New Size(100, timePicker.PreferredHeight + 4)
        End Function    Protected Overrides Function GetMinimumHeight() As Integer
            Return timePicker.PreferredHeight + 4
        End Function
        Protected Overrides Function GetPreferredHeight(ByVal g As Graphics, ByVal value As Object) As Integer
            Return timePicker.PreferredHeight + 4
        End Function
        Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal [source] As CurrencyManager, ByVal rowNum As Integer)
            Paint(g, bounds, [source], rowNum, False)
        End Sub    Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal [source] As CurrencyManager, ByVal rowNum As Integer, ByVal alignToRight As Boolean)
            Paint(g, bounds, [source], rowNum, Brushes.Red, Brushes.Blue, alignToRight)
        End Sub    Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal [source] As CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As Brush, ByVal foreBrush As Brush, ByVal alignToRight As Boolean)
            Dim [date] As DateTime = CType(GetColumnValueAtRow([source], rowNum), DateTime)
            Dim rect As Rectangle = bounds
            g.FillRectangle(backBrush, rect)
            rect.Offset(0, 2)
            rect.Height -= 2
            g.DrawString([date].ToString("d"), Me.DataGridTableStyle.DataGrid.Font, foreBrush, RectangleF.FromLTRB(rect.X, rect.Y, rect.Right, rect.Bottom))
        End Sub    Protected Overrides Sub SetDataGridInColumn(ByVal value As DataGrid)
            MyBase.SetDataGridInColumn(value)
            If Not (timePicker.Parent Is Nothing) Then
                timePicker.Parent.Controls.Remove(timePicker)
            End If
            If Not (value Is Nothing) Then
                value.Controls.Add(timePicker)
            End If
        End Sub    Private Sub TimePickerValueChanged(ByVal sender As Object, ByVal e As EventArgs)
            Me.isEditing = True
            MyBase.ColumnStartedEditing(timePicker)
        End Sub
    End ClassNamespace DataGridColumnStyleExample
        Public Class MyForm
            Inherits Form        Private namesDataTable As dataTable
            Private myGrid As DataGrid = New DataGrid()
            Public Sub New()            InitForm()            namesDataTable = New DataTable("NamesTable")
                namesDataTable.Columns.Add(New DataColumn("Name"))
                Dim dateColumn As DataColumn = _
                New DataColumn("Date", GetType(DateTime))
                namesDataTable.Columns.Add(dateColumn)
                Dim namesDataSet As DataSet = New DataSet()
                namesDataSet.Tables.Add(namesDataTable)
                myGrid.DataSource = namesDataSet
                myGrid.DataMember = "NamesTable"
                AddGridStyle()
                AddData()
            End Sub        Private Sub AddGridStyle()
                Dim myGridStyle As DataGridTableStyle = _
                            New DataGridTableStyle()
                myGridStyle.MappingName = "NamesTable"            Dim nameColumnStyle As DataGridTextBoxColumn = _
                    New DataGridTextBoxColumn()
                nameColumnStyle.MappingName = "Name"
                nameColumnStyle.HeaderText = "Name"
                myGridStyle.GridColumnStyles.Add(nameColumnStyle)            Dim timePickerColumnStyle As DataGridTimePickerColumn = _
                    New DataGridTimePickerColumn()
                timePickerColumnStyle.MappingName = "Date"
                timePickerColumnStyle.HeaderText = "Date"
                timePickerColumnStyle.Width = 100
                myGridStyle.GridColumnStyles.Add(timePickerColumnStyle)            myGrid.TableStyles.Add(myGridStyle)
            End Sub        Private Sub AddData()
                Dim dRow As DataRow = namesDataTable.NewRow()
                dRow("Name") = "Name 1"
                dRow("Date") = New DateTime(2001, 12, 1)
                namesDataTable.Rows.Add(dRow)            dRow = namesDataTable.NewRow()
                dRow("Name") = "Name 2"
                dRow("Date") = New DateTime(2001, 12, 4)
                namesDataTable.Rows.Add(dRow)            dRow = namesDataTable.NewRow()
                dRow("Name") = "Name 3"
                dRow("Date") = New DateTime(2001, 12, 29)
                namesDataTable.Rows.Add(dRow)            dRow = namesDataTable.NewRow()
                dRow("Name") = "Name 4"
                dRow("Date") = New DateTime(2001, 12, 13)
                namesDataTable.Rows.Add(dRow)            dRow = namesDataTable.NewRow()
                dRow("Name") = "Name 5"
                dRow("Date") = New DateTime(2001, 12, 21)
                namesDataTable.Rows.Add(dRow)            namesDataTable.AcceptChanges()
            End Sub        Private Sub InitForm()            Me.Size = New Size(500, 500)
                myGrid.Size = New Size(350, 250)
                myGrid.TabStop = True
                myGrid.TabIndex = 1
                Me.StartPosition = FormStartPosition.CenterScreen
                Me.Controls.Add(myGrid)
            End Sub
      

  2.   

    <STAThread()> _
            Public Shared Sub Main()
                Application.Run(New MyForm())
            End Sub
        End Class
    End Namespace
    [C#] 
    using System;
    using System.Data;
    using System.Windows.Forms;
    using System.Drawing;// This example shows how to create your own column style that
    // hosts a control, in this case, a DateTimePicker.
    public class DataGridTimePickerColumn : DataGridColumnStyle 
    {
        private DateTimePicker myDateTimePicker = new DateTimePicker();
        // The isEditing field tracks whether or not the user is
        // editing data with the hosted control.
        private bool isEditing;    public DataGridTimePickerColumn() : base() 
        {
            myDateTimePicker.Visible = false;
        }    protected override void Abort(int rowNum)
        {
            isEditing = false;
            myDateTimePicker.ValueChanged -= 
                new EventHandler(TimePickerValueChanged);
            Invalidate();
        }    protected override bool Commit
            (CurrencyManager dataSource, int rowNum) 
        {
            myDateTimePicker.Bounds = Rectangle.Empty;
             
            myDateTimePicker.ValueChanged -= 
                new EventHandler(TimePickerValueChanged);        if (!isEditing)
                return true;        isEditing = false;        try 
            {
                DateTime value = myDateTimePicker.Value;
                SetColumnValueAtRow(dataSource, rowNum, value);
            } 
            catch (Exception) 
            {
                Abort(rowNum);
                return false;
            }        Invalidate();
            return true;
        }    protected override void Edit(
            CurrencyManager source, 
            int rowNum,
            Rectangle bounds, 
            bool readOnly,
            string instantText, 
            bool cellIsVisible) 
        {
            DateTime value = (DateTime) 
                GetColumnValueAtRow(source, rowNum);
            if (cellIsVisible) 
            {
                myDateTimePicker.Bounds = new Rectangle
                    (bounds.X + 2, bounds.Y + 2, 
                    bounds.Width - 4, bounds.Height - 4);
                myDateTimePicker.Value = value;
                myDateTimePicker.Visible = true;
                myDateTimePicker.ValueChanged += 
                    new EventHandler(TimePickerValueChanged);
            } 
            else 
            {
                myDateTimePicker.Value = value;
                myDateTimePicker.Visible = false;
            }        if (myDateTimePicker.Visible)
                DataGridTableStyle.DataGrid.Invalidate(bounds);
        }    protected override Size GetPreferredSize(
            Graphics g, 
            object value) 
        {
            return new Size(100, myDateTimePicker.PreferredHeight + 4);
        }    protected override int GetMinimumHeight() 
        {
            return myDateTimePicker.PreferredHeight + 4;
        }    protected override int GetPreferredHeight(Graphics g, 
            object value) 
        {
            return myDateTimePicker.PreferredHeight + 4;
        }    protected override void Paint(Graphics g, 
            Rectangle bounds, 
            CurrencyManager source, 
            int rowNum) 
        {
            Paint(g, bounds, source, rowNum, false);
        }
        protected override void Paint(
            Graphics g, 
            Rectangle bounds,
            CurrencyManager source, 
            int rowNum,
            bool alignToRight) 
        {
            Paint(
                g,bounds, 
                source, 
                rowNum, 
                Brushes.Red, 
                Brushes.Blue, 
                alignToRight);
        }
        protected override void Paint(
            Graphics g, 
            Rectangle bounds,
            CurrencyManager source, 
            int rowNum,
            Brush backBrush, 
            Brush foreBrush,
            bool alignToRight) 
        {
            DateTime date = (DateTime) 
                GetColumnValueAtRow(source, rowNum);
            Rectangle rect = bounds;
            g.FillRectangle(backBrush,rect);
            rect.Offset(0, 2);
            rect.Height -= 2;
            g.DrawString(date.ToString("d"), 
                this.DataGridTableStyle.DataGrid.Font, 
                foreBrush, rect);
        }    protected override void SetDataGridInColumn(DataGrid value) 
        {
            base.SetDataGridInColumn(value);
            if (myDateTimePicker.Parent != null) 
            {
                myDateTimePicker.Parent.Controls.Remove 
                    (myDateTimePicker);
            }
            if (value != null) 
            {
                value.Controls.Add(myDateTimePicker);
            }
        }    private void TimePickerValueChanged(object sender, EventArgs e) 
        {
            this.isEditing = true;
            base.ColumnStartedEditing(myDateTimePicker);
        }
    }
    namespace DataGridColumnStyleExample 
    {
        using System;
        using System.Data;
        using System.Windows.Forms;
        using System.Drawing;
        using System.ComponentModel;
        public class MyForm : Form 
        {
            private DataTable namesDataTable;
            private DataGrid grid = new DataGrid();
            public MyForm() : base() 
            {
                InitForm();            namesDataTable = new DataTable("NamesTable");
                namesDataTable.Columns.Add(new DataColumn("Name"));
                DataColumn dateColumn = new DataColumn 
                    ("Date", typeof(DateTime));
                namesDataTable.Columns.Add(dateColumn);
                DataSet namesDataSet = new DataSet();
                namesDataSet.Tables.Add(namesDataTable);
                grid.DataSource = namesDataSet;
                grid.DataMember = "NamesTable";
                AddGridStyle();
                AddData();
            }        private void AddGridStyle()
            {
                DataGridTableStyle myGridStyle = new DataGridTableStyle();
                myGridStyle.MappingName = "NamesTable";            DataGridTextBoxColumn nameColumnStyle = 
                    new DataGridTextBoxColumn();
                nameColumnStyle.MappingName = "Name";
                nameColumnStyle.HeaderText= "Name";
                myGridStyle.GridColumnStyles.Add(nameColumnStyle);            DataGridTimePickerColumn timePickerColumnStyle = 
                    new DataGridTimePickerColumn();
                timePickerColumnStyle.MappingName = "Date";
                timePickerColumnStyle.HeaderText = "Date";
                timePickerColumnStyle.Width = 100;
                myGridStyle.GridColumnStyles.Add(timePickerColumnStyle);            grid.TableStyles.Add(myGridStyle);
            }        private void AddData() 
            {
            
                DataRow dRow = namesDataTable.NewRow();
                dRow["Name"] = "Name 1";
                dRow["Date"] = new DateTime(2001, 12, 01);
                namesDataTable.Rows.Add(dRow);            dRow = namesDataTable.NewRow();
                dRow["Name"] = "Name 2";
                dRow["Date"] = new DateTime(2001, 12, 04);
                namesDataTable.Rows.Add(dRow);            dRow = namesDataTable.NewRow();
                dRow["Name"] = "Name 3";
                dRow["Date"] = new DateTime(2001, 12, 29);
                namesDataTable.Rows.Add(dRow);            dRow = namesDataTable.NewRow();
                dRow["Name"] = "Name 4";
                dRow["Date"] = new DateTime(2001, 12, 13);
                namesDataTable.Rows.Add(dRow);            dRow = namesDataTable.NewRow();
                dRow["Name"] = "Name 5";
                dRow["Date"] = new DateTime(2001, 12, 21);
                namesDataTable.Rows.Add(dRow);            namesDataTable.AcceptChanges();
            }        private void InitForm() 
            {
                this.Size = new Size(500, 500);
                grid.Size = new Size(350, 250);
                grid.TabStop = true;
                grid.TabIndex = 1;
                this.StartPosition = FormStartPosition.CenterScreen;
                this.Controls.Add(grid);
            }
            [STAThread]
            public static void Main() 
            {
                MyForm myForm1= new MyForm();
                myForm1.ShowDialog();
            }
        }
    }
      

  3.   

    谢谢Samen168(卖女孩的小火柴),可是这也太复杂了,有没有其他的比较简单的解决办法呢?
    怎么没有人帮助呢   晕啊
      

  4.   

    1、可以在表中加一个bool类型的字段
    2、可以增加一个表,只有一行一列,bool类型的,然后和你现在的表联合查询就可以,
       select * form a,b...
      

  5.   

    // code assumes you have a DataSet named myDataSet, a table named "EastCoastSales" and a DataGrid myDataGrid  
    //STEP 1: Create a DataTable style object and set properties if required. 
         DataGridTableStyle ts1 = new DataGridTableStyle();  
         //specify the table from dataset (required step) 
         ts1.MappingName = "EastCoastSales";  
         // Set other properties (optional step) 
         ts1.AlternatingBackColor = Color.LightBlue;  
    //STEP 2: Create a string column and add it to the tablestyle 
         DataGridColumnStyle TextCol = new DataGridTextBoxColumn();  
         TextCol.MappingName = "custName"; //from dataset table  
         TextCol.HeaderText = "Customer Name";  
         TextCol.Width = 250;  
         ts1.GridColumnStyles.Add(TextCol);  
    //STEP 3: Create an int column style and add it to the tablestyle  
         //this requires setting the format for the column through its property descriptor  
         PropertyDescriptorCollection pdc = this.BindingContext  
               [myDataSet, "EastCoastSales"].GetItemProperties();  
         //now created a formated column using the pdc  
         DataGridDigitsTextBoxColumn csIDInt =  
               new DataGridDigitsTextBoxColumn(pdc["CustID"], "i", true);  
         csIDInt.MappingName = "CustID";  
         csIDInt.HeaderText = "CustID";  
         csIDInt.Width = 100;  
         ts1.GridColumnStyles.Add(csIDInt);  
    //STEP 4: Add the checkbox 
         DataGridColumnStyle boolCol = new DataGridBoolColumn();  
         boolCol.MappingName = "Current";  
         boolCol.HeaderText = "Info Current";  
    //uncomment this line to get a two-state checkbox  
         //((DataGridBoolColumn)boolCol).AllowNull = false;  
         boolCol.Width = 150; 
         ts1.GridColumnStyles.Add(boolCol);  
    //STEP 5: Add the tablestyle to your datagrid's tablestlye collection  
         myDataGrid.TableStyles.Add(ts1);
      

  6.   

    http://www.syncfusion.com/faq/winforms/Files/datagridcheckbox.zip
    这是一个例子,可以下载看看
      

  7.   

    可以试一下这里的方法,
    http://blog.csdn.net/zhzuo/archive/2006/02/27/611634.aspx
    http://blog.csdn.net/zhzuo/archive/2004/05/31/22036.aspx