当前位置: 首页 > 编程语言 > C#

在DatagridViewCell中显示用户控件Share

时间:2023-04-10 21:51:28 C#

在DatagridViewCell中显示用户控件Winforms.NET3.5(C#)我有一个DataGridView(DGView),我创建了CustomColumn和CustomCell在DGView中显示。我创建了一个CustomUserControl,我想在CustomCell中显示它。问题:我没有在列中看到用户控件。我想我需要重写CustomCell中的Paint()方法-我该怎么做?注意-托管用户控件的MSDN示例用于编辑单元格值-您可以在编辑单元格的位置使用户控件可见。我希望我的用户控件呈现为普通的winform控件。此用户控件显示行的通知。每行可以有不同的通知。我希望用户能够点击通知并获取有关它的更多详细信息。..但现在我坚持“如何显示此用户控件”任何指针将不胜感激。publicclassCustomColumn:DataGridViewColumn{publicCustomColumn():base(newCustomeCell()){}publicoverrideDataGridViewCellCellTemplate{get{returnbase.CellTemplate;}set{//确保用于模板的单元格是CalendarCell。if(value!=null&&!value.GetType().IsAssignableFrom(typeof(CustomeCell))){thrownewInvalidCastException("它应该是一个自定义单元格");}base.CellTemplate=值;}}}publicclassCustomeCell:DataGridViewTextBoxCell{publicCustomeCell():base(){}publicoverrideTypeValueType{get{returntypeof(CustomUserControl);}}publicoverrideTypeFormattedValueType{get{returntypeof(CustomUserControl);控件放置在我需要它们的网格上。问题:滚动数据网格视图需要重新排列所有这些用户控件。结果——被拒绝。第二次尝试:我构建了一个用户控件并将其绘制在适当的单元格中。结果-到目前为止工作。我只是覆盖CustomCell类中的DataGridViewCellPaint和OnClick方法。publicclassCustomeCell:DataGridViewCell{publicoverrideTypeValueType{get{returntypeof(CustomUserControl);}}protectedoverridevoidPaint(Graphicsgraphics,RectangleclipBounds,RectanglecellBounds,introwIndex,DataGridViewElementStatescellState,objectvalue,objectformattedValue,stringerrorText,DataGridViewCellStylecellStyle,DataGridViewAdvancedBorderStyleadvancedBorderStyle,DataGridViewPaintPartspaintParts){varctrl=(CustomUserControl)值;varimg=newBitmap(cellBounds.Width,cellBounds.Height);ctrl.DrawToBitmap(img,newRectangle(0,0,ctrl.Width,ctrl.Height));graphics.DrawImage(img,cellBounds.Location);}protectedoverridevoidOnClick(DataGridViewCellEventArgse){Listobjs=DataGridView.DataSourceasList;如果(objs==null)返回;如果(e.RowIndex=objs.Count)返回;CustomUserControlctrl=objs[e.RowIndex].Ctrl;//采取任何行动-我现在只改变颜色。ctrl.BackColor=列或.红色;Ctrl.Refresh();DataGridView.InvalidateCell(e.ColumnIndex,e.RowIndex);}}该示例在CustomControl的CustomCell中呈现CustomColumn;)当用户单击该单元格时,CustomCell的OnClick处理点击。理想情况下,我想将该点击委托给自定义用户控件CustomControl-它应该像处理点击本身一样处理事件(自定义用户控件可以在内部托管多个控件)-所以它的复杂性是最小的。publicclassCustomColumn:DataGridViewColumn{publicCustomColumn():base(newCustomeCell()){}publicoverrideDataGridViewCellCellTemplate{get{returnbase.CellTemplate;}set{if(value!=null&&!value.GetType().IsAssignableFrom(typeof(CustomeCell)))thrownewInvalidCastException("ItshouldbeacustomCell");base.CellTemplate=值;DataGridView控件仅支持在单元格处于编辑模式时显示实际控件。DataGridView控件并非设计用于显示多个控件或每行重复一组控件。当单元格未被编辑时,DataGridView控件绘制控件的表示。这种表示可以根据需要尽可能详细。例如,无论单元格是否正在编辑,DataGridViewButtonCell都会绘制一个按钮。但是,您可以通过DataGridView.Controls.Add()方法添加控件并设置它们的位置和大小,以便它们托管在单元格中,但是无论编辑如何在所有单元格中显示控件都是没有意义的。在这里阅读[更新-来自MSDataGridView团队的ProgManager]http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/394e04a8-d918-4fdd-be03-dfa13f6a1e66?persist=True以上是C#学习教程:在DatagridViewCell中显示用户控件共享的所有内容。如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员。删除。如需转载请注明出处: