DevExpress控件之GridControl、GridView
GridControl對應標準WinForm里的GridView,相當于是一個控件,里面包含多個GridView也可以放其它的控件
?
禁止修改
gridView1.OptionsBehavior.Editable = false;
?
一、
去掉"Drag a column header here to group by that column"一欄
gridView1.OptionsView.ShowGroupPanel = false;
只想隱藏這句話,保留這個頭部,設置Appearance下的GroupPanelText為" "
?
二、Devexpress GridControl切換數據源
gridControl1.DataSource = dt1;
(gridControl1.DefaultView as GridView).Columns.Clear();//切換前需要先把列清空了。
gridControl1.DataSource = dt2;
(gridControl1.DefaultView as GridView).PopulateColumns();
紅色部分取一種寫法即可。
?
三、選中的行、值
int selectRow = gridView1.GetSelectedRows()[0];
//1 需要知道列名
string id = this.gridView1.GetRowCellValue(selectRow, "id").ToString();
//2 獲取焦點值
object selectValue = gridView1.GetFocusedValue();
?
四、顯示搜索框
gridView1.OptionsFind.AlwaysVisible = true;
?
五、選中某一行
GridView.FocusedRowHandle?=i;
GridView.SelectRow(i);
?
?
六:遍歷GridView
?
for (int i = 0; i < gridView1.RowCount; i++)
{
? ?? ? for (int j = 0; j < gridView1.Columns.Count; j++)
? ?? ? {
? ?? ?? ?? ? object val = gridView1.GetRowCellValue(i, gridView1.Columns[j]);
? ?? ? }
}
?
?
七:單元格雙擊響應
?
需要先將gridview1.OptionsBehavior.Editable設為false,然后響應gridControl1_DoubleClick事件。
?
??????? private void gridControl1_DoubleClick(object sender, EventArgs e)
??????? {
??????????? MouseEventArgs arg = e as MouseEventArgs;
??????????? if (arg == null)
??????????????? return;
?
??????????? GridHitInfo hitInfo = gridView1.CalcHitInfo(new Point(arg.X, arg.Y));//獲取坐標點
??????????? if (hitInfo.RowHandle >= 0)
??????????? {
??????????????? DataRow row = gridView1.GetDataRow(hitInfo.RowHandle);
??????????????? _list.Clear();
??????????????? _list.Add(row[0].ToString());
??????????????? gisResoureMonControl1.SetSelectResource(_list);
??????????? }???????????
??????? }
?
?
八、DevExpress gridcontrol如何分組顯示
(1)、手動方式
1、添加分組項,Run Designer--Group Summary Items--Add,設置計算添加SummaryType:Count總計
?
2、設置顯示格式
? 2.1 格式:{0},效果:顯示分組的列標題,如:Order ID
?2.2?格式:{1},效果:顯示分組后的項,如:10248
3、效果如下:
(2)、代碼
?
? ? ? ? ? ? gridView1.GroupSummary.Add(DevExpress.Data.SummaryItemType.Count, "分組1"); ?//添加分組1,如果不是count,則名稱必須與字段名對應
? ? ? ? ? ? gridView1.GroupFormat = "{1} {2}"; ?//默認"{0}: [#image]{1} {2}"; 字段名稱:數據 計數=?
? ? ? ? ? ? gridView1.Columns["部門名稱"].GroupIndex = 0; ?//設置默認分組列
? ? ? ? ? ? //分組列格式
? ? ? ? ? ? gridView1.GroupSummary.Add(DevExpress.Data.SummaryItemType.Average, "id", gridView1.Columns["id"]);
? ? ? ? ? ? gridView1.GroupSummary[1].DisplayFormat = "AVG={0:c}";
? ? ? ? ? ? gridView1.GroupSummary.Add(DevExpress.Data.SummaryItemType.Count, "姓名", gridView1.Columns["姓名"]);
? ? ? ? ? ? ((DevExpress.XtraGrid.GridSummaryItem)gridView1.GroupSummary[gridView1.GroupSummary.Count - 1]).DisplayFormat = "小計:{0:N0}";
? ? ? ? ? ? gridView1.ExpandAllGroups();
效果如下:
?
九 、C# devExpress GridControl 行中行 子行 多級行
?
DB db = new DB();DataSet ds = new System.Data.DataSet();SqlCommand comm2 = new SqlCommand(sql, db.getSqlConnection()); //db.getSqlConnection() 返回一個sqlConnection 對象SqlCommand comm3 = new SqlCommand(sql1, db.getSqlConnection());SqlCommand comm4 = new SqlCommand(sql2, db.getSqlConnection());SqlDataAdapter da2 = new SqlDataAdapter(comm2);SqlDataAdapter da3 = new SqlDataAdapter(comm3);SqlDataAdapter da4 = new SqlDataAdapter(comm3);da2.Fill(ds,"emp");da3.Fill(ds,"job");da4.Fill(ds,"re");this.treeListLookUpEdit1TreeList.DataSource = ds;DataColumn parentColumn = ds.Tables["emp"].Columns["empNum"];DataColumn childColumn = ds.Tables["job"].Columns["empNum"];DataColumn secondChild = ds.Tables["re"].Columns["empNum"];DataRelation relCustOrder;relCustOrder = new DataRelation("對應工作", parentColumn, childColumn);DataRelation job;job = new DataRelation("部門調整", childColumn, secondChild);ds.Relations.Add(relCustOrder);ds.Relations.Add(job);this.gridControl1.DataSource = ds.Tables["emp"];?
?
?十、隱藏從表列 (只能隱藏一級子表)
//隱藏子表(即從表)的列,獲取主表的行展開事件
private void gridView3_MasterRowExpanded(object sender, DevExpress.XtraGrid.Views.Grid.CustomMasterRowEventArgs e) {//獲取所點擊行的從表對象 DevExpress.XtraGrid.Views.Grid.GridView childView= gridView3.GetDetailView(e.RowHandle, e.RelationIndex) as DevExpress.XtraGrid.Views.Grid.GridView;if (childView != null){childView.Columns["MainId"].Visible = false; //隱藏子表列 } }?
十一、展開第一級子表
for (int i = 0; i < gridView1.RowCount - 1; i++){gridView1.SetMasterRowExpandedEx(i, -1, true);}十二、展開所有子表 及隱藏子表的列
int m_RelationIndex = 0;private void ExpandAllRows(){for (int masteViewRowIndex = 0; masteViewRowIndex < ds.Tables["Table1"].Rows.Count; masteViewRowIndex++){MainGridView.ExpandMasterRow(masteViewRowIndex, 0);ExpandChildRows(MainGridView, masteViewRowIndex);}}private void ExpandChildRows(GridView gv, int rowIndex){GridView currentChildGV = gv.GetDetailView(rowIndex, m_RelationIndex) as GridView;if (currentChildGV != null){for (int childGVRowIndex = 0; childGVRowIndex < currentChildGV.DataRowCount; childGVRowIndex++){ExpandChildRows(currentChildGV, childGVRowIndex);//more 可以在此處隱藏子表的列}}else if (currentChildGV == null && gv.CanExpandMasterRowEx(rowIndex, m_RelationIndex)){gv.SetMasterRowExpandedEx(rowIndex, m_RelationIndex, true);ExpandChildRows(gv, rowIndex);
//more 可以在此處隱藏子表的列}else if (currentChildGV == null && !gv.CanExpandMasterRowEx(rowIndex, m_RelationIndex)){return;}}
?
十三、GridControl切換數據源異常,提示 “無法將類型為“NameSpace.ClassName”的對象強制轉換為類型“System.Data.DataRowView”?!?/p>
解決方法,在DataSource賦值前后加上BeginUpdate和EndUpdate
mainGridControl1.BeginUpdate(); mainGridControl1.DataSource = listProduct; mainGridControl1.EndUpdate();?
參考
1 DEV控件GridControl常用屬性設置
2 DevExpress gridcontrol如何分組顯示
3 DevExpress gridcontrol gridView主從表折疊/展開顯示
4 DevExpressControl中的GridControl展現主從表數據結構
轉載于:https://www.cnblogs.com/code1992/p/10450362.html
總結
以上是生活随笔為你收集整理的DevExpress控件之GridControl、GridView的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信小程序http转https
- 下一篇: (译)删除未使用的前端代码