问题描述
请问在Combo的下拉列表中可以显示一个DataTable列表吗?
解决方案
解决方案二:
可以,但是需要自己来绘制ComboBox的下拉列表。
解决方案三:
可以,但是只可以绑定一列.
解决方案四:
可以通过继承该控件来做.如点击ComboBox则显示一个DataGrid
解决方案五:
是可以的。我以前写过一个,代码发给你:usingSystem;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Windows.Forms;namespaceLooongSoft{///<summary>///支持多列数据显示的ComboBox///</summary>publicclassMultiColumnComboBox:ComboBox{#regionvariablesprivateSystem.ComponentModel.Containercomponents=null;//确定哪些列显示出来privateint[]_displayColumns=null;//计算显示列的显示宽度privateint[]_displayWidths=null;//数据表privateDataTable_dtSource=null;//显示的数据行privateDataRow[]_drDisplay=null;//列与列之间的间隙privateint_columnSpacing=4;//是否是第一次初始化itemsprivatebool_needInitItem=true;//列分隔符privatebool_needSeperator=false;#endregion#regioninitializepublicMultiColumnComboBox(){if(components==null)components=null;this.DrawMode=System.Windows.Forms.DrawMode.OwnerDrawVariable;}#regionComponentDesignergeneratedcode/**////<summary>///RequiredmethodforDesignersupport-donotmodify///thecontentsofthismethodwiththecodeeditor.///</summary>privatevoidInitializeComponent(){}#endregion#endregion#regionInheritedeventsprotectedoverridevoidOnDrawItem(DrawItemEventArgse){try{intiIndex=e.Index;if(_drDisplay==null)return;if(iIndex>=_drDisplay.Length)return;if(iIndex>-1){intiXPos=0;intiYPos=0;if(_drDisplay.Length==0)return;e.DrawBackground();//处理分隔符intiLeftSpacing1=_columnSpacing/2;intiLeftSpacing2=_columnSpacing-iLeftSpacing1;for(intindex=0;index<_dtSource.Columns.Count;index++){if(!this.ColumnIsDisplay(index,_displayColumns))continue;intidwIndex=GetColumnIndexOfArray(_displayColumns,index);intidw=idwIndex==-1?80:_displayWidths[idwIndex];e.Graphics.DrawString(_drDisplay[iIndex][index].ToString(),Font,newSolidBrush(e.ForeColor),newRectangleF(iXPos,e.Bounds.Y,idw,ItemHeight));//iXPos+=idw+iLeftSpacing1;if(NeedSeperator){if(index!=0&&index!=_dtSource.Columns.Count-1){//e.Graphics.DrawString(Seperator,Font,newSolidBrush(e.ForeColor),newRectangleF(iXPos+idw+iLeftSpacing1,e.Bounds.Y,iLeftSpacing1,ItemHeight));e.Graphics.DrawLine(newPen(e.ForeColor),newPointF(iXPos+idw-iLeftSpacing1,iIndex*ItemHeight),newPointF(iXPos+idw-iLeftSpacing1,(iIndex+1)*ItemHeight));}}iXPos+=idw;}iXPos=0;iYPos+=ItemHeight;e.DrawFocusRectangle();base.OnDrawItem(e);}}catch(Exceptionex){thrownewException(ex.Message+"rnInColumnComboBox.OnDrawItem(DrawItemEventArgs).");}}
解决方案六:
protectedoverridevoidOnDropDown(EventArgse){if(_needInitItem){InitItems();_needInitItem=false;}InitDisplay();base.OnDropDown(e);}protectedoverridevoidOnDataSourceChanged(EventArgse){base.OnDataSourceChanged(e);InitDisplay();}#endregion#regionProperty[Category("MultiColumn")][Description("显示的列,分别用对应列的index来表示.")]publicint[]DisplayColumns{get{return_displayColumns;}set{_displayColumns=value;}}[Category("MultiColumn")][Description("显示的数据")]publicDataTableData{get{return_dtSource;}set{DataSource=value;_dtSource=value;}}[Category("MultiColumn")][Description("不同列之间是否需要分隔符")]publicboolNeedSeperator{get{return_needSeperator;}set{_needSeperator=value;}}#endregion#regionprivatemethodsprivateboolColumnIsDisplay(intcolumn,int[]columns){if(column<0)thrownewException("columnmustbegreaterthan0.rn(privatemethod)ColumnIsDisplay(int,int[])");if(columns==null){columns=newint[1];columns[0]=0;}elseif(_dtSource!=null){if(columns.Length>_dtSource.Columns.Count)thrownewException("DisplayColumns'countcannotbegreaterthancountofdatasource.rn(privatemethod)ColumnIsDisplay(int,int[])");}foreach(intcincolumns){if(c==column)returntrue;}returnfalse;}privateintGetColumnIndexOfArray(int[]columns,intcolumn){intiResult=-1;if(column<0)thrownewException("columnmustbegreaterthan0.rn(privatemethod)ColumnIsDisplay(int,int[])");if(_dtSource!=null){if(columns.Length>_dtSource.Columns.Count)thrownewException("DisplayColumns'countcannotbegreaterthancountofdatasource.rn(privatemethod)ColumnIsDisplay(int,int[])");}for(intc=0;c<columns.Length;c++){if(columns[c]==column)returnc;}returniResult;}privatevoidInitDisplay(){_displayWidths=newint[_displayColumns.Length>0?_displayColumns.Length:1];SizeFsize=newSizeF(10000,ItemHeight);Graphicsgraphics=CreateGraphics();foreach(DataRowdrinthis._drDisplay){for(inti=0;i<this._dtSource.Columns.Count;i++){intiexist=this.GetColumnIndexOfArray(_displayColumns,i);if(iexist!=-1){stringstmp=dr[i].ToString();intitmpwidth=(int)graphics.MeasureString(stmp,Font,size).Width;if(itmpwidth>_displayWidths[iexist])_displayWidths[iexist]=itmpwidth;}}}graphics.Dispose();intiwid=0;for(intw=0;w<_displayWidths.Length;w++){_displayWidths[w]+=_columnSpacing;iwid+=_displayWidths[w];}DropDownWidth=1;if(iwid>Width)Width=iwid;}privatevoidInitItems(){_drDisplay=_dtSource.Select();}#endregion}}