实例介绍
【实例截图】
【核心代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 | using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace MyCustomControls { public delegate void AfterRowSelectEventHandler( object sender, DataRow SelectedRow); /// <summary> /// Summary description for MultiColumnComboPopup. /// </summary> public class MultiColumnComboPopup : System.Windows.Forms.Form { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null ; private DataRow selectedRow = null ; private System.Windows.Forms.ListView lstvMyView; private DataRow[] inputRows = null ; private DataTable dataTable = null ; private int mCols = 0; private int mRows = 0; private string [] columnsToDisplay = null ; private string [][] data = null ; public event AfterRowSelectEventHandler AfterRowSelectEvent; public MultiColumnComboPopup() { InitializeComponent(); mCols = 4; mRows = 0; InitializeGridProperties(); } public MultiColumnComboPopup(DataRow[] drows) { InitializeComponent(); this .inputRows = drows; SetDataRows(inputRows); this .dataTable = inputRows[0].Table; } public MultiColumnComboPopup(DataTable dtable, ref DataRow selRow, string [] colsToDisplay) { InitializeComponent(); this .dataTable = dtable; this .selectedRow = selRow; this .columnsToDisplay = colsToDisplay; if (columnsToDisplay!= null ) SetDataTable(dataTable,columnsToDisplay); else SetDataTable(dataTable); } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if ( disposing ) { if (components != null ) { components.Dispose(); } } base .Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this .lstvMyView = new System.Windows.Forms.ListView(); this .SuspendLayout(); // // lstvMyView // this .lstvMyView.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this .lstvMyView.Dock = System.Windows.Forms.DockStyle.Fill; this .lstvMyView.FullRowSelect = true ; this .lstvMyView.GridLines = true ; this .lstvMyView.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable; this .lstvMyView.MultiSelect = false ; this .lstvMyView.Name = "lstvMyView" ; this .lstvMyView.Size = new System.Drawing.Size(292, 88); this .lstvMyView.TabIndex = 0; this .lstvMyView.View = System.Windows.Forms.View.Details; this .lstvMyView.KeyDown = new System.Windows.Forms.KeyEventHandler( this .lstvMyView_KeyDown); this .lstvMyView.DoubleClick = new System.EventHandler( this .lstvMyView_DoubleClick); // // MultiColumnComboPopup // this .AutoScaleBaseSize = new System.Drawing.Size(5, 13); this .ClientSize = new System.Drawing.Size(292, 88); this .ControlBox = false ; this .Controls.AddRange( new System.Windows.Forms.Control[] { this .lstvMyView}); this .FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; this .KeyPreview = true ; this .Name = "MultiColumnComboPopup" ; this .ShowInTaskbar = false ; this .StartPosition = System.Windows.Forms.FormStartPosition.Manual; this .KeyDown = new System.Windows.Forms.KeyEventHandler( this .MultiColumnComboPopup_KeyDown); this .Leave = new System.EventHandler( this .MultiColumnComboPopup_Leave); this .Deactivate = new System.EventHandler( this .MultiColumnComboPopup_Deactivate); this .ResumeLayout( false ); } #endregion #region Functions private void InitializeGridProperties() { lstvMyView.Items.Clear(); lstvMyView.Columns.Clear(); //Add Data Headers for ( int i=0;i<mCols;i ) { lstvMyView.Columns.Add(columnsToDisplay[i],-1,HorizontalAlignment.Left); } //Add Empty Values for ( int i=0;i<mRows;i ) { ListViewItem item = new ListViewItem( "" ); lstvMyView.Items.Add(item); for ( int j=0;j<mCols;j ) { item.SubItems.Add( " " ); } } } public void SetCellValue( int Row, int Column, string ItemValue) { if (IsValidRowAndColumn(Row,Column)) { if (Column == 0) { lstvMyView.Items[Row].Text = ItemValue; } else { lstvMyView.Items[Row].SubItems[Column].Text = ItemValue; } } } public string GetCellValue( int Row, int Column) { if (IsValidRowAndColumn(Row,Column)) { if (Column == 0) { return lstvMyView.Items[Row].Text; } else { return lstvMyView.Items[Row].SubItems[Column].Text; } } else { return null ; } } public void SetFullRow( int Row, string [] ItemValues) { if (IsValidRow(Row)) { if (ItemValues.Length > 0) { for ( int i=0;i<ItemValues.Length;i ) { SetCellValue(Row,i,ItemValues[i]); } } else { MessageBox.Show( this , "SetFullRow: Empty Values Sent" , "Grid Error" ); } } } public void SetFullRow( int Row, DataRow drow) { if (IsValidRow(Row)) { if (mCols > 0) { for ( int i=0;i<mCols;i ) { SetCellValue(Row,i,( string )drow[i]); } } else { MessageBox.Show( this , "SetFullRow: Empty Values Sent" , "Grid Error" ); } } } public void SetFullColumn( int Column, string [] ItemValues) { if (IsValidColumn(Column)) { if (ItemValues.Length > 0) { for ( int i=0;i<lstvMyView.Items.Count;i ) { SetCellValue(i,Column,ItemValues[i]); } } else { MessageBox.Show( this , "SetFullColumn: Empty Values Sent" , "Grid Error" ); } } } public string [] GetFullRow( int Row) { string [] ItemValues = new string [lstvMyView.Columns.Count]; if (IsValidRow(Row)) { for ( int i=0;i<lstvMyView.Items.Count;i ) { ItemValues[i] = GetCellValue(Row,i); } return ItemValues; } else { return null ; } } public string [] GetFullColumn( int Column) { string [] ItemValues = new string [lstvMyView.Items.Count]; if (IsValidColumn(Column)) { for ( int i=0;i<lstvMyView.Items.Count;i ) { ItemValues[i] = GetCellValue(i,Column); } return ItemValues; } else { return null ; } } public void AddItems( string [][] ItemValues) { if (ItemValues.Length > 0) { mRows = ItemValues.Length; mCols = ItemValues[0].Length; InitializeGridProperties(); for ( int i=0;i<mRows;i ) { SetFullRow(i,ItemValues[i]); } } else { MessageBox.Show( this , "AddItems: Empty Values Sent" , "Grid Error" ); } } public string [][] GetItems() { string [][] ItemValues= new string [lstvMyView.Items.Count][]; for ( int i=0;i<lstvMyView.Items.Count;i ) { ItemValues[i] = GetFullRow(i); } return ItemValues; } public void SetColumnHeaderNames( string [] ColumnNames) { for ( int i=0;i<ColumnNames.Length;i ) { if (i>=lstvMyView.Columns.Count) { lstvMyView.Columns.Add(ColumnNames[i],25,HorizontalAlignment.Center); lstvMyView.Columns[i].Width = ColumnNames[i].Length * ( int )Font.SizeInPoints; } else { lstvMyView.Columns[i].Text = ColumnNames[i]; lstvMyView.Columns[i].Width = ColumnNames[i].Length * ( int )Font.SizeInPoints; } } } public void SetColumnWidths( int [] Widths) { for ( int i=0;i<Widths.Length;i ) { if (i >= lstvMyView.Columns.Count) { lstvMyView.Columns.Add( "" ,Widths[i] * ( int )lstvMyView.Font.SizeInPoints ,HorizontalAlignment.Center); } else { lstvMyView.Columns[i].Width = Widths[i] * ( int )Font.SizeInPoints; } } } public void SetColumnWidth( int Column, int ColWidth) { if (IsValidColumn(Column)) { lstvMyView.Columns[Column].Width = ColWidth * ( int )Font.SizeInPoints; } } public void SetDataTable(DataTable dtable) { DataColumnCollection dcc = dtable.Columns; DataRowCollection drc = dtable.Rows; data = new string [drc.Count][]; string [] strarrColNames = null ; int ictr = 0; for ( int i=0;i<drc.Count;i ) { data[i] = new string [dcc.Count]; } foreach (DataRow dr in drc) { for ( int i=0;i<dcc.Count;i ) { data[ictr][i] = dr[i].ToString(); } ictr ; } strarrColNames = new string [dcc.Count]; for ( int i=0;i<dcc.Count;i ) { strarrColNames[i] = dcc[i].ColumnName; } AddItems(data); //SetColumnHeaderNames(strarrColNames); } public void SetDataTable(DataTable dtable, string [] colsToDisplay) { DataRowCollection drc = dtable.Rows; data = new string [drc.Count][]; int ictr = 0; for ( int i=0;i<drc.Count;i ) { data[i] = new string [colsToDisplay.Length]; } foreach (DataRow dr in drc) { for ( int i=0;i<colsToDisplay.Length;i ) { data[ictr][i] = dr[colsToDisplay[i]].ToString(); } ictr ; } AddItems(data); //SetColumnHeaderNames(colsToDisplay); } private bool IsValidRowAndColumn( int Row, int Column) { if (Column < 0 || Row < 0) { MessageBox.Show( this , "Row or Column Cannot be Negative!" , "Grid Error" ); return false ; } else if (Row > lstvMyView.Items.Count) { MessageBox.Show( this , "SetCellValue: Row Out Of Range" , "Grid Error" ); return false ; } else if (Column > lstvMyView.Columns.Count) { MessageBox.Show( this , "SetCellValue: Column Out Of Range " Column.ToString(), "Grid Error" ); return false ; } else { return true ; } } private bool IsValidRow( int Row) { if (Row < 0) { MessageBox.Show( this , "Row Cannot be Negative!" , "Grid Error" ); return false ; } else if (Row > lstvMyView.Items.Count) { MessageBox.Show( this , "Row Out Of Range" , "Grid Error" ); return false ; } else { return true ; } } private bool IsValidColumn( int Column) { if (Column < 0) { MessageBox.Show( this , "Column Cannot be Negative!" , "Grid Error" ); return false ; } else if (Column > lstvMyView.Columns.Count) { MessageBox.Show( this , "Column Out Of Range " Column.ToString() , "Grid Error" ); return false ; } else { return true ; } } #endregion private void gridValue_SelectedIndexChanged( object sender, System.EventArgs e) { // } public void SetDataRows(DataRow[] drows){ mRows = drows.Length; mCols = drows[0].Table.Columns.Count; InitializeGridProperties(); for ( int i=0;i<mRows;i ) { SetFullRow(i,drows[i]); } SetColumnHeaderNames(GetColumnNames(drows[0])); } private string [] GetColumnNames(DataRow drow){ string [] strColNames = new string [drow.Table.Columns.Count]; for ( int i=0;i<drow.Table.Columns.Count;i ){ strColNames[i] = drow.Table.Columns[i].ColumnName; } return strColNames; } private void MultiColumnComboPopup_Deactivate( object sender, System.EventArgs e) { this .Close(); } private void MultiColumnComboPopup_Leave( object sender, System.EventArgs e) { this .Close(); } private void MultiColumnComboPopup_KeyDown( object sender, System.Windows.Forms.KeyEventArgs e) { if (e.KeyData==Keys.Escape){ this .Close(); } } private void lstvMyView_KeyDown( object sender, System.Windows.Forms.KeyEventArgs e) { if (e.KeyData==Keys.Enter){ if (lstvMyView.SelectedItems.Count>0){ selectedRow = dataTable.Rows[lstvMyView.SelectedIndices[0]]; if (AfterRowSelectEvent!= null ) AfterRowSelectEvent( this ,selectedRow); } this .Close(); } } private void lstvMyView_DoubleClick( object sender, System.EventArgs e) { if (lstvMyView.SelectedItems.Count>0){ selectedRow = dataTable.Rows[lstvMyView.SelectedIndices[0]]; if (AfterRowSelectEvent!= null ) AfterRowSelectEvent( this ,selectedRow); } this .Close(); } private string [] GetColumnNames(DataTable dtable){ string [] strColNames = new string [dtable.Columns.Count]; for ( int i=0;i<dtable.Columns.Count;i ){ strColNames[i] = dtable.Columns[i].ColumnName; } return strColNames; } public DataRow[] InputRows{ set { inputRows = value; if (inputRows == null ) return ; SetDataRows(inputRows); } } // public string[] NonDisplayColumnNames{ // set{ // // } // } public DataTable Table{ set { dataTable = value; if (dataTable == null ) return ; SetDataTable(dataTable); } } public DataRow SelectedRow{ get { return selectedRow; } } public int Cols { get { mCols = lstvMyView.Columns.Count; return mCols; } } public int Rows { get { mRows = lstvMyView.Items.Count; return mRows; } } } } |
标签: ComboBox
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论