点中datagrid,然后移动鼠标到右边,属性编辑页面出现,下面有个“属性生成器"

解决方案 »

  1.   

    最好的方法是向DataGrid里添加一模板列,再在页面上的DataGrid上点右键,“编辑模板列”往ItemTemplate或EditItemTemple里放你想加的控件就行了。
      

  2.   

    public class ColumnTemplate : ITemplate {
    private string ControlId = "";
    private bool blnEnable;
    public ColumnTemplate(string strId,bool enable) {
    ControlId = strId;
    blnEnable=enable;
    }
    public void InstantiateIn(Control container) {
    CheckBox myCheckBox = new CheckBox();
      myCheckBox.Checked =true;
    myCheckBox.ID = ControlId;          
    myCheckBox.Enabled=blnEnable;
    container.Controls.Add(myCheckBox);

    }

    }
    然后
     TemplateColumn newtempColumn;
    newtempColumn = new TemplateColumn();            
    newtempColumn.HeaderText = "Active";
    newtempColumn.HeaderStyle.Width=Unit.Pixel(50);
    ColumnTemplate cTemp;
    cTemp = new ColumnTemplate("CheckBox1",false);
    newtempColumn.ItemTemplate = cTemp;
                cTemp = new ColumnTemplate("CheckBox1",true);
                newtempColumn.EditItemTemplate=cTemp;    
    datagrid.Columns.Add(newtempColumn);
      

  3.   

    You create a custom DataTableStyle that contains column styles for each column you want to display. You add the column styles in the order you want them to appear. Here are the steps to add an string column, an int column and a bool check column to a DataGrid. You can also download a working project. 
     
    // 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);