绑定数据源

public void Data()
{
DataTable td = new DataTable();
DataRow row = td.NewRow();
foreach (GridColumn item in gridView1.Columns)
{
DataColumn it = new DataColumn(item.FieldName, typeof(String));
if (item.FieldName == "check")//添加复选框
{
td.Columns.Add(item.FieldName, Type.GetType("System.Boolean"));
td.Columns[item.FieldName].DefaultValue = Boolean.FalseString;
continue;
}
td.Columns.Add(it);
}

gridControl1.DataSource = td;
}

//重要

dev grid 常用方法-编程知识网

 

 

列标题字体设置
Appearences》HeaderPanel

1、获取选中行的某列的值:object ojb =GridView1.getrowcellvalue(GridView1.focusedrowhandle,"列名");

获取选中行的某列的值
this.gridView1.GetRowCellValue(0, gridView1.Columns["StorkCode"]);
//赋值
this.gridView1.SetRowCellValue(i, gridView1.Columns["jianshu"], gdjianshu.ToString());

2、设置标头居中,只需要设置Views–>Appearance–>HeaderPanel–>TextOptions.HAlignment=Center。内容居中设置:Columns–>AppearanceCell–>TextOptions.HAlignment=Center。上图第一列设置了内容居中。

3、设置Gridview控件,列头不可排序

this.gridDataDetail.gridView1.OptionsCustomization.AllowSort = false;

4、使Gridview控件,失去选中焦点

this.gridData.gridView1.FocusedRowHandle = -1

5、判断Gridview是否选中了数据

int index= this.gridData.gridView1.GetFocusedDataSourceRowIndex() ;

如果index小于0,证明没有选中行,否则就选中了行数据

6、获取选中Gridview的行数据

DataRow vCurrenRow = this.gridData.gridView1.GetFocusedDataRow();

7、删除选中Gridview行数据

this.gridDataDetail.gridView1.DeleteRow(this.gridDataDetail.gridView1.FocusedRowHandle);

8、Gridview新增一条编辑行

DataRow vDetailRow = this.DataDetailSourceTable.Rows.Add(); //这是新增加了一行
vDetailRow["primary_key"] = ""; primary_key为数据库绑定到Gridview中的字段,后面可以对其进行赋值。

9、获取GridView中所有的选中的行号

int[] iRowId = this.gridData.gridView1.GetSelectedRows();

在表格添加一列按钮
在column properties–>buttons—>将属性kind选为Glyph,然后将Caption设为把ButtonEdit的TextEditStyle设为HideTextEditor然后Button,第一个button的Caption写GO,Kind设为Glyph

在Gridview的OptionCustomization里面,有个属性叫"AllowColumnMoving",把这个关闭,就不会显示Column Chooser的菜单了。

//选中行的下标 
int index = this.gridView1.FocusedRowHandle;
//判断是否为回车 
if (e.KeyChar == Keys.Enter.GetHashCode())
{
//视图内是否有数据,并且当前下标是否在最后一行 
if (gridView1.Columns.View.RowCount != 0 && index < gridView1.Columns.View.RowCount – 1)
{
//如果大于0就从下标行开始 
if (index > 0)

//TO DO 
}
}
else { 
// SubjectQuotaRowAdd();
}
}
//这是我写的 回车代表新建 也能够代替Tab跳格键 我现在只能做出回车焦点定位到某行 而不能定位到某行的某单元

 

4、获取RadioGroup上选择的是哪一个,代码如下:
private void radioGroup1_SelectedIndexChanged(object sender, EventArgs e)
{
if (radioGroup1.SelectedIndex == 1)
{
MessageBox.Show("未发送"); 
}
if (radioGroup1.SelectedIndex == 0)
{
MessageBox.Show("已发送");
}
if (radioGroup1.SelectedIndex == 2)
{
MessageBox.Show("发送失败");
}
}

 

在gridview的CustomDrawCell事件中,添加如下代码,则可实现行数据的颜色控制。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 if (gvData.GetDataRow(e.RowHandle) == nullreturn;    if (gvData.GetDataRow(e.RowHandle)["列名"].ToString()=="1" )    { //该行数据的该列的值为1时,其背景色为gray    e.Appearance.BackColor = Color.Gray;    }    else    {    e.Appearance.BackColor = Color.Blue;    }    if (e.RowHandle == gvData.FocusedRowHandle)    {    e.Appearance.ForeColor = Color.White;    e.Appearance.BackColor = Color.RoyalBlue;    

        private void gridView2_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)         {             if(e.RowHandle == 2 && e.Column.FieldName == "NAME")             {                 e.Appearance.ForeColor = Color.Red;             }         }

 

知道DevExpress XtraGrid是如何定位和查找指定列显示值的行吗?注意是列的实显示值,而不是关联数据源列值。

下面将给出两个查找实例的代码,供大家一起来分享:

using DevExpress.XtraGrid.Views.Base; using DevExpress.XtraGrid.Columns; // ... string searchText = "Japan"; // obtaining the focused view ColumnView view = (ColumnView)gridControl1.FocusedView; // obtaining the column bound to the Country field GridColumn column = view.Columns["Country"]; if(column != null) { // locating the row //如果用数据源中的列值,请用ColumnView.LocateByValue int rhFound = view.LocateByDisplayText(view.FocusedRowHandle + 1, column, searchText); // focusing the cell if(rhFound != GridControl.InvalidRowHandle) { view.FocusedRowHandle = rhFound; view.FocusedColumn = column; } }

 

DevExpress.XtraGrid.Views.Base.ColumnView view = gridControl1.MainView as DevExpress.XtraGrid.Views.Base.ColumnView; view.BeginUpdate(); try { int rowHandle = 0; DevExpress.XtraGrid.Columns.GridColumn col = view.Columns["Category"]; while(true) { // locating the next row rowHandle = view.LocateByValue(rowHandle, col, "SPORTS"); // exiting the loop if no row is found if (rowHandle == DevExpress.XtraGrid.GridControl.InvalidRowHandle) break; // perform specific operations on the row found here // ... rowHandle++; } } finally { view.EndUpdate(); }

转载于:https://www.cnblogs.com/lhyqzx/p/7842742.html