if you are using webform, in TemplateColumn's HeaderTemplate or ItemCreated or ItemDataBound event handler, insert a table or <BR> into the header column

解决方案 »

  1.   

    请使用DataGridTableStyle,参见ms-help://MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfsystemwindowsformsdatagridclasstopic.htm:using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;public class Form1 : System.Windows.Forms.Form
    {
       private System.ComponentModel.Container components;
       private Button button1;
       private Button button2;
       private DataGrid myDataGrid;   
       private DataSet myDataSet;
       private bool TablesAlreadyAdded;
       public Form1()
       {
          // Required for Windows Form Designer support.
          InitializeComponent();
          // Call SetUp to bind the controls.
          SetUp();
       }   protected override void Dispose( bool disposing ){
          if( disposing ){
             if (components != null){
                components.Dispose();}
          }
          base.Dispose( disposing );
       }
       private void InitializeComponent()
       {
          // Create the form and its controls.
          this.components = new System.ComponentModel.Container();
          this.button1 = new System.Windows.Forms.Button();
          this.button2 = new System.Windows.Forms.Button();
          this.myDataGrid = new DataGrid();
          
          this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
          this.Text = "DataGrid Control Sample";
          this.ClientSize = new System.Drawing.Size(450, 330);
          
          button1.Location = new Point(24, 16);
          button1.Size = new System.Drawing.Size(120, 24);
          button1.Text = "Change Appearance";
          button1.Click+=new System.EventHandler(button1_Click);      button2.Location = new Point(150, 16);
          button2.Size = new System.Drawing.Size(120, 24);
          button2.Text = "Get Binding Manager";
          button2.Click+=new System.EventHandler(button2_Click);      myDataGrid.Location = new  Point(24, 50);
          myDataGrid.Size = new Size(300, 200);
          myDataGrid.CaptionText = "Microsoft DataGrid Control";
          myDataGrid.MouseUp += new MouseEventHandler(Grid_MouseUp);
          
          this.Controls.Add(button1);
          this.Controls.Add(button2);
          this.Controls.Add(myDataGrid);
       }   public static void Main()
       {
          Application.Run(new Form1());
       }
       
       private void SetUp()
       {
          // Create a DataSet with two tables and one relation.
          MakeDataSet();
          /* Bind the DataGrid to the DataSet. The dataMember
          specifies that the Customers table should be displayed.*/
          myDataGrid.SetDataBinding(myDataSet, "Customers");
       }   protected void button1_Click(object sender, System.EventArgs e)
       {
          if(TablesAlreadyAdded) return;
          AddCustomDataTableStyle();
       }   private void AddCustomDataTableStyle()
       {
          DataGridTableStyle ts1 = new DataGridTableStyle();
          ts1.MappingName = "Customers";
          // Set other properties.
          ts1.AlternatingBackColor = Color.LightGray;      /* Add a GridColumnStyle and set its MappingName 
          to the name of a DataColumn in the DataTable. 
          Set the HeaderText and Width properties. */
          
          DataGridColumnStyle boolCol = new DataGridBoolColumn();
          boolCol.MappingName = "Current";
          boolCol.HeaderText = "IsCurrent Customer";
          boolCol.Width = 150;
          ts1.GridColumnStyles.Add(boolCol);
          
          // Add a second column style.
          DataGridColumnStyle TextCol = new DataGridTextBoxColumn();
          TextCol.MappingName = "custName";
          TextCol.HeaderText = "Customer Name";
          TextCol.Width = 250;
          ts1.GridColumnStyles.Add(TextCol);      // Create the second table style with columns.
          DataGridTableStyle ts2 = new DataGridTableStyle();
          ts2.MappingName = "Orders";      // Set other properties.
          ts2.AlternatingBackColor = Color.LightBlue;
          
          // Create new ColumnStyle objects
          DataGridColumnStyle cOrderDate = 
          new DataGridTextBoxColumn();
          cOrderDate.MappingName = "OrderDate";
          cOrderDate.HeaderText = "Order Date";
          cOrderDate.Width = 100;
          ts2.GridColumnStyles.Add(cOrderDate);      /* Use a PropertyDescriptor to create a formatted
          column. First get the PropertyDescriptorCollection
          for the data source and data member. */
          PropertyDescriptorCollection pcol = this.BindingContext
          [myDataSet, "Customers.custToOrders"].GetItemProperties();
     
          /* Create a formatted column using a PropertyDescriptor.
          The formatting character "c" specifies a currency format. */     
          DataGridColumnStyle csOrderAmount = 
          new DataGridTextBoxColumn(pcol["OrderAmount"], "c", true);
          csOrderAmount.MappingName = "OrderAmount";
          csOrderAmount.HeaderText = "Total";
          csOrderAmount.Width = 100;
          ts2.GridColumnStyles.Add(csOrderAmount);      /* Add the DataGridTableStyle instances to 
          the GridTableStylesCollection. */
          myDataGrid.TableStyles.Add(ts1);
          myDataGrid.TableStyles.Add(ts2);     // Sets the TablesAlreadyAdded to true so this doesn't happen again.
         TablesAlreadyAdded=true;
       }   protected void button2_Click(object sender, System.EventArgs e)
       {
          BindingManagerBase bmGrid;
          bmGrid = BindingContext[myDataSet, "Customers"];
          MessageBox.Show("Current BindingManager Position: " + bmGrid.Position);
       }   private void Grid_MouseUp(object sender, MouseEventArgs e)
       {
          // Create a HitTestInfo object using the HitTest method.      // Get the DataGrid by casting sender.
          DataGrid myGrid = (DataGrid)sender;
          DataGrid.HitTestInfo myHitInfo = myGrid.HitTest(e.X, e.Y);
          Console.WriteLine(myHitInfo);
          Console.WriteLine(myHitInfo.Type);
          Console.WriteLine(myHitInfo.Row);
          Console.WriteLine(myHitInfo.Column);
       }
      

  2.   

    // Create a DataSet with two tables and populate it.
       private void MakeDataSet()
       {
          // Create a DataSet.
          myDataSet = new DataSet("myDataSet");
          
          // Create two DataTables.
          DataTable tCust = new DataTable("Customers");
          DataTable tOrders = new DataTable("Orders");      // Create two columns, and add them to the first table.
          DataColumn cCustID = new DataColumn("CustID", typeof(int));
          DataColumn cCustName = new DataColumn("CustName");
          DataColumn cCurrent = new DataColumn("Current", typeof(bool));
          tCust.Columns.Add(cCustID);
          tCust.Columns.Add(cCustName);
          tCust.Columns.Add(cCurrent);      // Create three columns, and add them to the second table.
          DataColumn cID = 
          new DataColumn("CustID", typeof(int));
          DataColumn cOrderDate = 
          new DataColumn("orderDate",typeof(DateTime));
          DataColumn cOrderAmount = 
          new DataColumn("OrderAmount", typeof(decimal));
          tOrders.Columns.Add(cOrderAmount);
          tOrders.Columns.Add(cID);
          tOrders.Columns.Add(cOrderDate);      // Add the tables to the DataSet.
          myDataSet.Tables.Add(tCust);
          myDataSet.Tables.Add(tOrders);      // Create a DataRelation, and add it to the DataSet.
          DataRelation dr = new DataRelation
          ("custToOrders", cCustID , cID);
          myDataSet.Relations.Add(dr);
       
          /* Populates the tables. For each customer and order, 
          creates two DataRow variables. */
          DataRow newRow1;
          DataRow newRow2;      // Create three customers in the Customers Table.
          for(int i = 1; i < 4; i++)
          {
             newRow1 = tCust.NewRow();
             newRow1["custID"] = i;
             // Add the row to the Customers table.
             tCust.Rows.Add(newRow1);
          }
          // Give each customer a distinct name.
          tCust.Rows[0]["custName"] = "Customer1";
          tCust.Rows[1]["custName"] = "Customer2";
          tCust.Rows[2]["custName"] = "Customer3";      // Give the Current column a value.
          tCust.Rows[0]["Current"] = true;
          tCust.Rows[1]["Current"] = true;
          tCust.Rows[2]["Current"] = false;      // For each customer, create five rows in the Orders table.
          for(int i = 1; i < 4; i++)
          {
             for(int j = 1; j < 6; j++)
             {
                newRow2 = tOrders.NewRow();
                newRow2["CustID"]= i;
                newRow2["orderDate"]= new DateTime(2001, i, j * 2);
                newRow2["OrderAmount"] = i * 10 + j  * .1;
                // Add the row to the Orders table.
                tOrders.Rows.Add(newRow2);
             }
          }
       }
    }
      

  3.   

    搞错你的意思了,你说的要求,CSDN中有人借用标题行自绘格式实现过,不过我感觉那个例子比较粗糙的,你应该可以在CSDN的文档中找到它
      

  4.   

    code project上有个文章讲这个的,你去查查吧,我忘了地址了
      

  5.   

    我前几天也在研究datagrid,可以和你交流,你的第二个问题我已经解决了,第一个问题没太看懂你的意思。我的msn: [email protected]
      

  6.   

    DATAGRID中的列完全可以按照自己的要求想显示几行列就显示几行列。可使用自定义列的方法,先用boundcolomn定义各列,DATAGRID的autocolomn属性要设为faulse。以下是我们已完成的一个网站的部分代码:
    //定义自定义列
    private BoundColumn MaterialID=new BoundColumn();
    private BoundColumn Textureway=new BoundColumn();
    private BoundColumn Name=new BoundColumn();
    private BoundColumn SurfaceIndex=new BoundColumn();
    private BoundColumn Colour=new BoundColumn();
    //将自定义列按自己所需的位置插入datagrid中
    private void FillColumn()
    {
    DataSet RecordTalble=GetMaterial();
    MaterialID.HeaderText="代码";
    MaterialID.DataField=RecordTalble.Tables[0].Columns[0].ToString();
    MaterialID.SortExpression="MaterialID";
    gridTable.Columns.AddAt(0,MaterialID);        
    if(tableName=="SolidLumber"||tableName=="ArtificialLumber")
    {
    Textureway.HeaderText="纹理方向";
    Textureway.DataField=RecordTalble.Tables[0].Columns[3].ToString();
    Textureway.SortExpression="Textureway";
    gridTable.Columns.AddAt(2, Textureway);
    SurfaceIndex.HeaderText="表面";
    SurfaceIndex.DataField=RecordTalble.Tables[0].Columns[6].ToString();
    SurfaceIndex.SortExpression="SurfaceIndex";
    gridTable.Columns.AddAt(5,SurfaceIndex);
    }
    else if(tableName=="ProfileMaterial")
    {
    Name.HeaderText="名称";
    Name.DataField= RecordTalble.Tables[0].Columns[3].ToString();
    gridTable.Columns.AddAt(2,Name);
    Colour.HeaderText="颜色";
    Colour.DataField=RecordTalble.Tables[0].Columns[5].ToString();
    gridTable.Columns.AddAt(4,Colour);
    }
    else
    {

    Name.HeaderText="名称";
    Name.DataField= RecordTalble.Tables[0].Columns[3].ToString();
    gridTable.Columns.AddAt(2,Name);
    }

    }
    //数据绑定到datagrid之前应先调用FillColumn()
    private void UpdateGrid(DataView tableView)
    {
    FillColumn();
          gridTable.DataSource=tableView;
    gridTable.DataBind();
    }
      

  7.   

    具体看:
    http://youcome.vicp.net/bbs/dispbbs.asp?boardid=35&id=16956