c# winForm DotNetBar控件之SuperGridControl
1.添加表頭
sgc.PrimaryGrid.SelectionGranularity = SelectionGranularity.Row;//點擊選中一行DevComponents.DotNetBar.SuperGrid.GridColumn gc = null;gc = new DevComponents.DotNetBar.SuperGrid.GridColumn("ID");sgc.PrimaryGrid.Columns.Add(gc);gc = new DevComponents.DotNetBar.SuperGrid.GridColumn("類型編碼");sgc.PrimaryGrid.Columns.Add(gc);2.添加數據 加一行
sgc.PrimaryGrid.Rows.Add(new GridRow(new object[] { "a", "b" }));//也就是加一個GrindRow對像?
3.設點擊選中一行后 取第一行第一列的值
SelectedElementCollection col = sgc.PrimaryGrid.GetSelectedRows();//選中的行集合if (col.Count > 0){GridRow gr = (col[0] as GridRow);//把第一行轉為GridRow fac.ID = int.Parse(gr.Cells[0].Value.ToString());//取第一列的Value轉一下//等效于int id= int.Parse((sgc.PrimaryGrid.GetSelectedRows()[0] as GridRow).Cells[0].Value.ToString());}?4.增加一個下拉框
?4.1
using DevComponents.DotNetBar.SuperGrid; using System; using System.Windows.Forms;namespace TestForm {public partial class Form1 : Form{public Form1(){InitializeComponent();this.Load += Form1_Load;}private void Form1_Load(object sender, EventArgs e){GridColumn col = new GridColumn("這是一個下拉框");col.HeaderText = "這是一個下拉框";col.Name = "這是一個下拉框";col.AutoSizeMode = ColumnAutoSizeMode.DisplayedCells;col.EditorType = typeof(DefineGridCB);col.EditorParams = new object[] { new object[] { "第一個", "第二個" } };superGridControl1.PrimaryGrid.Columns.Add(col);}//自定義控件public class DefineGridCB : GridComboBoxExEditControl{public DefineGridCB(object source){DataSource = source;}}//增加一行private void buttonItem1_Click(object sender, EventArgs e){superGridControl1.PrimaryGrid.NewRow(true);}} } View Code? 4.2 方法二 上邊不傳值 直接給一個無參構造方法
public class ThisGridComboBoxControl : GridComboBoxExEditControl{public ThisGridComboBoxControl(){CustomList cl = new CustomList("BSC201Compare"); DataTable dt = cl.GetList();//這里我是執(zhí)行了一個sql查詢 有符號和名稱兩個列 反回一個dataTable DataSource = dt;DisplayMember = "名稱";ValueMember = "符號";DropDownStyle = ComboBoxStyle.DropDownList;DropDownColumns = "名稱|符號";MaxDropDownItems = 8;}}4.2效果?
?
5.增加一個按鈕
using DevComponents.DotNetBar.SuperGrid; using System; using System.Windows.Forms; using DevComponents.DotNetBar.SuperGrid.Style;namespace TestForm {public partial class Form1 : Form{public Form1(){InitializeComponent();this.Load += Form1_Load;}private void Form1_Load(object sender, EventArgs e){GridColumn col = new GridColumn("這是一個下拉框");col.HeaderText = "這是一個下拉框";col.Name = "這是一個下拉框";col.AutoSizeMode = ColumnAutoSizeMode.DisplayedCells;col.EditorType = typeof(DefineGridCB);col.EditorParams = new object[] { new object[] { "第一個", "第二個" } };superGridControl1.PrimaryGrid.Columns.Add(col);col = new GridColumn("這是一個按鈕");col.AutoSizeMode = ColumnAutoSizeMode.DisplayedCells;col.EditorType = typeof(DefineGridButtonX);superGridControl1.PrimaryGrid.Columns.Add(col);}/// <summary>/// 自己定義按鈕/// </summary>public class DefineGridButtonX : GridButtonXEditControl{public DefineGridButtonX(){this.Click += DefineGridButtonX_Click;}private void DefineGridButtonX_Click(object sender, EventArgs e){MessageBox.Show("1");}/// <summary>/// 控件屬性/// </summary>/// <param name="cell"></param>/// <param name="style"></param>public override void InitializeContext(GridCell cell, CellVisualStyle style){base.InitializeContext(cell, style);this.Text = "這是一個按鈕";}}//自定義下拉控件public class DefineGridCB : GridComboBoxExEditControl{public DefineGridCB(object source){DataSource = source;}}//增加一行private void buttonItem1_Click(object sender, EventArgs e){superGridControl1.PrimaryGrid.NewRow(true);}} } View Code改變按鈕顏色-依照別的列改變 在class的初始化中做如下操作
public override void InitializeContext(GridCell cell, CellVisualStyle style){base.InitializeContext(cell, style);BackColor = Color.Transparent;//這個必須加上 不然也沒有效果if ((EditorCell.GridRow["這是一個下拉框"].Value ?? "").ToString() != ""){unchecked{ForeColor = Color.FromArgb(255, 135, 206, 235);BackColor = Color.FromArgb(255, 135, 206, 235);}ColorTable = eButtonColor.Blue;}}
?
?
4~5效果
? ?
5.superGridControl一列變化 另一列自動加載對應的數據 判斷一下值變貨的單元格(后來看了看還是用cellClick會更好一些)
private void SuperGridControl1_CellValueChanged(object sender, GridCellValueChangedEventArgs e){if (e.GridCell.GridColumn.Name == "下拉列"){superGridControl1.PrimaryGrid.Columns["下拉變動列"].EditorParams = new object[] { new object[] { "wf","HH"} };}}?6.自動行高
SPG.PrimaryGrid.DefaultRowHeight = 0;?7.去掉列頭行頭
sgcCondition.SPG.PrimaryGrid.ShowColumnHeader = false;// sgcCondition.SPG.PrimaryGrid.ColumnHeader.Visible = false;//效果ms和上邊一個樣。。。sgcCondition.SPG.PrimaryGrid.ShowRowHeaders = false;?8.選中行 默認選中首行
SPG.PrimaryGrid.SelectionGranularity = SelectionGranularity.Row; SPG.PrimaryGrid.InitialSelection = RelativeSelection.Row;?9.增加一行
spgDetail.PrimaryGrid.NewRow(true);10.刪除選中行
SelectedElementCollection lstGR = spgDetail.PrimaryGrid.GetSelectedRows();if (lstGR != null)if (lstGR.Count > 0){foreach (GridRow gr in lstGR){spgDetail.PrimaryGrid.Rows.Remove(gr);}}11.顯示行號 且從1開始
SPG.PrimaryGrid.ShowRowGridIndex = true; SPG.PrimaryGrid.RowHeaderIndexOffset=1;?12.交換兩行數據 實現上移下移
public static object[] GetRowValues(GridContainer gcRow){object[] obj = new object[(gcRow as GridRow).Cells.Count];for (int i = 0; i < (gcRow as GridRow).Cells.Count; i++){obj[i] = (gcRow as GridRow)[i].Value;}return obj;}/// <summary>/// 上、下移動表格行 /// </summary>/// <param name="spg"></param>/// <param name="gr"></param>/// <param name="isUp"></param>public static bool MoveSPGRow(SuperGridControl spg, bool isUp = true){var atRow = spg.PrimaryGrid.ActiveRow;if (atRow == null){PublicProperties.ShowInformation("請先選中要移動的行", AlertImage.Alert, 2000);//這里是個吐司函數就是提示一下return false;}object[] objRow = GetRowValues(atRow);if (isUp){if (atRow.RowIndex == 0){PublicProperties.ShowInformation("已經是第一行了,無法再向上移動", AlertImage.Alert, 2000);//這里是個吐司函數就是提示一下return false;}var atTop = spg.PrimaryGrid.Rows[atRow.RowIndex - 1];object[] objTop = GetRowValues(atTop as GridRow);spg.PrimaryGrid.Rows[atRow.Index - 1] = new GridRow(objRow);spg.PrimaryGrid.Rows[atRow.Index] = new GridRow(objTop);spg.PrimaryGrid.SetActiveRow(spg.PrimaryGrid.Rows[atRow.Index - 1] as GridRow);spg.PrimaryGrid.SetSelected(spg.PrimaryGrid.Rows[atRow.Index] as GridRow, false);}else{if (atRow.RowIndex == spg.PrimaryGrid.Rows.Count - 1){PublicProperties.ShowInformation("已經是最后一行了,無法再向下移動", AlertImage.Alert, 2000);//這里是個吐司函數就是提示一下return false;}var atBottum = spg.PrimaryGrid.Rows[atRow.RowIndex + 1];object[] objBottum = GetRowValues(atBottum as GridRow);spg.PrimaryGrid.Rows[atRow.Index + 1] = new GridRow(objRow);spg.PrimaryGrid.Rows[atRow.Index] = new GridRow(objBottum);spg.PrimaryGrid.SetActiveRow(spg.PrimaryGrid.Rows[atRow.Index + 1] as GridRow);spg.PrimaryGrid.SetSelected(spg.PrimaryGrid.Rows[atRow.Index] as GridRow, false);}return true;}} View Code?13.編輯superGridCell時 焦點沒離開cell 點不失焦點的控件 如bar的buttonItem 那么cell里的值不會變,這里可用SetActive()函數 使焦點離開cell
SPG.PrimaryGrid.SetActive(false)?14.選中行自動行高
/// <summary>/// 只對選中行自動行高/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void SPG_RowActivated(object sender, GridRowActivatedEventArgs e){GridRow newrow = e.NewActiveRow == null ? new GridRow() : (e.NewActiveRow as GridRow);GridRow oldrow = e.OldActiveRow == null ? new GridRow() : e.OldActiveRow as GridRow;newrow.RowHeight = 0;if (newrow != oldrow){oldrow.RowHeight = 21;//原始寬度 }}?15.superGrid 實現選中區(qū)域上下填充
/// <summary>/// 實現superGridControl選中區(qū)上下填充/// </summary>/// <param name="isDown">true 向下 false 向上</param>/// <param name="isFolowCursor">true激活行跟隨 false 不跟隨</param>private static void FillCells(SuperGridControl SPG, bool isDown = true, bool isFolowCursor = true){//var cellSel = spgData.SPG.GetSelectedCells();//if (cellSel != null)//{//}var cellSel = SPG.GetSelectedCells();if (cellSel == null){return;}int iFirst = (cellSel.First() as GridCell).RowIndex;int iEnd = (cellSel.Last() as GridCell).RowIndex;GridRow grFirst = SPG.PrimaryGrid.Rows[iFirst] as GridRow;GridRow grEnd = SPG.PrimaryGrid.Rows[iEnd] as GridRow;for (int j = iFirst; j <= iEnd; j++){GridRow gr = SPG.PrimaryGrid.Rows[j] as GridRow;GridRow grTmp = null;if (isDown)grTmp = grFirst;elsegrTmp = grEnd;for (int i = 0; i < SPG.PrimaryGrid.Columns.Count; i++){if (gr[i].IsSelected && gr[i].AllowEdit == true){gr[i].Value = grTmp[i].Value;}}if (isFolowCursor)gr.SetActive();}if (isFolowCursor)grFirst.SetActive();}?
?16.superGridControl出現滾動條時 追加數據 始終顯示最后一條
//SPG.PrimaryGrid.ScrollToBottom()//這個不知為什么不行 看意思好像行的樣子 //如果對spg有其他操作要刷新一下 spg.Refresh()不刷新下邊的代碼將不起作用 SPG.PrimaryGrid.LastOnScreenRowIndex = (SPG.PrimaryGrid.Rows.Last() as GridRow).Index;?17.superGridControl 列的數據居中對齊
spgLoginInfo.DefaultVisualStyles.CellStyles.Default.Alignment = DevComponents.DotNetBar.SuperGrid.Style.Alignment.MiddleCenter;18.改變列頭顏色
spgLoginInfo.DefaultVisualStyles.ColumnHeaderStyles.Default.Background.Color1 = Color.FromArgb(8, 47, 76);spgLoginInfo.DefaultVisualStyles.ColumnHeaderStyles.Default.Background.Color2 = Color.FromArgb(8, 47, 76);?19.superGridControl透明
//整體透明superGridControl1.BackColor = Color.Transparent;//面板透明superGridControl1.DefaultVisualStyles.GridPanelStyle.Background.Color1= Color.Transparent;//行透明superGridControl1.DefaultVisualStyles.RowStyles.Default.Background.Color1 = Color.Transparent;//單元格透明superGridControl1.DefaultVisualStyles.CellStyles.Default.Background.Color1 = Color.Transparent;//改變單個單元格顏色(superGridControl1.PrimaryGrid.Rows[0] as GridRow).Cells[0].CellStyles.Default.Background.Color1 = Color.Red;?
轉載于:https://www.cnblogs.com/SoftWareIe/p/8757352.html
總結
以上是生活随笔為你收集整理的c# winForm DotNetBar控件之SuperGridControl的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 快速幂求模
- 下一篇: 网络IO超时的几种实现