【单元格变色】
在GridView中的gridView_RowCellStyle事件中写代码逻辑,一定要加列名判断。如果不加列名判断,好像整行变色或者是没有效果。具体不记得了
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 if(e.Column.FiledName="XXX") //xxxx为需要变色的那一列绑定的字段名 2 { 3 DataRow dr = gridView2.GetDataRow(e.RowHandle); 4 string value= dr["XXX"].ToString().Trim();//获取单元格数据 5 //add change cell backcolor code 6 e.Appearance.BackColor=Color.Red;//使其变色的关键代码 7 }
【单元格根据条件可编辑设置】
1.首先我们定义是否可编辑函数。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 private bool DisableEditCell(GridView view,int fourceRow) 2 { 3 bool edit = false; 4 GridColumn col = view.Columns["Locked"]; 5 //选中单元格的数据 6 string val = Convert.ToString(view.GetRowCellValue(fourceRow, col)); 7 if (val == "N") edit = true; 8 return edit; 9 }
2.再在GridView的showingEdit事件中添加如下代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 private void gvforecast_ShowingEditor(object sender, CancelEventArgs e) 2 { 3 GridView view = sender as GridView; 4 //如果列名是"Forecast"且Locked列值不等于N,则不可以编辑 5 if (view.FocusedColumn.FieldName == "Forecast" && !DisableEditCell(view, view.FocusedRowHandle)) 6 { 7 e.Cancel = true; 8 } 9 }