问题描述
急求大神们%>_<%publicpartialclassNewCombox:ComboBox{publicintcolumnPadding=5;privateDataGridViewDGV;publicfloat[]columnWidths={50,150,100};//项宽度publicString[]columnNames={"title_id","au_id","au_ord"};//项名称publicintvalueMemberColumnIndex=0;//valueMember属性列所在的索引privateDataTabledataTable=newDataTable("Student");publicstringvalueText="";publicDataTablepubtable=newDataTable();publicNewCombox(DataGridViewdgv){this.DGV=dgv;InitializeComponent();this.AutoCompleteMode=System.Windows.Forms.AutoCompleteMode.Suggest;this.DrawMode=DrawMode.OwnerDrawVariable;//手动绘制所有元素this.DropDownHeight=60;//自己设置下拉框最长到多长显示滚动条this.IntegralHeight=false;InitItems();}publicNewCombox(stringvaluetext){this.valueText=valuetext;InitializeComponent();this.DropDownHeight=30;//自己设置下拉框最长到多长显示滚动条this.IntegralHeight=false;InitItems();}privateDataTableCreateDataTable(strings){SqlConnectionconn=newSqlConnection(@"server=2012-20140123spsqlexpress;userid=sa;password=zr199406;database=publish");SqlDataAdapterdapt2;DataSetds=newDataSet();stringsql2;sql2="selecttitle_id,au_id,au_ordfromdbo.titleauthorwheretitle_idlike'"+s.Replace("'","''")+"%'";if(s==null||s==""){sql2="selecttitle_id,au_id,au_ordfromdbo.titleauthorwheretitle_idlike'"+s+"%'";//可以把s改成你想要的默认值}dapt2=newSqlDataAdapter(sql2,conn);dapt2.Fill(ds,"table1");DataTabledtable=newDataTable();dtable.Columns.Add("title_id",typeof(System.String));dtable.Columns.Add("au_id",typeof(System.String));dtable.Columns.Add("au_ord",typeof(System.String));//dtable.Columns.Add("royaltyper",typeof(System.String));dtable=ds.Tables["table1"];returndtable;}//获取相应的数据tablepublicvoidInitItems(){dataTable=CreateDataTable(this.valueText);pubtable=dataTable;this.DataSource=dataTable;this.DisplayMember="title_id";this.ValueMember="title_id";if((this.DataSource!=null)&&(!string.IsNullOrEmpty(this.DisplayMember))){if(!string.IsNullOrEmpty(this.Text)){DataViewdv=dataTable.DefaultView;dv.RowFilter=string.Format("title_idlike'{0}%'",this.valueText);DataTableNewDt=dv.ToTable("newTableName");DataTablecopyTable=newDataTable();copyTable.Columns.Add("title_id",Type.GetType("System.String"));copyTable.Columns.Add("au_id",Type.GetType("System.String"));copyTable.Columns.Add("au_ord",Type.GetType("System.String"));//copyTable.Columns.Add("royaltyper",Type.GetType("System.String"));copyTable.Rows.Add(newobject[]{"title_id","au_id","au_ord"});for(inti=0;i<NewDt.Rows.Count;i++){copyTable.Rows.Add(newobject[]{NewDt.Rows[i][0].ToString()+"t",NewDt.Rows[i][1].ToString()+"t",NewDt.Rows[i][2].ToString()+"t"});}//copyTable.Rows.Add(newobject[]{this.valueText,"","",""});this.DataSource=copyTable;pubtable=copyTable;}else{this.DataSource=dataTable;pubtable=dataTable;}this.DropDownWidth=(int)CalculateTotalWidth();//计算下拉框的总宽度}}privatefloatCalculateTotalWidth(){columnPadding=5;floattotalWidth=0;foreach(intwidthincolumnWidths){totalWidth+=(width+columnPadding);}returntotalWidth+SystemInformation.VerticalScrollBarWidth;}protectedoverridevoidOnDrawItem(DrawItemEventArgse){base.OnDrawItem(e);if(DesignMode){return;}RectangleboundsRect=e.Bounds;//获取绘制项边界的矩形e.Graphics.FillRectangle(Brushes.White,e.Bounds);if(e.State==DrawItemState.Selected){//thiscodekeepsthelastitemdrawnfromhavingaBisquebackground.e.Graphics.FillRectangle(Brushes.Bisque,e.Bounds);}intlastRight=0;using(PenlinePen=newPen(SystemColors.GrayText)){using(SolidBrushbrush=newSolidBrush(ForeColor)){if(columnNames.Length==0){e.Graphics.DrawString(Convert.ToString(Items[e.Index]),Font,brush,boundsRect);}else{//循环各列for(inti=0;i<columnNames.Length;i++){stringitem=Convert.ToString(FilterItemOnProperty(Items[e.Index],columnNames[i]));boundsRect.X=lastRight;//列的左边位置boundsRect.Width=(int)columnWidths[i]+columnPadding;//列的宽度//lastRight=boundsRect.Right;if(i==valueMemberColumnIndex)//如果是valuemember{using(Fontfont=newFont(Font,FontStyle.Bold)){//绘制项的内容e.Graphics.DrawString(item,font,brush,boundsRect);}}else{//绘制项的内容e.Graphics.DrawString(item,Font,brush,boundsRect);}//绘制各项间的竖线if(i<columnNames.Length-1){e.Graphics.DrawLine(linePen,boundsRect.Right,boundsRect.Top,boundsRect.Right,boundsRect.Bottom);}}}}}e.DrawFocusRectangle();}protectedoverridevoidOnKeyUp(KeyEventArgse){this.valueText=this.Text;InitItems();this.DroppedDown=true;if(this.Text!=null&&this.Text!=string.Empty){//获得光标位置this.Text=this.valueText;this.SelectionStart=this.Text.Length;}this.Cursor=Cursors.Default;base.OnKeyUp(e);}}已经可以动态显示,并且加入到datagridview里面,但是选中某一行数据之后,没有办法更改所在单元格和这一行的数据怎么办??谢谢大家了!!
解决方案
解决方案二:
问题没描述清楚?代码多,但又不全?各个函数的关系的代码?