前段时间和同学接了Pocket PC上的项目,关于测量煤矿温度的,其业务逻辑非常的复杂,因为要给用户演示,所以Datagrid显示就是一个放在眼前比较大的问题,它要求单元格能够宽度定位,并且选中时候能够改变背景颜色以标识选中行,由于.net Compact Framework下的资源非常的有限,控件功能非常的少,自带的DataGrid控件不能解决这个问题,所以就找了一个开源的Grid控件SourceGrid2.0(下载来解决问题。
   下面讲一下如何解决选定某行变色的问题,主要是解决问题的思路。
   因为是一个新的控件,功能也非常多,所以第一步,看一下说明文档,大家可以看下我兄弟翻译的文章,(这里)初步对其了解一下。
  
然后就开始找是否有可以直接修改的属性,结果没有,打开例子工程,发现工程上有一个变色的框框,然后定位到SourceGrid2.VisualModels.Common()这个类,知道其是一个描述单元格外观的类,不过是只能改一个单元格的,那我一行有N个单元格,岂不是要一个个的改?所以就接着看是否有可以改变一行的,结果没有,那就只好用这个了。:(
   接下来添加事件,它里面的事件可真是多,不过既然是要改变行的颜色,自然要定位能够获取选中行的Index的事件,所以根据字面解释找到了Selection_FocusRowEntered事件,它可以根据e.Row.Index找到屏幕上点中了哪一行,然后一个个遍历单元格变色。先直接试了下 

SourceGrid[e.Row.Index,i].VisualModel.BackColor=Color.SteelBlue

   结果发现这是只读属性不能赋值,
所以只能先定义一个VisualModels.Common对象再赋值了,代码如下

SourceGrid2.VisualModels.Common vm;
vm.BackColor 
= Color.SteelBlue;
SourceGrid[e.Row.Index,i].VisualModel
=vm;


  不过这样会在不知不觉中改变单元格属性,虽然你没有赋予其它值,只是改了颜色,但是它会把单元格其它属性变为默认值,譬如位置,和类型等,如下,这里位置变为居中,CheckBox变为String类型


   
   

让我非常的不爽,要是有能直接改变的就好了,就接着看它的属性和对象之间的构成,终于发现一个方法可以完美解决

    public static void Selection_FocusRowEntered(object sender, SourceGrid2.RowEventArgs e)
        {
            
for(int i=0;i<e.Row.Cells.Length;i++)
            {
                ((SourceGrid2.Cells.Real.Cell)e.Row.Cells[i]).BackColor
=Color.LightGreen;
            }
        }

    这就只改变背景了,对其它属性和类型没有影响,如下:


   然后添加焦点离开事件

    public static void Selection_FocusRowLeaving(object sender, SourceGrid2.RowCancelEventArgs e)
        {
            
for(int i=0;i<e.Row.Cells.Length;i++)
            {
                ((SourceGrid2.Cells.Real.Cell)e.Row.Cells[i]).BackColor
=Color.White;
            }
        }

    接着要解决用键盘上下键点击变色问题,想到KeyDown事件,结果发现这个SourceGrid自动解决了,变色也解决了,应该是上下键选择触发以上两个事件,所以不用添加了。
    
    可是我接下来发现还有一个问题,就是每次要点击屏幕两下才能变色,我想,应该是第一次点击是选择屏幕上的Grid 使其获得焦点,第二次才是获得事件,所以增加一个方法,SourceGrid.Rows[1].Select=true,使其一开始就获得了屏幕的焦点,然后再点击一次就可以变色了。

    因为SourceGrid里面没有直接绑定dataset的。所以为了简化代码,,先所以写了两个Static Help Object类包装事件,和数据绑定,方便调用。
 

public class ClickEvent
    {
        
private ClickEvent()
        {
        }
        
public static void Selection_FocusRowEntered(object sender, SourceGrid2.RowEventArgs e)
        {
            
for(int i=0;i<e.Row.Cells.Length;i++)
            {
                ((SourceGrid2.Cells.Real.Cell)e.Row.Cells[i]).BackColor
=Color.LightGreen;
            }
        }
        
public static void Selection_FocusRowLeaving(object sender, SourceGrid2.RowCancelEventArgs e)
        {
            
for(int i=0;i<e.Row.Cells.Length;i++)
            {
                ((SourceGrid2.Cells.Real.Cell)e.Row.Cells[i]).BackColor
=Color.White;
            }
        }
    }

 

    public class MySourceGrid
    {
        
private MySourceGrid()
        {
  
        }
        
public static void  BuileHeadText(SourceGrid2.Grid mySourceGird,string []heads,int count)
        {
            mySourceGird.ColumnsCount 
=count;
            mySourceGird.Rows.Insert(
0);
            
for (int r = 0; r <mySourceGird.ColumnsCount; r++)
            {
                mySourceGird[
0,r] = new SourceGrid2.Cells.Real.ColumnHeader(heads[r].ToString());
            }
        }
        
//isedit属性确定是否准许编辑
        public static void BuildSourceGrid_DS(SourceGrid2.Grid mySourceGird,DataSet ds,bool isedit)
        {
          
            
for (int i = 1; i <=ds.Tables[0].Rows.Count; i++)
            {
                mySourceGird.Rows.Insert(i);
                
for(int j=0; j<ds.Tables[0].Columns.Count; j++)
                {
                    SourceGrid2.Cells.Real.Cell l_cell 
=new SourceGrid2.Cells.Real.Cell(ds.Tables[0].Rows[i-1][j].ToString(),typeof(string));
                    l_cell.WordWrap
=true;
                    mySourceGird[i,j]
=l_cell;
                    mySourceGird[i,j].DataModel.EnableEdit 
= isedit;    
                }
            }
        }
        
public static void BuildSourceGrid(SourceGrid2.Grid mySourceGird,DataSet ds,bool isedit,string []heads,int count)
        {
            mySourceGird.ColumnsCount 
=count;
            mySourceGird.Rows.Insert(
0);
            
for (int r = 0; r <mySourceGird.ColumnsCount; r++)
            {
                mySourceGird[
0,r] = new SourceGrid2.Cells.Real.ColumnHeader(heads[r].ToString());
            }
 
            
for (int i = 1; i <=ds.Tables[0].Rows.Count; i++)
            {
                mySourceGird.Rows.Insert(i);
                
for(int j=0; j<3; j++)
                {
                    SourceGrid2.Cells.Real.Cell l_cell 
=new SourceGrid2.Cells.Real.Cell(ds.Tables[0].Rows[i-1][j].ToString(),typeof(string));
                    l_cell.WordWrap
=true;
                    mySourceGird[i,j]
=l_cell;
                    mySourceGird[i,j].DataModel.EnableEdit 
= isedit;    
                }
            }
        }
    }

  最后前台测试一下通过,代码片段如下

    private void Form1_Load(object sender, EventArgs e)
        {
            
string[] head={"String1","String2","String3","string4"};
            MySourceGrid.BuileHeadText(grid1,head,
4);  //添加表头

            
//构造测试dataset
            DataSet ds=new DataSet();
            DataTable dt
=new DataTable();
            DataColumn myColumn1
= new  DataColumn("string1");
            DataColumn myColumn2
= new DataColumn("string2");
            DataColumn myColumn3
= new DataColumn("string3");
            DataColumn myColumn4
= new DataColumn("string4");
            dt.Columns.Add(myColumn1);
            dt.Columns.Add(myColumn2);
            dt.Columns.Add(myColumn3);
            dt.Columns.Add(myColumn4);
            
for(int i=0;i<10;i++)
            {
                DataRow    myRow 
= dt.NewRow();
                myRow[
0]="John";
                myRow[
1= "maxwolf";
                myRow[
2= "jordon";
                myRow[
3= "songbo";
                dt.Rows.Add(myRow);
            }
            ds.Tables.Add(dt);
           
//构造测试dataset
            
            MySourceGrid.BuildSourceGrid_DS(grid1,ds,
false);//构造记录
            grid1.Rows[0].Select=true;//这句必须
            grid1.Selection.FocusRowEntered+=new SourceGrid2.RowEventHandler(ClickEvent.Selection_FocusRowEntered);//添加事件
            grid1.Selection.FocusRowLeaving+=new SourceGrid2.RowCancelEventHandler(ClickEvent.Selection_FocusRowLeaving);//添加事件
            grid1.AutoSize();
            
        }

  
  代码下载