1:隐藏右上角的选择页面三角:controlbox-menubox-autohide 设置为false
2:设置某一单元的背景颜色:DG[c].CellStyles.Default.Background.Color1 = Color.Red;//如果列的类型是GridButtonXEditControl只会改变边框线颜色
public class SuperGridOP
{
//根据列分组
public static void SG_Group(SuperGridControl SGC,string colname)
{
GridPanel panel = SGC.PrimaryGrid;
panel.AutoExpandSetGroup = true;
if (colname == "")
{
panel.SetGroup();
}
else
{
panel.SetGroup(panel.Columns[colname], SortDirection.Descending);
}
SGC.Focus();
}
//SuperGridControl控件初始化
public static void ResetLayout(SuperGridControl SGC)
{
GridPanel panel = SGC.PrimaryGrid;
GridColumnCollection columns = panel.Columns;
for (int i = 0; i < columns.Count; i++)
{
GridColumn col = columns[i];
col.Visible = false;
col.DisplayIndex = -1;
col.HeaderStyles.Default.Image = null;
col.HeaderStyles.Default.Background = null;
col.HeaderStyles.MouseOver.Background = null;
col.CellStyles.Default.Background = null;
}
panel.ColumnHeader.GroupHeaders.Clear();
panel.ClearAll();
SGC.PrimaryGrid.ColumnHeaderClickBehavior = ColumnHeaderClickBehavior.None;//表头点击没效果
}
//设置控件属性
public static void SetSGCControl(SuperGridControl SGC)
{
//superGridControl1.PrimaryGrid.MaxRowHeight = 18;
SGC.PrimaryGrid.GroupHeaderHeight = 18;//分组高度
SGC.PrimaryGrid.DefaultRowHeight = 18;//行高
SGC.PrimaryGrid.ColumnHeader.RowHeight = 20;//标题高度
// superGridControl1.PrimaryGrid.Columns["PrjLocation"].AutoSizeMode = ColumnAutoSizeMode.Fill;
SGC.PrimaryGrid.ExpandButtonType = ExpandButtonType.Triangle;//分组-树展开符号
SGC.PrimaryGrid.GroupHeaderClickBehavior = GroupHeaderClickBehavior.ExpandCollapse;//分组-展开
SGC.PrimaryGrid.MultiSelect = false;//不能选多行
SGC.PrimaryGrid.SelectionGranularity = SelectionGranularity.Row;//一次选中一整行
SGC.PrimaryGrid.RowWhitespaceClickBehavior = RowWhitespaceClickBehavior.ExtendSelection;//行空白区单击行为
SGC.PrimaryGrid.ShowGroupUnderline = false;
SGC.PrimaryGrid.ShowRowHeaders = false;// = 10;
SGC.DefaultVisualStyles.RowStyles.SelectedMouseOver =
SGC.BaseVisualStyles.RowStyles.Selected;//当控件宽度>列宽之和时 设置这个更好看
//设置表格中文字的位置居中
SGC.PrimaryGrid.DefaultVisualStyles.CellStyles.Default.Alignment = DevComponents.DotNetBar.SuperGrid.Style.Alignment.MiddleCenter;
}
//导出excel
public static System.Data.DataTable GetGridToDT(SuperGridControl SGC, string tbname)
{
System.Data.DataTable dt = new System.Data.DataTable(tbname);
Dictionary<string, string> diccol = new Dictionary<string, string>();//headertext-name
GridPanel gp = SGC.PrimaryGrid;
for (int i = 0; i < gp.Columns.Count; i++)
{
GridColumn gc = gp.Columns[i];
if (gc.Visible)
{
dt.Columns.Add(gc.HeaderText);
diccol.Add(gc.HeaderText, gc.Name);
}
}
int rs = GetGridRowCount(SGC);
for (int i = 0; i < rs; i++)
{
DataRow dr = dt.NewRow();
GridRow DG = GetGridRowByRowIndex(SGC, i);
DataRow drtag = DG.Tag as DataRow;
for (int j = 0; j < dt.Columns.Count; j++)
{
string colname = dt.Columns[j].ColumnName;
dr[colname] = GetGridVal(SGC, i, diccol[colname]);
}
dt.Rows.Add(dr);
}
return dt;
}
//根据行号获取GridRow对象
public static GridRow GetGridRowByRowIndex(SuperGridControl SGC, int r)
{
GridPanel gp = SGC.PrimaryGrid;
int startindex = 0;
foreach (GridElement item in gp.Rows)
{
if (item is GridGroup)
{
GridGroup gg = item as GridGroup;
foreach (GridElement item1 in gg.Rows)
{
if (item1 is GridRow)
{
if (startindex == r)
{
return (GridRow)item1;
}
startindex++;
}
}
}
else if (item is GridRow)
{
if (startindex == r)
{
return (GridRow)item;
}
startindex++;
}
}
return null;
}
//获取所有行(如果有分组的情况,SGC.PrimaryGrid.Rows.Count获取行数得到的是组的个数)
private static int GetGridRowCount(SuperGridControl SGC)
{
GridPanel gp = SGC.PrimaryGrid;
int startindex = 0;
foreach (GridElement item in gp.Rows)
{
if (item is GridGroup)
{
GridGroup gg = item as GridGroup;
foreach (GridElement item1 in gg.Rows)
{
if (item1 is GridRow)
{
startindex++;
}
}
}
else if (item is GridRow)
{
startindex++;
}
}
return startindex;
}
//获取某行某列的单元格数据
private static string GetGridRowVal(GridRow DG, string cname)
{
string txt = "";
//获取某一单元格的值
object obj = DG[cname].Value;
obj = obj == null ? "" : obj;
txt = obj.ToString();
return txt;
}
private static string GetGridRowVal(GridRow DG, int c)
{
string txt = "";
//获取某一单元格的值
object obj = DG[c].Value;
obj = obj == null ? "" : obj;
txt = obj.ToString();
return txt;
}
//获取某行某列的单元格数据
public static string GetGridVal(SuperGridControl SGC, int r, string cname)
{
// 获取某一行的信息
GridRow DG = GetGridRowByRowIndex(SGC, r);
return GetGridRowVal(DG,cname);
}
public static string GetGridVal(SuperGridControl SGC, int r, int c)
{
// 获取某一行的信息
GridRow DG = GetGridRowByRowIndex(SGC, r);// SGC.PrimaryGrid.Rows[r] as GridRow;
return GetGridRowVal(DG, c);
}
//判断checkbox的值
public static bool GetGridVal_CK(SuperGridControl SGC, int r, int c)
{
// 获取某一行的信息
GridRow DG = GetGridRowByRowIndex(SGC, r);// SGC.PrimaryGrid.Rows[r] as GridRow;
string strval = GetGridRowVal(DG, c).ToUpper();
if (strval == "1" || strval == "TRUE")
{
return true;
}
return false;
}
public static bool GetGridVal_CK(SuperGridControl SGC, int r, string cname)
{
GridRow DG = GetGridRowByRowIndex(SGC, r);// SGC.PrimaryGrid.Rows[r] as GridRow;
string strval = GetGridRowVal(DG, cname).ToUpper();
if (strval == "1" || strval == "TRUE")
{
return true;
}
return false;
}
//设置某行某列的单元格数据
public static void SetGridVal(SuperGridControl SGC, int r, int c, string val)
{
GridRow DG = GetGridRowByRowIndex(SGC, r);// SGC.PrimaryGrid.Rows[r] as GridRow;
DG[c].Value = val;
}
public static void SetGridVal(SuperGridControl SGC, int r, string cname, string val)
{
GridRow DG = GetGridRowByRowIndex(SGC, r);// SGC.PrimaryGrid.Rows[r] as GridRow;
DG[cname].Value = val;
}
/// <summary>创建列
///
/// </summary>
/// <param name="name">列的name</param>
/// <param name="headertxt">列的title</param>
/// <param name="cwidth">列宽</param>
/// <param name="ctp">列的类型 0默认,1check框,2下拉框,3按钮</param>
/// <returns></returns>
public static GridColumn CreatNewCol(string name, string headertxt, int cwidth, int ctp)
{
GridColumn gc = new GridColumn();
gc.Name = name;
gc.HeaderText = headertxt;
//gc.ReadOnly = true;
gc.Width = cwidth;
//gc.CellMergeMode = CellMergeMode.Vertical;
//gc.AllowSelection = false;
//gc.AllowEdit = false;
if (ctp == 1)
{
gc.EditorType = typeof(DevComponents.DotNetBar.SuperGrid.GridCheckBoxXEditControl);//列的类型
}
else if (ctp == 2)
{
gc.EditorType = typeof(DevComponents.DotNetBar.SuperGrid.GridComboBoxExEditControl);//列的类型
}
else if (ctp == 3)
{
gc.EditorType = typeof(DevComponents.DotNetBar.SuperGrid.GridButtonXEditControl);//列的类型
}
if (cwidth < 0)
{
gc.AutoSizeMode = ColumnAutoSizeMode.Fill;
}
return gc;
}
/// <summary>获取某列值为x所在行的另一列数据
///
/// </summary>
/// <param name="SGC"></param>
/// <param name="cname">要求的列</param>
/// <param name="cothername">已知数据的列</param>
/// <param name="cotherval">已知的数据</param>
/// <returns></returns>
public static string GetValByOtherColData(SuperGridControl SGC, string cname, string cothername, string cotherval)
{
string res = "";
int rs = GetGridRowCount(SGC);
if (rs < 1)
{
return res;
}
for (int i = 0; i < rs; i++)
{
string val = GetGridVal(SGC, i, cothername);
if (val == cotherval)
{
res = GetGridVal(SGC, i, cname);
}
}
return res;
}
public static int GetRowIndexByOtherColData(SuperGridControl SGC, string cname, string cothername, string cotherval)
{
int res = -1;
int rs = GetGridRowCount(SGC);
if (rs < 1)
{
return res;
}
for (int i = 0; i < rs; i++)
{
string val = GetGridVal(SGC, i, cothername);
if (val == cotherval)
{
res = i;
break;
}
}
return res;
}
//横的合并表头
public static ColumnGroupHeader GetAdContactHeader(GridColumnCollection columns, string str1, string str2, string title)
{
ColumnGroupHeader cgh = new ColumnGroupHeader();
cgh.MinRowHeight = 16;
cgh.Name = "titlename";
cgh.HeaderText = title;
cgh.AllowSelection = false;
// Set the start and end Display Index which the
// group header is associated with.
cgh.StartDisplayIndex = columns.GetDisplayIndex(columns[str1]);
cgh.EndDisplayIndex = columns.GetDisplayIndex(columns[str2]);
return (cgh);
}
private ColumnGroupHeader GetAdContactHeader(int stc, int endc, string title)
{
ColumnGroupHeader cgh = new ColumnGroupHeader();
cgh.MinRowHeight = 16;
cgh.Name = "titlename";
cgh.HeaderText = title;
cgh.AllowSelection = false;
cgh.StartDisplayIndex = stc;
cgh.EndDisplayIndex = endc;
return (cgh);
}
#region 例子
//创建一个 3行 n列的表格头
public static void testsgc(SuperGridControl SGC)
{
//先建好列
GridPanel panel = SGC.PrimaryGrid; panel.MinRowHeight = 20;
panel.Columns.Add(CreatNewCol("cs", "列0", 80, 0));
panel.Columns.Add(CreatNewCol("jz1", "列1", 80, 0));
panel.Columns.Add(CreatNewCol("jz2", "列2", 80, 0));
panel.Columns.Add(CreatNewCol("jz3", "列3", 80, 0));
panel.Columns.Add(CreatNewCol("jz4", "列4", 80, 0));
panel.Columns.Add(CreatNewCol("jz5", "列5", 80, 0));
panel.Columns.Add(CreatNewCol("ds1", "列1", 80, 0));
panel.Columns.Add(CreatNewCol("ds2", "列2", 80, 0));
panel.Columns.Add(CreatNewCol("ds3", "列3", 80, 0));
panel.Columns.Add(CreatNewCol("ds4", "列4", 80, 0));
panel.Columns.Add(CreatNewCol("dx1", "列1", 80, 0));
panel.Columns.Add(CreatNewCol("dx2", "列2", 80, 0));
panel.Columns.Add(CreatNewCol("dx3", "列3", 80, 0));
panel.Columns.Add(CreatNewCol("hmjr", "列1", 80, 0));
panel.Columns.Add(CreatNewCol("hmujr", "列2", 80, 0));
panel.Columns.Add(CreatNewCol("hm", "列3", 80, 0));
GridColumnCollection columns = panel.Columns;
// ColumnGroupHeader cgh = GetIlAdditionalInfoHeader(columns);
//设置列头合并
ColumnGroupHeader c1 = GetAdContactHeader(columns, "jz1", "jz5", "组1");//
ColumnGroupHeader c2 = GetAdContactHeader(columns, "ds1", "dx3", "组2");
ColumnGroupHeader c3 = GetAdContactHeader(columns, "ds1", "ds4", "组3");
ColumnGroupHeader c4 = GetAdContactHeader(columns, "dx1", "dx3", "组4");
c2.GroupHeaders.Add(c3); c2.GroupHeaders.Add(c4);
panel.ColumnHeader.GroupHeaders.Add(c1);
panel.ColumnHeader.GroupHeaders.Add(c2);
}
private void addrows(SuperGridControl SGC)
{
for (int i = 0; i < 100; i++)
{
GridRow Dfr = SGC.PrimaryGrid.NewRow();
Dfr[0].Value = i;
Dfr[1].Value = i;
Dfr[2].Value = i;
SGC.PrimaryGrid.Rows.Add(Dfr);
}
}
#endregion
}
来源:oschina
链接:https://my.oschina.net/u/4275560/blog/4304645