WinForm控件--DotNetBar--SuperGridControl
作者:互联网
1 //SuperGridControl控件初始化 2 private void ResetLayout(SuperGridControl SGC) 3 { 4 GridPanel panel = SGC.PrimaryGrid; 5 GridColumnCollection columns = panel.Columns; 6 7 for (int i = 0; i < columns.Count; i++) 8 { 9 GridColumn col = columns[i]; 10 11 col.Visible = false; 12 col.DisplayIndex = -1; 13 14 col.HeaderStyles.Default.Image = null; 15 col.HeaderStyles.Default.Background = null; 16 col.HeaderStyles.MouseOver.Background = null; 17 col.CellStyles.Default.Background = null; 18 } 19 panel.ColumnHeader.GroupHeaders.Clear(); 20 panel.ClearAll(); 21 //SGC.PrimaryGrid.ColumnHeaderClickBehavior = ColumnHeaderClickBehavior.None;//表头点击没效果 22 } 23 private GridColumn CreatNewCol(string name, string headertxt, int cwidth,int ctp) 24 { 25 GridColumn gc = new GridColumn(); 26 gc.Name = name; 27 gc.HeaderText = headertxt; 28 //gc.ReadOnly = true; 29 gc.Width = cwidth; 30 //gc.CellMergeMode = CellMergeMode.Vertical; 31 //gc.AllowSelection = false; 32 //gc.AllowEdit = false; 33 //ctp:(0默认,1check框,2:下拉框) 34 if (ctp==1) 35 { 36 gc.EditorType = typeof(DevComponents.DotNetBar.SuperGrid.GridCheckBoxXEditControl);//列的类型 37 } 38 else if(ctp==2) 39 { 40 gc.EditorType = typeof(DevComponents.DotNetBar.SuperGrid.GridComboBoxExEditControl);//列的类型 41 } 42 return gc; 43 } 44 //创建一个 3行 n列的表格头 45 private void testsgc(SuperGridControl SGC) 46 { 47 //先建好列 48 GridPanel panel = SGC.PrimaryGrid; panel.MinRowHeight = 20; 49 panel.Columns.Add(CreatNewCol("cs", "列0", 80,0)); 50 panel.Columns.Add(CreatNewCol("jz1", "列1", 80, 0)); 51 panel.Columns.Add(CreatNewCol("jz2", "列2", 80, 0)); 52 panel.Columns.Add(CreatNewCol("jz3", "列3", 80, 0)); 53 panel.Columns.Add(CreatNewCol("jz4", "列4", 80, 0)); 54 panel.Columns.Add(CreatNewCol("jz5", "列5", 80, 0)); 55 56 panel.Columns.Add(CreatNewCol("ds1", "列1", 80, 0)); 57 panel.Columns.Add(CreatNewCol("ds2", "列2", 80, 0)); 58 panel.Columns.Add(CreatNewCol("ds3", "列3", 80, 0)); 59 panel.Columns.Add(CreatNewCol("ds4", "列4", 80, 0)); 60 61 panel.Columns.Add(CreatNewCol("dx1", "列1", 80, 0)); 62 panel.Columns.Add(CreatNewCol("dx2", "列2", 80, 0)); 63 panel.Columns.Add(CreatNewCol("dx3", "列3", 80, 0)); 64 65 panel.Columns.Add(CreatNewCol("hmjr", "列1", 80, 0)); 66 panel.Columns.Add(CreatNewCol("hmujr", "列2", 80, 0)); 67 panel.Columns.Add(CreatNewCol("hm", "列3", 80, 0)); 68 GridColumnCollection columns = panel.Columns; 69 // ColumnGroupHeader cgh = GetIlAdditionalInfoHeader(columns); 70 //设置列头合并 71 ColumnGroupHeader c1 = GetAdContactHeader(columns, "jz1", "jz5", "组1");// 72 ColumnGroupHeader c2 = GetAdContactHeader(columns, "ds1", "dx3", "组2"); 73 ColumnGroupHeader c3 = GetAdContactHeader(columns, "ds1", "ds4", "组3"); 74 ColumnGroupHeader c4 = GetAdContactHeader(columns, "dx1", "dx3", "组4"); 75 c2.GroupHeaders.Add(c3); c2.GroupHeaders.Add(c4); 76 77 panel.ColumnHeader.GroupHeaders.Add(c1); 78 panel.ColumnHeader.GroupHeaders.Add(c2); 79 } 80 //横的合并表头 81 private ColumnGroupHeader GetAdContactHeader(GridColumnCollection columns, string str1, string str2, string title) 82 { 83 ColumnGroupHeader cgh = new ColumnGroupHeader(); 84 85 cgh.MinRowHeight = 16; 86 87 cgh.Name = "titlename"; 88 cgh.HeaderText = title; 89 cgh.AllowSelection = false; 90 91 92 // Set the start and end Display Index which the 93 // group header is associated with. 94 95 cgh.StartDisplayIndex = columns.GetDisplayIndex(columns[str1]); 96 cgh.EndDisplayIndex = columns.GetDisplayIndex(columns[str2]); 97 98 return (cgh); 99 } 100 private ColumnGroupHeader GetAdContactHeader(int stc, int endc, string title) 101 { 102 ColumnGroupHeader cgh = new ColumnGroupHeader(); 103 cgh.MinRowHeight = 16; 104 cgh.Name = "titlename"; 105 cgh.HeaderText = title; 106 cgh.AllowSelection = false; 107 cgh.StartDisplayIndex = stc; 108 cgh.EndDisplayIndex = endc; 109 return (cgh); 110 } 111 private void addrows(SuperGridControl SGC) 112 { 113 for (int i = 0; i < 100; i++) 114 { 115 GridRow Dfr = SGC.PrimaryGrid.NewRow(); 116 Dfr[0].Value = i; 117 Dfr[1].Value = i; 118 Dfr[2].Value = i; 119 SGC.PrimaryGrid.Rows.Add(Dfr); 120 } 121 } 122 //获取R行C列的单元格数据 123 private string GetGridVal(SuperGridControl SGC, int r, int c) 124 { 125 string txt = ""; 126 // 获取某一行的信息 127 GridRow DG = GetGridRowIfGroup(SGC, r);// SGC.PrimaryGrid.Rows[r] as GridRow; 128 //设置某一单元的背景颜色 129 // DG[c].CellStyles.Default.Background.Color1 = Color.Red; 130 //获取某一单元格的值 131 object obj = DG[c].Value; 132 obj = obj == null ? "" : obj; 133 txt = obj.ToString(); 134 return txt; 135 } 136 //判断checkbox的值,打勾了为1 137 private int GetGridVal_CK(SuperGridControl SGC, int r, int c) 138 { 139 // 获取某一行的信息 140 GridRow DG = GetGridRowIfGroup(SGC, r);// SGC.PrimaryGrid.Rows[r] as GridRow; 141 //设置某一单元的背景颜色 142 // DG[c].CellStyles.Default.Background.Color1 = Color.Red; 143 //获取某一单元格的值 144 object obj = DG[c].Value; 145 if (obj==null) 146 { 147 return 0; 148 } 149 string strobj=obj.ToString().ToUpper(); 150 if (strobj == "1" || strobj=="TRUE") 151 { 152 return 1; 153 } 154 return 0; 155 } 156 private string GetGridVal(SuperGridControl SGC, int r, string cname) 157 { 158 string txt = ""; 159 // 获取某一行的信息 160 GridRow DG = GetGridRowIfGroup(SGC, r);// SGC.PrimaryGrid.Rows[r] as GridRow; 161 //设置某一单元的背景颜色 162 // DG[c].CellStyles.Default.Background.Color1 = Color.Red; 163 //获取某一单元格的值 164 object obj = DG[cname].Value; 165 obj = obj == null ? "" : obj; 166 txt = obj.ToString(); 167 return txt; 168 } 169 private int GetGridVal_CK(SuperGridControl SGC, int r, string cname) 170 { 171 //string txt = ""; 172 // 获取某一行的信息 173 GridRow DG = GetGridRowIfGroup(SGC, r);// SGC.PrimaryGrid.Rows[r] as GridRow; 174 //设置某一单元的背景颜色 175 // DG[c].CellStyles.Default.Background.Color1 = Color.Red; 176 //获取某一单元格的值 177 object obj = DG[cname].Value; 178 if (obj == null) 179 { 180 return 0; 181 } 182 string strobj = obj.ToString().ToUpper(); 183 if (strobj == "1" || strobj == "TRUE") 184 { 185 return 1; 186 } 187 return 0; 188 } 189 //设置R行C列的单元格数据 190 private void SetGridVal(SuperGridControl SGC, int r, int c, string val) 191 { 192 GridRow DG = GetGridRowIfGroup(SGC, r);// SGC.PrimaryGrid.Rows[r] as GridRow; 193 DG[c].Value = val; 194 } 195 private void SetGridVal(SuperGridControl SGC, int r, string cname, string val) 196 { 197 GridRow DG = GetGridRowIfGroup(SGC, r);// SGC.PrimaryGrid.Rows[r] as GridRow; 198 DG[cname].Value = val; 199 } 200 201 /// <summary>获取某列值为x所在行的另一列数据 202 /// 203 /// </summary> 204 /// <param name="SGC"></param> 205 /// <param name="cname">要求的列</param> 206 /// <param name="cothername">已知数据的列</param> 207 /// <param name="cotherval">已知的数据</param> 208 /// <returns></returns> 209 private string GetValByOtherColData(SuperGridControl SGC,string cname,string cothername,string cotherval) 210 { 211 string res=""; 212 int rs=SGC.PrimaryGrid.Rows.Count; 213 if (rs<1) 214 { 215 return res; 216 } 217 for (int i = 0; i < rs;i++ ) 218 { 219 string val = GetGridVal(SGC, i, cothername); 220 if (val == cotherval) 221 { 222 res = GetGridVal(SGC, i, cname); 223 } 224 } 225 return res; 226 } 227 private int GetRowByOtherColData(SuperGridControl SGC, string cname, string cothername, string cotherval) 228 { 229 int res = -1; 230 int rs = SGC.PrimaryGrid.Rows.Count; 231 if (rs < 1) 232 { 233 return res; 234 } 235 for (int i = 0; i < rs; i++) 236 { 237 string val = GetGridVal(SGC, i, cothername); 238 if (val == cotherval) 239 { 240 res = i; 241 break; 242 } 243 } 244 return res; 245 } 246 //根据行号获取GridRow对象(考虑到分组对行数据的影响) 247 private GridRow GetGridRowIfGroup(SuperGridControl SGC, int r) 248 { 249 GridPanel gp = SGC.PrimaryGrid; 250 int startindex = 0; 251 foreach (GridElement item in gp.Rows) 252 { 253 if (item is GridGroup) 254 { 255 GridGroup gg = item as GridGroup; 256 foreach (GridElement item1 in gg.Rows) 257 { 258 if (item1 is GridRow) 259 { 260 if (startindex==r) 261 { 262 return (GridRow)item1; 263 } 264 startindex++; 265 } 266 } 267 } 268 else if(item is GridRow) 269 { 270 if (startindex == r) 271 { 272 return (GridRow)item; 273 } 274 startindex++; 275 } 276 } 277 return null; 278 } 279 //获取所有行(因分组了会导致Rows获取的是分组的数) 280 private int GetGridRowIfGroup(SuperGridControl SGC) 281 { 282 GridPanel gp = SGC.PrimaryGrid; 283 int startindex = 0; 284 foreach (GridElement item in gp.Rows) 285 { 286 if (item is GridGroup) 287 { 288 GridGroup gg = item as GridGroup; 289 foreach (GridElement item1 in gg.Rows) 290 { 291 if (item1 is GridRow) 292 { 293 startindex++; 294 } 295 } 296 } 297 else if (item is GridRow) 298 { 299 startindex++; 300 } 301 } 302 return startindex; 303 }
标签:DotNetBar,控件,string,int,SuperGridControl,Add,SGC,Columns,panel 来源: https://www.cnblogs.com/happyqiang/p/10418237.html