我的问题是:
1。如何删除多行
2。如何更改单元硌内容和颜色
3。如何根据不同条件显示不同的底色和前景色

解决方案 »

  1.   

    你的问题不难。
    1。
    public ArrayList GetSelectedRows(MyDataGrid dg) 
     

     
    ArrayList al = new ArrayList();
     
    CurrencyManager cm1 = (CurrencyManager)this.BindingContext[dg.DataSource]; 
     
    DataView dv = (DataView)cm1.List; 
      
    for(int i = 0; i < dv.Count; ++i) 
     

     
    if(dg.IsSelected(i)) 
     
    al.Add(i);
     

     
    return al; 
     

    以上返回选择项 
    -------------------------------------
    下面是删除:
    ArrayList ar=GetSelectedRows(dataGrid);
    ar.Sort();
    CurrencyManager cm = (CurrencyManager)this.BindingContext[dataGrid.DataSource]; 
    for(int i=ar.Count-1;i>=0;i--)//一定得从选中行的最后一行开始删除,否则删除会出错的
    {
    cm.RemoveAt((int)ar[i]);
    }
    ---------------------------------------------------------------------
    2.设置单元格值:
    dataGrid[eow,col]=123;
    改变单元格颜色:
    DataGridCell cl=new DataGridCell(r,c);
    Graphics g=Graphics.FromHwnd(grd.Handle);
    g.FillRectangle(new System.Drawing.SolidBrush(Color.YellowGreen),grd.GetCellBounds(cl));
    g.DrawString(grd[r,c].ToString(), grd.Font, new System.Drawing.SolidBrush(Color.Blue),grd.GetCellBounds(cl));
      

  2.   

    对了,以上设置单元格颜色是没多大用处,当换了行,表格将重画,颜色没了,只有在Paint中画才行,下面介绍你最后一问题:
    3。在Paint事件中加入下面代码
    int row=0;
    int yDelta = dataGrid.GetCellBounds(row, 0).Height + 1; 
     
    int y =dataGrid.GetCellBounds(row, 0).Top + 2; 
            CurrencyManager cr = (CurrencyManager) this.BindingContext[dataGrid.DataSource,dataGrid.DataMember]; 
     
    while(y <dataGrid.Height - yDelta && row < cr.Count) 
     

    DataView dv=(DataView)dataGrid.DataSource;
    int v=int.Parse(dv[row]["age"].ToString());
    if(v>40)//判断年龄大于40就改变颜色
    {
    for(int col=0;col<dataGrid.TableStyles[0].GridColumnStyles.Count;col++)
    {
    e.Graphics.FillRectangle(new System.Drawing.SolidBrush(Color.Snow),dataGrid.GetCellBounds(new DataGridCell(row, col)));
    e.Graphics.DrawString(dataGrid[row, col].ToString(), dataGrid.Font, new System.Drawing.SolidBrush(Color.Red),dataGrid.GetCellBounds(new DataGridCell(row, col)));
    }
    }
    y += yDelta; 
     
    row++; 
    }
      

  3.   

    推荐你看看LoveCherry的Blog,他那有很多DataGrid的详细介绍.