如下:
DataGrid1[DataGrid1.CurrentCell.RowNumber,DataGrid1.CurrentCell.ColumnNumber].ToString ();
即:
DataGrid1[row,col]

解决方案 »

  1.   

    谢谢dzq138(钟添)我问的是: 
    DataGrid中处于编辑状态的单元格的值。DataGrid1[row,col]得不到处于编辑状态的单元格的值,
    DataGrid1[row,col]得到的是该单元格BeginEdit前的值
    和该单元格EndEdit后的值
    就是的不到处于编辑状态的单元格的值
      

  2.   

    哎说清楚点嘛,
    想得到键盘键盘在输入的内容对吧。这样有好处是比如可以不让别人输入不想要的字符等等,我猜你是想达到这样的目的。
    好办:
    1、你需重载当前的单元格输入框。方法如下://声明
    private DataGridTextBoxColumn datagridtextBox4;
    //在合适的地方写入,如绑定数据的后写放此句dg1为datagrid
    datagridtextBox4 = (DataGridTextBoxColumn)dg1.TableStyles[0].GridColumnStyles[4];   //指明第几列!
    datagridtextBox4.TextBox.GotFocus += new EventHandler(this.dgdFunctionArea_GotFocus);
    //再输入一个放一个自己需要的输放框如ComboBox呀TextBox
    private void dgdFunctionArea_GotFocus(object o, EventArgs e)
    {
    comboControl= new ComboBox();
    comboControl.Cursor = System.Windows.Forms.Cursors.Arrow;
    comboControl.Dock = DockStyle.Fill ;
    datagridtextBox4.TextBox.Controls.Add(comboControl);
    comboControl.Text =dg1[dg1.CurrentCell.RowNumber,dg1.CurrentCell.ColumnNumber].ToString ();
    comboControl.BringToFront();
    comboControl.SelectedIndexChanged  +=new EventHandler(this.comboControl_SelectedIndexChanged);
    comboControl.KeyPress  +=new EventHandler(this.comboControl_KeyPress);
    //
    }
    //在这里处理你的东东。!!!!!
    private void this.comboControl_KeyPress(object sender, 
    System.Windows.Forms.KeyPressEventArgs e)
    {
    //在这里处理你的东东。!!!!!
    }private void comboControl_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    dg1[dg1.CurrentCell.RowNumber,dg1.CurrentCell.ColumnNumber]=comboControl.Text;   //将改变的值传回DATAGRID中.
    }
      

  3.   

    dzq138(钟添):
    你的想法不错,但对DataGridBoolColumn不行,
    我没找到在DataGridBoolColumn上添加控件的方法。我是这样做的:
    1、添加一个checkBox控件
       在程序中响应键盘、鼠标事件更改单元格的值,
       同时也得到了单元格的值
    2、将dataGrid设为ReadOnly,
    3、//第4列为DataGridBoolColumn
    private void dataGrid1_Paint(object sender,    
             System.Windows.Forms.PaintEventArgs e)
    {
    checkBox1.Checked=
        Convert.ToBoolean(dataGrid1 [dataGrid1.CurrentRowIndex,4]);
    checkBox1.Size=dataGrid1.GetCellBounds(dataGrid1.CurrentCell).Size;
    checkBox1.Left=
     dataGrid1.GetCellBounds(dataGrid1.CurrentCell).Left+dataGrid1.Left;
    checkBox1.Top=
       dataGrid1.GetCellBounds(dataGrid1.CurrentCell).Top+dataGrid1.Top;
    }
    其他代码略但这样做,当拉动dataGrid的滚动条时,checkBox会跑到dataGrid的外面,
    要想消除这种情况,实现起来真得很麻烦,反正我是没找到好方法。另外,我加checkBox控件是为了当用户选中单元格时,界面有所反映,
    不加checkBox控件一样可以实现,
    只是应在dataGrid中响应键盘、鼠标事件更改单元格的值。还有更好的方法么?
      

  4.   

    //加上这两句,跑到外面的情况消除的了.
    checkBox1.Dock = DockStyle.Fill ;
    datagridtextBox4.TextBox.Controls.Add(checkBox1);
      

  5.   

    dzq138(钟添) :
    你这样做还是不行,
    虽然checkBox加去了,跑到外面的情况也消除了,
    但,
    当拉动dataGrid的滚动条,checkBox超过dataGrid边界时,
    checkBox会消失,留下datagridtextBox4.TextBox;
    而且,
    这一列应是DataGridBoolColumn不是datagridtextBox,
    我需要在该列显示checkBox的外观
      

  6.   

    通过添加checkBox或comboControl控件方法解决该问题,
    我以完全实现。哪位朋友有不通过该方法实现的,请俩日内答复,
    俩日后,无更好答案,我将结贴。
      

  7.   

    实现了能不能发到我信箱一份参考阿?
    [email protected]
      

  8.   

    ybcheng(夏之雨):
    完整代码是无法发给你了,以下是部分相关代码:System.Windows.Forms.CheckBox checkBox2=new CheckBox();private void Form_Load(object sender, System.EventArgs e)
    {
    dataGrid1.Controls.Add(checkBox2);
    checkBox2.BackColor=System.Drawing.Color.Blue;
    checkBox2.CheckAlign=System.Drawing.ContentAlignment.MiddleCenter;
    checkBox2.CheckedChanged+=new System.EventHandler
                  (this.checkBox2_CheckedChanged);
    }private void dataGrid1_CurrentCellChanged(object sender,
            System.EventArgs e)
    {
    if(dataGrid1.CurrentCell.ColumnNumber==4)
    {
    checkBox2.Checked=Convert.ToBoolean
          (dataGrid1[dataGrid1.CurrentRowIndex,4]);
    checkBox2.Size=dataGrid1.GetCellBounds(dataGrid1.CurrentCell).Size;
    checkBox2.Left=dataGrid1.GetCellBounds(dataGrid1.CurrentCell).Left;
    checkBox2.Top=dataGrid1.GetCellBounds(dataGrid1.CurrentCell).Top;
    checkBox2.Visible=true;
    }
    else
       checkBox2.Visible=false;
    dataGrid1.Focus();
    }private void checkBox2_CheckedChanged(object sender,  
           System.EventArgs e)
    {
    dataGrid1[dataGrid1.CurrentRowIndex,4]=checkBox2.Checked;
    dataGrid1.Focus();
    }private void dataGrid1_Paint(object sender,   
        System.Windows.Forms.PaintEventArgs e)
    {
    if(dataGrid1.CurrentCell.ColumnNumber==4)
    {
    checkBox2.Checked=Convert.ToBoolean
          (dataGrid1[dataGrid1.CurrentRowIndex,4]);
    checkBox2.Size=dataGrid1.GetCellBounds(dataGrid1.CurrentCell).Size;
    checkBox2.Left=dataGrid1.GetCellBounds(dataGrid1.CurrentCell).Left;
    checkBox2.Top=dataGrid1.GetCellBounds(dataGrid1.CurrentCell).Top;
    }
    }private void dataGrid1_KeyDown(object sender, 
        System.Windows.Forms.KeyEventArgs e)
    {
    if(e.KeyValue==32) //Space
    {
    if(dataGrid1[dataGrid1.CurrentRowIndex,4].ToString()=="False")
       dataGrid1[dataGrid1.CurrentRowIndex,4]=true;
    else
       dataGrid1[dataGrid1.CurrentRowIndex,4]=false;
    checkBox2.Checked=Convert.ToBoolean
       (dataGrid1[dataGrid1.CurrentRowIndex,4]);
    }
    }以上代码,为程序片断,是我从程序中挑选出的相关代码,看起来有点乱,
    请见谅,若你有更好的方法,请告知,谢谢。
      

  9.   

    另,
    dataGridBoolColumn1.ReadOnly = true;
    dataGrid1的第4列是dataGridBoolColumn1