1.编辑gridView后触发事件
private void gridView1_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
{}
2.gridView行不可编辑事件
private void gridView1_ShowingEditor(object sender, CancelEventArgs e)
{
DataRow row = this.gridView1.GetDataRow(this.gridView1.FocusedRowHandle);
if (row != null)
{
if ()
{
e.Cancel = true;//该行不可编辑
}}
}
3.gridView根据显示单元格内容控制行颜色以及字体颜色(包含选中行不变色)
///选中行带默认颜色
private void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
{
string a = gridView1.GetRowCellValue(e.RowHandle, "单元格1").ToString();
string b= gridView1.GetRowCellValue(e.RowHandle, "单元格2").ToString();
e.Appearance.ForeColor = Color.Black;//设置常规字体颜色if(a==?)
{
e.Appearance.BackColor = Color.Green;//根据单元格的值设置行颜色
e.Appearance.ForeColor = Color.White;//根据单元格的值设置字体颜色
}
if (e.RowHandle == gridView1.FocusedRowHandle)//设置选中行字体颜色
e.Appearance.ForeColor = Color.Red;
}
///选中行不带颜色
private void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
{
string trucknumber = gridView1.GetRowCellDisplayText(e.RowHandle, "单元格1").ToString().Trim();
switch (trucknumber)
{case "A":
e.Appearance.BackColor = Color.LightSkyBlue;
e.Appearance.ForeColor = Color.Black;
break;
case "B":
e.Appearance.BackColor = Color.MediumAquamarine;
e.Appearance.ForeColor = Color.Black;
break;
case "C":
e.Appearance.BackColor = Color.LemonChiffon;
e.Appearance.ForeColor = Color.Black;
break;
case "D":
e.Appearance.BackColor = Color.LightPink;
e.Appearance.ForeColor = Color.Black;
break;
default:
e.Appearance.BackColor = Color.LightSkyBlue;
e.Appearance.ForeColor = Color.Black;
break;
}
if (e.RowHandle == gridView1.FocusedRowHandle)
e.Appearance.ForeColor = Color.Red;}
4.gridView获取选中行某一单元格的数据
private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
{
//根据选择行进行数据的获取,将数据进行整合传递
int intRowHandle = e.FocusedRowHandle;
if (intRowHandle < 0)//没有选中行直接返回
{
return;
}
string Specifications = gridView1.GetRowCellValue(intRowHandle, "A").ToString();
string jlStatus = gridView1.GetRowCellValue(intRowHandle, "B" ) ToString();switch (jlStatus)//根据单元格数据自行调动自己想要的数值
{
case "E":
jlStatus = "03";
break;
case "D":
jlStatus = "01";
break;
case "F":
jlStatus = "02";
break ;
case "G":
jlStatus = "00";
break;
}}
5.gridView单元格数据双显示(直观和鼠标光标移动至...)
private void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
{
if ("状态".Equals(e.Column.FieldName))
{
string colValue = e.Value.ToString();
switch (colValue)
{
case "00":
e.DisplayText = "A";
break;
case "01":
e.DisplayText = "B";
break;
case "02":
e.DisplayText = "C";
break;
case "03":
e.DisplayText = "D";
break;
default:
break;
}
}
}
6.GridView根据列名确定需不需要排序
private void gv_MouseDown(object sender, MouseEventArgs e)
{
GridView gv = sender as GridView;
DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo gridHitInfo = gv.CalcHitInfo(new Point(e.X, e.Y));
if (gridHitInfo.InRow)
{
int currRow = gridHitInfo.RowHandle;
if (gv.IsGroupRow(currRow))
{
return;
}
}
//关闭gridview编辑
this..GridView.CloseEditor();
//提交gridview的更改
this..GridView.UpdateCurrentRow();
DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo info = this.pagedGridControl1.GridView.CalcHitInfo(e.X, e.Y);
//在列标题栏内且列标题name是"colName"
if (info.InColumnPanel && (info.Column.FieldName == "选择" || info.Column.FieldName == "层号" || info.Column.FieldName == "Layerno"))
{
//点击字段为“选择”时,不排序
this.GridView.OptionsCustomization.AllowSort = false;
}
else
{
//点击其他字段排序
this.GridView.OptionsCustomization.AllowSort = true;
}
}
6.GridView 多选、全选
6.1 添加复选框
//添加复选框
dt.Columns.Add("选择", typeof(bool));
dt.Columns["选择"].SetOrdinal(0);
//复选框赋予初始值 false
foreach (DataRow dr in dt.Rows)
{
dr["选择"] = false;
}
this.gc.DataSource = dt;
6.2 多选
//右键多选
private void Gridview_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e)
{
if (e.MenuType == DevExpress.XtraGrid.Views.Grid.GridMenuType.Row)
{
e.Menu.Items.Clear();
if (e.HitInfo.InRowCell)
{
e.Menu.Items.Add(CreateSelOrCelMenuItem(sender, e, "选择"));
e.Menu.Items.Add(CreateSelOrCelMenuItem(sender, e, "取消选择"));
}
}
}
private DXMenuItem CreateSelOrCelMenuItem(object sender, EventArgs e, string caption)
{
DXMenuItem selitem = new DXMenuItem(caption);
selitem.Click += delegate(object sender1, EventArgs e1) { OnSelOrCelClick(sender, e, caption); };
selitem.Tag = (GridView)sender;
return selitem;
}
///
/// 右键选择
///
///
///
///
private void OnSelOrCelClick(object sender, EventArgs e, string caption)
{
Boolean SelOrCel = true;
if (caption.Equals("选择"))
{
SelOrCel = true;
}
else if (caption.Equals("取消选择"))
{
SelOrCel = false;
}
GridView gvSel = (GridView)sender;
if (gvSel.GetSelectedRows().Length > 0)
{
Int32[] selectedRowHandles = gvSel.GetSelectedRows();
for (int i = 0; i < selectedRowHandles.Length; i++)
{
int selectedRowHandle = selectedRowHandles[i];
gvSel.SetRowCellValue(selectedRowHandle, "选择", SelOrCel);
DataRow dr = gvson.GetDataRow(selectedRowHandle);
if (dr["判断字段"].ToString() == "值") //根据列值才判定是否不可选
{
this.gvson.SetRowCellValue(selectedRowHandle, "选择", false);
}
}
//刷新gridview
this.gvson.RefreshData();
}
}
6.3 全选
private void gv_MouseDown(object sender, MouseEventArgs e)
{
//关闭gridview编辑
this.gvson.CloseEditor();
//提交gridview的更改
this.gvson.UpdateCurrentRow();
DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo info = this.gvson.CalcHitInfo(e.X, e.Y);
try
{
this.gvson.OptionsCustomization.AllowSort = false;
//在列标题栏内且列标题name是"colName"
if (info.InColumnPanel && info.Column.FieldName == "选择")
{
if (_chooseFlag)
{
for (int i = 0; i < this.gvson.RowCount; i++)
{
this.gvson.SetRowCellValue(i, "选择", true);
}
_chooseFlag = false;
}
else
{
for (int i = 0; i < this.gvson.RowCount; i++)
{
this.gvson.SetRowCellValue(i, "选择", false);
}
_chooseFlag = true;
}
}
for (int i = 0; i < this.gvson.DataRowCount; i++)
{
DataRow dr = gvson.GetDataRow(i);
if (dr["判断字段"].ToString() == "值") //根据列值才判定是否不可选
{
this.gvson.SetRowCellValue(i, "选择", false);
this.gvson.GetDataRow(i)["选择"] = false;
}
}
//刷新gridview
this.gvson.RefreshData();
}
catch { }
}
7.GridView 根据当前单元格内容修改当前单元格颜色。
private void gv_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
{string value = this.gvSample.GetRowCellValue(e.RowHandle, this.gvSample.Columns[e.Column.FieldName]).ToString();//获取列e.Column.FieldName的值。
if()//根据实际情况来
{
e.Appearance.BackColor = Color.White;//背景色
e.Appearance.ForeColor = Color.Black;//字体颜色}
}
8. GridView 单元格内容变更后触发事件。
private void gvSample_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
{string value = e.Value.ToString();//获取修改单元格值数据
this.gv.FocusedColumn.FieldName;//获取当前单元格列名 this.gv.GetFocusedRowCellValue(this.gvSample.Columns["列名"]).ToString();//获取改变单元格所在行某列单元格数据
this.gvSample.SetFocusedRowCellValue(this.gv.Columns["列名"],录入值);//给改变单元格所在行某列单元格赋值
}
9. GridView 双击内容行触发事件。
private void gv_MouseDown(object sender, MouseEventArgs e)
{//关闭gridview编辑
this.gv.CloseEditor();
//提交gridview的更改
this.gv.UpdateCurrentRow();
//获取选中
DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo info = this.gv.CalcHitInfo(e.X, e.Y);
//双击功能
if (e.Button == MouseButtons.Left && e.Clicks == 2)
{
if (info.Column != null && info.Column.FieldName == "选择")//是否有列的限制
return;
//获取当前选中的项目,若没有给出提示
if (string.IsNullOrEmpty(“选中行值”))
return;
//XtraMessageBox.Show("双击测试成功!");}
}