我在用C#开发一个C/S的程序的时候,用到了控件DataGrid,这个控件用着也不错,单击表头的时候还可以实现按你单击表头的字段排序。
现在问题来了,这样的排序只是针对当前有效,翻到下一页的时候就失效了。不知道有什么办法通过单击表头的时候获得那个表头的字段名称,盼高手告知!来者有分!

解决方案 »

  1.   

    private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) 
         { 
              System.Drawing.Point pt = new Point(e.X, e.Y); 
              DataGrid.HitTestInfo hti = dataGrid1.HitTest(pt); 
              if(hti.Type == DataGrid.HitTestType.Cell) 
              {  
                   MessageBox.Show(dataGrid1[hti.Row, hti.Column].ToString()); 
              } 
              else if(hti.Type == DataGrid.HitTestType.ColumnHeader)  
              {  
                   MessageBox.Show(((DataView)DataGrid1.DataSource).Table.Columns        [hti.Column].ToString());           } 
     
         } 
      

  2.   

    在窗体的onmouseup事件中:
    DataGrid.HitTestInfo hit = this.dataGrid1.HitTest(e.X,e.Y);
    if ((hit.Type & DataGrid.HitTestType.ColumnHeader) != 0)
    {
    //
    MessageBox.Show(this.dataSet1.Tables[0].Columns[hit.Column].ColumnName);
    }