大家可以在VS.Net2003查一下DataGridTextBoxColumn.Edit方法的示例,下面是其代码
public class Form1: Form
{
protected DataGrid dataGrid1;
protected DataSet myDataSet;protected void dataGrid1_MouseDown(object sender, MouseEventArgs e)
{
    // Use the HitTest method to get a HitTestInfo object.
    DataGrid.HitTestInfo hi;   
    DataGrid grid = (DataGrid)sender;
    hi=grid.HitTest(e.X, e.Y);
    // Test if the clicked area was a cell.
    if (hi.Type == DataGrid.HitTestType.Cell)
    {
       // If it's a cell, get the GridTable and CurrencyManager of the
       // clicked table.         
       DataGridTableStyle dgt = dataGrid1.TableStyles[0];     
       CurrencyManager cm = (CurrencyManager)
           this.BindingContext[myDataSet.Tables[dgt.MappingName]];
       // Get the Rectangle of the clicked cell.
       Rectangle cellRect = grid.GetCellBounds(hi.Row, hi.Column);
       // Get the clicked DataGridTextBoxColumn.
       MyGridColumn gridCol =(MyGridColumn)dgt.GridColumnStyles[hi.Column];
       // Edit the value.
       gridCol.EditCol(cm, hi.Row, cellRect, false, "New Text", true);
    }
 }
}public class MyGridColumn:DataGridTextBoxColumn{
   public void EditCol(CurrencyManager cm, int rowNum, 
      Rectangle cellRect, bool readOnly, 
      string myString, bool isVisible){
      this.Edit(cm, rowNum, cellRect, readOnly, myString, isVisible);
   }
}该代码在运行到 MyGridColumn gridCol = (MyGridColumn)dgt.GridColumnStyles[hi.Column];这一行时,会发生无法将GridColumnStyle类型转换为自定义的MyGridColumn类型的错误。
我理解GridColumnStyle类是DataGridTextBoxColumn类的父类,DataGridTextBoxColumn类是自定义的MyGridColumn类的父类,因此将dgt.GridColumnStyles[hi.Column]引用的GridColumnStyle作为父类向MyGridColumn子类转换是不正确的。
谁能解释一下?